Skip to content

Commit

Permalink
fix: lint
Browse files Browse the repository at this point in the history
  • Loading branch information
igobranco committed Jan 21, 2025
1 parent 8129603 commit c7b3b8e
Show file tree
Hide file tree
Showing 3 changed files with 177 additions and 55 deletions.
64 changes: 49 additions & 15 deletions nau_openedx_extensions/studio/management/commands/export_course.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
"""
This command has 2 execution modes, one for exporting course content and another for transferring course content.
It dispatch celery tasks that or export to a tar.gz file or transfer course content and upload it to the course 'GRADES_DOWNLOAD' storage.
It dispatch celery tasks that or export to a tar.gz file or transfer course content and upload it to the course
'GRADES_DOWNLOAD' storage.
Warn: this django command will dispatch multiple celery tasks, making the CMS celery workers having many pending tasks.
Please check the CMS_URL/heartbeat?extended to monitor its progress.
Expand All @@ -15,47 +16,73 @@
python manage.py cms export_course --task export --username <my_username>
Transfer all courses:
python manage.py cms export_course --task transfer --username <my_username>
python manage.py cms export_course --task transfer --username <my_username>
Transfer a specific course:
python manage.py cms export_course --task transfer --username <my_username> <course_id_1>,<course_id_2>,<course_id_3>
"""

from time import sleep

from cms.djangoapps.contentstore.tasks import export_olx # lint-amnesty, pylint: disable=import-error
from django.contrib.auth import get_user_model
from django.core.management.base import BaseCommand
from xmodule.modulestore.django import modulestore

from nau_openedx_extensions.studio.contentstore.tasks import \
transfer_course_content # lint-amnesty, pylint: disable=import-error
from nau_openedx_extensions.studio.contentstore.tasks import ( # lint-amnesty, pylint: disable=import-error
transfer_course_content,
)

User = get_user_model()


def get_task_name(task_name):
"""
Generate the task givent its name.
"""
if task_name == 'export':
if task_name == "export":
# The upstream export course content task
return export_olx
elif task_name == 'transfer':
elif task_name == "transfer":
# The custom transfer course content task
return transfer_course_content
else:
raise "Task not supported"


class Command(BaseCommand):
"""
This command has 2 execution modes, one for exporting course content and another for transferring course content.
It dispatch celery tasks that or export to a tar.gz file or transfer course content and upload it to the course 'GRADES_DOWNLOAD' storage.
It dispatch celery tasks that or export to a tar.gz file or transfer course content and upload it to the course
'GRADES_DOWNLOAD' storage.
"""

def add_arguments(self, parser):
parser.add_argument("--username", type=str, help="The username of the user to export the course content")
parser.add_argument("--task", default='export', nargs='?', choices=['export', 'transfer'], help="Task to execute: export or transfer")
parser.add_argument("--index", type=int, default=0, help="Start index of the course ids to begin exporting")
parser.add_argument("course_ids", nargs="*", metavar="course_id", default=None, help="Course ids to export or if omitted, all courses will be exported")
parser.add_argument(
"--username",
type=str,
help="The username of the user to export the course content",
)
parser.add_argument(
"--task",
default="export",
nargs="?",
choices=["export", "transfer"],
help="Task to execute: export or transfer",
)
parser.add_argument(
"--index",
type=int,
default=0,
help="Start index of the course ids to begin exporting",
)
parser.add_argument(
"course_ids",
nargs="*",
metavar="course_id",
default=None,
help="Course ids to export or if omitted, all courses will be exported",
)

def log_msg(self, msg):
self.stdout.write(msg)
Expand Down Expand Up @@ -85,7 +112,9 @@ def handle(self, *args, **options):
course_celery_task_dict = {}
for index in range(start_index, courses_count):
course_id = course_ids[index]
self.log_msg(f"Enqueue task {task_name} - {index+1} of {courses_count} - {course_id}")
self.log_msg(
f"Enqueue task {task_name} - {index+1} of {courses_count} - {course_id}"
)
task = task_func.delay(user_id, course_id, "en")
course_celery_task_dict[course_id] = task

Expand All @@ -97,13 +126,18 @@ def handle(self, *args, **options):
while not celery_task.ready():
self.log_msg(f"Waiting for task {celery_task} to complete...")
sleep(5)
self.log_msg(f"Task {celery_task} for course {course_id} has finished with {'success' if celery_task.successful() else 'failure'}")
self.log_msg(
f"Task {celery_task} for course {course_id} has finished with "
f"{'success' if celery_task.successful() else 'failure'}"
)
if task.successful():
successfull_courses.append(course_id)
else:
failed_courses.append(course_id)

self.log_msg(f"Tasks completed, successful count: {len(successfull_courses)}, failed, {len(failed_courses)}")

self.log_msg(
f"Tasks completed, successful count: {len(successfull_courses)}, failed, {len(failed_courses)}"
)

for failed_course in failed_courses:
self.log_msg(f"Failed course: {failed_course}")
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@
Export all courses:
python manage.py cms export_course_content_async --username <my_username>
"""

from cms.djangoapps.contentstore.tasks import export_olx # lint-amnesty, pylint: disable=import-error
from django.conf import settings
from django.contrib.auth import get_user_model
Expand All @@ -33,9 +34,24 @@ class Command(BaseCommand):
"""

def add_arguments(self, parser):
parser.add_argument("--username", type=str, help="The username of the user to export the course content")
parser.add_argument("--index", type=int, default=0, help="Start index of the course ids to begin exporting")
parser.add_argument("course_ids", nargs="*", metavar="course_id", default=None, help="Course ids to export or if omitted, all courses will be exported")
parser.add_argument(
"--username",
type=str,
help="The username of the user to export the course content",
)
parser.add_argument(
"--index",
type=int,
default=0,
help="Start index of the course ids to begin exporting",
)
parser.add_argument(
"course_ids",
nargs="*",
metavar="course_id",
default=None,
help="Course ids to export or if omitted, all courses will be exported",
)

def log_msg(self, msg):
self.stdout.write(msg)
Expand All @@ -60,7 +76,9 @@ def handle(self, *args, **options):
courses_count = len(course_ids)
for index in range(start_index, courses_count):
course_id = course_ids[index]
self.log_msg(f"Exporting {index+1} of {courses_count} - exporting {course_id}")
self.log_msg(
f"Exporting {index+1} of {courses_count} - exporting {course_id}"
)
try:
course_key = CourseKey.from_string(course_id)

Expand All @@ -69,15 +87,14 @@ def handle(self, *args, **options):
cms_root_url = SiteConfiguration.get_value_for_org(
course_key.org, "CMS_ROOT_URL", settings.CMS_ROOT_URL
)
to_download_url = (
f"{cms_root_url}/export/{course_id}"
)
to_download_url = f"{cms_root_url}/export/{course_id}"
self.log_msg(
f"You can confirm the existence of the file on: {to_download_url}"
)
except Exception as e:
self.log_msg(f"Error exporting course {course_id}: {e}")
# print stacktrace and continue
import traceback

self.log_msg(traceback.format_exc())
continue
Loading

0 comments on commit c7b3b8e

Please sign in to comment.