Commit 6fce2dad by Simon Chen

Provide the option to the command to create or not create canonical_course_run

EDCUATOR-676
parent 7ebb34b9
......@@ -10,7 +10,7 @@ from course_discovery.apps.publisher.models import Course, CourseRun, CourseRunS
logger = logging.getLogger(__name__)
def execute_query(start_id, end_id):
def execute_query(start_id, end_id, create_course_run):
""" Execute query according to the range."""
from course_discovery.apps.course_metadata.models import Course as CourseMetaData
......@@ -18,10 +18,10 @@ def execute_query(start_id, end_id):
for course in CourseMetaData.objects.select_related('canonical_course_run', 'level_type', 'video').filter(
id__range=(start_id, end_id)):
process_course(course)
process_course(course, create_course_run)
def process_course(meta_data_course):
def process_course(meta_data_course, create_course_run):
""" Create or update the course."""
# if course has more than 1 organization don't import that course. Just log the entry.
......@@ -34,13 +34,13 @@ def process_course(meta_data_course):
if not available_organization:
return
create_or_update_course(meta_data_course, available_organization)
create_or_update_course(meta_data_course, available_organization, create_course_run)
except: # pylint: disable=bare-except
logger.exception('Exception appear for course-id [%s].', meta_data_course.uuid)
def create_or_update_course(meta_data_course, available_organization):
def create_or_update_course(meta_data_course, available_organization, create_course_run):
primary_subject = None
secondary_subject = None
......@@ -89,8 +89,9 @@ def create_or_update_course(meta_data_course, available_organization):
logger.info('Import course with id [%s], number [%s].', publisher_course.id, publisher_course.number)
# create canonical course-run against the course.
create_course_runs(meta_data_course, publisher_course)
if create_course_run:
# create canonical course-run against the course.
create_course_runs(meta_data_course, publisher_course)
def transfer_course_image(meta_data_course, publisher_course):
......
......@@ -531,6 +531,11 @@ class CourseRunAdminForm(forms.ModelForm):
class AdminImportCourseForm(forms.Form):
start_id = forms.IntegerField(min_value=1, label='This course id will import.')
create_course_run = forms.BooleanField(
label=_('Create initial run for the course'),
widget=forms.CheckboxInput,
required=False
)
class Meta:
fields = ('start_id',)
......
......@@ -30,9 +30,19 @@ class Command(BaseCommand):
help='To this id courses will be imported.'
)
parser.add_argument(
'--create_run',
action='store',
dest='create_course_run',
default=False,
required=False,
help='Whether this script should create the initial course run'
)
def handle(self, *args, **options):
""" Import the course according to the given range."""
start_id = options.get('start_id')
end_id = options.get('end_id')
create_course_run = bool(options.get('create_course_run'))
execute_query(start_id, end_id)
execute_query(start_id, end_id, create_course_run)
......@@ -70,7 +70,15 @@ class ImportCoursesTests(TestCase):
def test_query_return_correct_course(self, process_course):
""" Verify that query return correct courses using start and end ids. """
call_command(self.command_name, *self.command_args)
call_list = [mock.call(self.course), ]
call_list = [mock.call(self.course, False), ]
self.assertEqual(call_list, process_course.call_args_list)
@mock.patch('course_discovery.apps.publisher.dataloader.create_courses.process_course')
def test_query_return_correct_course_with_run(self, process_course):
""" Verify that query return correct courses using start and end ids. """
self.command_args.append('--create_run={}'.format(True))
call_command(self.command_name, *self.command_args)
call_list = [mock.call(self.course, True), ]
self.assertEqual(call_list, process_course.call_args_list)
@mock.patch('course_discovery.apps.publisher.dataloader.create_courses.process_course')
......@@ -78,7 +86,7 @@ class ImportCoursesTests(TestCase):
""" Verify that query return correct courses using start and end ids. """
course_3 = CourseFactory()
call_command(self.command_name, *['--start_id={}'.format(self.course_2.id), '--end_id={}'.format(course_3.id)])
call_list = [mock.call(self.course_2), mock.call(course_3), ]
call_list = [mock.call(self.course_2, False), mock.call(course_3, False), ]
self.assertEqual(call_list, process_course.call_args_list)
@mock.patch('course_discovery.apps.publisher.dataloader.create_courses.create_or_update_course')
......@@ -137,8 +145,10 @@ class CreateCoursesTests(TestCase):
self.organization = self.forganization_extension.organization
self.course.authoring_organizations.add(self.organization)
def test_course_create_successfully(self):
""" Verify that publisher course successfully."""
def test_course_run_created_successfully(self):
""" Verify that publisher course and course_runs successfully."""
self.command_args.append('--create_run={}'.format(True))
call_command(self.command_name, *self.command_args)
course = Publisher_Course.objects.all().first()
......@@ -146,10 +156,18 @@ class CreateCoursesTests(TestCase):
self._assert_course_run(course.course_runs.first(), self.course.canonical_course_run)
self._assert_seats(course.course_runs.first(), self.course.canonical_course_run)
def test_course_create_successfully(self):
""" Verify that publisher course successfully."""
call_command(self.command_name, *self.command_args)
course = Publisher_Course.objects.all().first()
self._assert_course(course)
def test_course_create_without_video(self):
""" Verify that publisher course successfully."""
self.course.video = None
self.course.save()
self.command_args.append('--create_run={}'.format(True))
call_command(self.command_name, *self.command_args)
course = Publisher_Course.objects.all().first()
......@@ -176,6 +194,7 @@ class CreateCoursesTests(TestCase):
""" Verify that course does not create two course with same title and number.
Just update.
"""
self.command_args.append('--create_run={}'.format(True))
call_command(self.command_name, *self.command_args)
self.assertEqual(Publisher_Course.objects.all().count(), 1)
course = Publisher_Course.objects.all().first()
......@@ -194,6 +213,7 @@ class CreateCoursesTests(TestCase):
def test_course_without_canonical_course_run(self):
""" Verify that import works fine even if course has no canonical-course-run."""
self.command_args.append('--create_run={}'.format(True))
self.course.canonical_course_run = None
self.course.save()
......@@ -264,6 +284,7 @@ class CreateCoursesTests(TestCase):
def test_course_run_without_seats(self):
""" Verify that import works fine even if course-run has no seats."""
self.course.canonical_course_run.seats.all().delete()
self.command_args.append('--create_run={}'.format(True))
with LogCapture(dataloader_logger.name) as log_capture:
call_command(self.command_name, *self.command_args)
......@@ -309,8 +330,6 @@ class CreateCoursesTests(TestCase):
self.assertEqual(publisher_course.full_description, self.course.full_description)
self.assertEqual(publisher_course.level_type, self.course.level_type)
# each course will have only 1 course-run
self.assertEqual(publisher_course.course_runs.all().count(), 1)
self.assertEqual(publisher_course.course_metadata_pk, self.course.pk)
self.assertEqual(publisher_course.primary_subject, self.subjects[0])
self.assertEqual(publisher_course.secondary_subject, self.subjects[1])
......
......@@ -1140,12 +1140,13 @@ class AdminImportCourse(mixins.LoginRequiredMixin, TemplateView):
if form.is_valid():
start_id = self.request.POST.get('start_id')
create_course_run = self.request.POST.get('create_course_run')
try:
course = CourseMetaData.objects.select_related('canonical_course_run', 'level_type', 'video').get(
id=start_id
)
process_course(course)
process_course(course, create_course_run)
# check publisher db that course is available now.
publisher_course = Course.objects.filter(course_metadata_pk=start_id)
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-11-30 13:11+0000\n"
"POT-Creation-Date: 2017-12-11 20:22+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......@@ -831,6 +831,10 @@ msgstr ""
msgid "Invalid course key."
msgstr ""
#: apps/publisher/forms.py
msgid "Create initial run for the course"
msgstr ""
#: apps/publisher/models.py
msgid "Course title"
msgstr ""
......@@ -2728,6 +2732,8 @@ msgstr ""
#: apps/publisher/templates/publisher/email/course/seo_review.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.txt
#: apps/publisher/templates/publisher/email/course_run/preview_available.html
#: apps/publisher/templates/publisher/email/course_run/preview_available.txt
#: apps/publisher/templates/publisher/email/course_run/published.txt
......@@ -2774,6 +2780,8 @@ msgstr ""
#: apps/publisher/templates/publisher/email/course_created.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.txt
#: apps/publisher/templates/publisher/email/course_run/preview_accepted.html
#: apps/publisher/templates/publisher/email/course_run/preview_accepted.txt
#: apps/publisher/templates/publisher/email/course_run/preview_available.html
......@@ -2793,6 +2801,7 @@ msgstr ""
#: apps/publisher/templates/publisher/email/course/send_for_review.html
#: apps/publisher/templates/publisher/email/course/seo_review.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.html
#: apps/publisher/templates/publisher/email/course_run/preview_accepted.html
#: apps/publisher/templates/publisher/email/course_run/published.html
#: apps/publisher/templates/publisher/email/course_run/published_course_run_editing.html
......@@ -2808,6 +2817,7 @@ msgstr ""
#: apps/publisher/templates/publisher/email/course/send_for_review.txt
#: apps/publisher/templates/publisher/email/course/seo_review.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.txt
#: apps/publisher/templates/publisher/email/course_run/preview_accepted.txt
#: apps/publisher/templates/publisher/email/course_run/preview_available.txt
#: apps/publisher/templates/publisher/email/course_run/published.txt
......@@ -2877,6 +2887,7 @@ msgid ""
msgstr ""
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.html
#, python-format
msgid ""
"%(sender_team)s has reviewed the "
......@@ -2894,12 +2905,15 @@ msgstr ""
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.txt
msgid ""
"Additionally, please check the comments in Publisher for information about "
"OFAC blocking."
msgstr ""
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.txt
#, python-format
msgid ""
"The %(sender_team)s has reviewed the %(run_number)s %(page_url)s course run "
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-11-30 13:11+0000\n"
"POT-Creation-Date: 2017-12-11 20:22+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-11-30 13:11+0000\n"
"POT-Creation-Date: 2017-12-11 20:22+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......@@ -994,6 +994,11 @@ msgstr "Thïs fïéld ïs réqüïréd. Ⱡ'σяєм ιρѕυм ∂σłσя ѕι
msgid "Invalid course key."
msgstr "Ìnvälïd çöürsé kéý. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт,#"
#: apps/publisher/forms.py
msgid "Create initial run for the course"
msgstr ""
"Çréäté ïnïtïäl rün för thé çöürsé Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕє¢тє#"
#: apps/publisher/models.py
msgid "Course title"
msgstr "Çöürsé tïtlé Ⱡ'σяєм ιρѕυм ∂σłσя ѕ#"
......@@ -3225,6 +3230,8 @@ msgstr ""
#: apps/publisher/templates/publisher/email/course/seo_review.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.txt
#: apps/publisher/templates/publisher/email/course_run/preview_available.html
#: apps/publisher/templates/publisher/email/course_run/preview_available.txt
#: apps/publisher/templates/publisher/email/course_run/published.txt
......@@ -3282,6 +3289,8 @@ msgstr ""
#: apps/publisher/templates/publisher/email/course_created.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.txt
#: apps/publisher/templates/publisher/email/course_run/preview_accepted.html
#: apps/publisher/templates/publisher/email/course_run/preview_accepted.txt
#: apps/publisher/templates/publisher/email/course_run/preview_available.html
......@@ -3301,6 +3310,7 @@ msgstr "Thänks, Ⱡ'σяєм ιρѕυм #"
#: apps/publisher/templates/publisher/email/course/send_for_review.html
#: apps/publisher/templates/publisher/email/course/seo_review.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.html
#: apps/publisher/templates/publisher/email/course_run/preview_accepted.html
#: apps/publisher/templates/publisher/email/course_run/published.html
#: apps/publisher/templates/publisher/email/course_run/published_course_run_editing.html
......@@ -3318,6 +3328,7 @@ msgstr ""
#: apps/publisher/templates/publisher/email/course/send_for_review.txt
#: apps/publisher/templates/publisher/email/course/seo_review.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.txt
#: apps/publisher/templates/publisher/email/course_run/preview_accepted.txt
#: apps/publisher/templates/publisher/email/course_run/preview_available.txt
#: apps/publisher/templates/publisher/email/course_run/published.txt
......@@ -3414,6 +3425,7 @@ msgstr ""
"∂σłσя ѕιт αмєт, ¢σηѕє¢тєтυя#"
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.html
#, python-format
msgid ""
"%(sender_team)s has reviewed the "
......@@ -3443,6 +3455,8 @@ msgstr ""
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.html
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.txt
msgid ""
"Additionally, please check the comments in Publisher for information about "
"OFAC blocking."
......@@ -3451,6 +3465,7 @@ msgstr ""
"ÖFÀÇ ßlöçkïng. Ⱡ'σяєм ιρѕυм ∂σłσя ѕιт αмєт, ¢σηѕ#"
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed.txt
#: apps/publisher/templates/publisher/email/course_run/mark_as_reviewed_pc.txt
#, python-format
msgid ""
"The %(sender_team)s has reviewed the %(run_number)s %(page_url)s course run "
......
......@@ -7,7 +7,7 @@ msgid ""
msgstr ""
"Project-Id-Version: PACKAGE VERSION\n"
"Report-Msgid-Bugs-To: \n"
"POT-Creation-Date: 2017-11-30 13:11+0000\n"
"POT-Creation-Date: 2017-12-11 20:22+0000\n"
"PO-Revision-Date: YEAR-MO-DA HO:MI+ZONE\n"
"Last-Translator: FULL NAME <EMAIL@ADDRESS>\n"
"Language-Team: LANGUAGE <LL@li.org>\n"
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment