Commit ec7a14e5 by Awais Committed by Awais Qureshi

Fixing import issue. Adding missing video field

Optimize the query..

ECOM-7844
parent e67e9096
......@@ -6,6 +6,17 @@ from course_discovery.apps.publisher.models import Course, CourseRun, CourseRunS
logger = logging.getLogger(__name__)
def execute_query(start_id, end_id):
""" Execute query according to the range."""
from course_discovery.apps.course_metadata.models import Course as CourseMetaData
for course in CourseMetaData.objects.select_related('canonical_course_run', 'level_type', 'video').filter(
id__range=(start_id, end_id)):
process_course(course)
def process_course(meta_data_course):
""" Create or update the course."""
......@@ -27,11 +38,26 @@ def process_course(meta_data_course):
def create_or_update_course(meta_data_course, available_organization):
primary_subject = None
secondary_subject = None
tertiary_subject = None
for i, subject in enumerate(meta_data_course.subjects.all()):
if i == 0:
primary_subject = subject
elif i == 1:
secondary_subject = subject
elif i == 2:
tertiary_subject = subject
defaults = {
'title': meta_data_course.title, 'number': meta_data_course.number,
'short_description': meta_data_course.short_description,
'full_description': meta_data_course.full_description,
'level_type': meta_data_course.level_type
'level_type': meta_data_course.level_type,
'primary_subject': primary_subject, 'secondary_subject': secondary_subject,
'tertiary_subject': tertiary_subject,
'video_link': meta_data_course.video.src if meta_data_course.video else None
}
publisher_course, created = Course.objects.update_or_create(
......@@ -39,16 +65,6 @@ def create_or_update_course(meta_data_course, available_organization):
defaults=defaults
)
for i, subject in enumerate(meta_data_course.subjects.all()):
if i == 0:
publisher_course.primary_subject = subject
elif i == 1:
publisher_course.secondary_subject = subject
elif i == 2:
publisher_course.tertiary_subject = subject
publisher_course.save()
if created:
if available_organization:
publisher_course.organizations.add(available_organization)
......
......@@ -2,8 +2,7 @@ import logging
from django.core.management import BaseCommand
from course_discovery.apps.course_metadata.models import Course
from course_discovery.apps.publisher.dataloader.create_courses import process_course
from course_discovery.apps.publisher.dataloader.create_courses import execute_query
logger = logging.getLogger(__name__)
......@@ -32,11 +31,8 @@ class Command(BaseCommand):
)
def handle(self, *args, **options):
""" Import the course according to the given range. But in prod for multiple runs there are multiple
courses. During import just pick the latest course and do not import the old ones.
"""
""" Import the course according to the given range."""
start_id = options.get('start_id')
end_id = options.get('end_id')
for course in Course.objects.filter(id__range=(start_id, end_id)):
process_course(course)
execute_query(start_id, end_id)
......@@ -5,7 +5,7 @@ from django.test import TestCase
from testfixtures import LogCapture
from course_discovery.apps.course_metadata.tests.factories import (CourseFactory, CourseRunFactory, OrganizationFactory,
PersonFactory, SeatFactory)
PersonFactory, SeatFactory, SubjectFactory)
from course_discovery.apps.ietf_language_tags.models import LanguageTag
from course_discovery.apps.publisher.dataloader.create_courses import logger as dataloader_logger
from course_discovery.apps.publisher.models import Course as Publisher_Course
......@@ -58,14 +58,14 @@ class ImportCoursesTests(TestCase):
self.command_name = 'import_metadata_courses'
self.command_args = ['--start_id={}'.format(self.course.id), '--end_id={}'.format(format(self.course.id))]
@mock.patch('course_discovery.apps.publisher.management.commands.import_metadata_courses.process_course')
@mock.patch('course_discovery.apps.publisher.dataloader.create_courses.process_course')
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), ]
self.assertEqual(call_list, process_course.call_args_list)
@mock.patch('course_discovery.apps.publisher.management.commands.import_metadata_courses.process_course')
@mock.patch('course_discovery.apps.publisher.dataloader.create_courses.process_course')
def test_query_return_correct_courses(self, process_course):
""" Verify that query return correct courses using start and end ids. """
course_3 = CourseFactory()
......@@ -116,7 +116,8 @@ class CreateCoursesTests(TestCase):
super(CreateCoursesTests, self).setUp()
transcript_languages = LanguageTag.objects.all()[:2]
self.course = CourseFactory()
self.subjects = SubjectFactory.create_batch(3)
self.course = CourseFactory(subjects=self.subjects)
self.command_name = 'import_metadata_courses'
self.command_args = ['--start_id={}'.format(self.course.id), '--end_id={}'.format(format(self.course.id))]
......@@ -143,7 +144,18 @@ class CreateCoursesTests(TestCase):
self.course.authoring_organizations.add(self.organization)
def test_course_create_successfully(self):
""" Verify that publisher course without default user roles and subjects."""
""" Verify that publisher course successfully."""
call_command(self.command_name, *self.command_args)
course = Publisher_Course.objects.all().first()
self._assert_course(course)
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_without_video(self):
""" Verify that publisher course successfully."""
self.course.video = None
self.course.save()
call_command(self.command_name, *self.command_args)
course = Publisher_Course.objects.all().first()
......@@ -151,8 +163,6 @@ class CreateCoursesTests(TestCase):
self._assert_course(course)
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)
self.assertFalse(course.course_user_roles.all())
self.assertFalse(self.course.subjects.all())
def test_course_does_not_create_twice(self):
""" Verify that course does not create two course with same title and number.
......@@ -237,6 +247,14 @@ class CreateCoursesTests(TestCase):
# 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])
self.assertEqual(publisher_course.tertiary_subject, self.subjects[2])
if self.course.video:
self.assertEqual(publisher_course.video_link, self.course.video.src)
else:
self.assertFalse(publisher_course.video_link)
def _assert_course_run(self, publisher_course_run, metadata_course_run):
""" Verify that publisher course-run and metadata course run has correct values."""
......
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