Skip to content

Commit

Permalink
feat: update get_draft_flag method and update tests with log capture
Browse files Browse the repository at this point in the history
  • Loading branch information
AfaqShuaib09 committed Nov 15, 2023
1 parent b5fd893 commit 96b9a1f
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 11 deletions.
14 changes: 10 additions & 4 deletions course_discovery/apps/course_metadata/data_loaders/csv_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -213,7 +213,7 @@ def ingest(self): # pylint: disable=too-many-statements
if not is_course_created:
self.add_product_source(course)

is_draft = self.get_draft_flag(course_run)
is_draft = self.get_draft_flag(course_runs=CourseRun.objects.filter_drafts(course=course))
logger.info(f"Draft flag is set to {is_draft} for the course {course_title}")

try:
Expand Down Expand Up @@ -461,15 +461,20 @@ def _create_course_run_api_request_data(self, data, course, course_type, course_
})
return course_run_creation_fields

def get_draft_flag(self, course_run):
def get_draft_flag(self, course_runs):
"""
To keep behavior of CSV loader consistent with publisher, draft flag is false only when:
1. Course run is moved from Unpublished -> Review State
2. Any of the Course run is in published state
No 1 is not applicable at the moment. For 2, CSV loader right now only expects
one course run for each course, hence the status of the single fetched course run is checked.
"""
return not course_run.status == CourseRunStatus.Published
draft_flag = True
for course_run in course_runs:
if course_run.status == CourseRunStatus.Published:
draft_flag = False
break
return draft_flag

def _archive_stale_products(self, course_external_identifiers):
"""
Expand Down Expand Up @@ -652,7 +657,8 @@ def _create_course_run(self, data, course, course_type, course_run_type_uuid, re
"""
course_run_api_url = reverse('api:v1:course_run-list')
url = f"{settings.DISCOVERY_BASE_URL}{course_run_api_url}"
data.update({'rerun': rerun})
if rerun:
data.update({'rerun': rerun})
request_data = self._create_course_run_api_request_data(data, course, course_type, course_run_type_uuid)
response = self._call_course_api('POST', url, request_data)
if response.status_code not in (200, 201):
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -442,7 +442,7 @@ def test_ingest_flow_for_preexisting_published_course_with_new_run_creation(self
"""
self._setup_prerequisites(self.partner)
self.mock_studio_calls(self.partner)
studio_url = '{root}/api/v1/course_runs/'.format(root=self.partner.studio_url.strip('/'));
studio_url = '{root}/api/v1/course_runs/'.format(root=self.partner.studio_url.strip('/'))
responses.add(responses.POST, f'{studio_url}{self.COURSE_RUN_KEY}/rerun/', status=200)
self.mock_studio_calls(self.partner, run_key='course-v1:edx+csv_123+1T2020a')
self.mock_ecommerce_publication(self.partner)
Expand Down Expand Up @@ -484,13 +484,31 @@ def test_ingest_flow_for_preexisting_published_course_with_new_run_creation(self
LOGGER_PATH,
'INFO',
'Course edx+csv_123 is located in the database.'
)
),
(
LOGGER_PATH,
'INFO',
'Course Run with variant_id {variant_id} isn\'t found'.format(
variant_id='00000000-0000-0000-0000-000000000000'
)
),
(
LOGGER_PATH,
'INFO',
'Creating new course run for course {course_key} with variant_id {variant_id}'
.format(course_key=self.COURSE_KEY, variant_id='00000000-0000-0000-0000-000000000000')
),
(
LOGGER_PATH,
'INFO',
'Draft flag is set to False for the course CSV Course'
),
)

assert Course.everything.count() == 1
assert CourseRun.everything.count() == 2
course = Course.everything.get(key=self.COURSE_KEY, partner=self.partner)
assert Course.everything.count() == 2
assert CourseRun.everything.count() == 3

course = Course.objects.filter_drafts(key=self.COURSE_KEY, partner=self.partner).first()
course_run = CourseRun.everything.get(
course=course,
variant_id='00000000-0000-0000-0000-000000000000'
Expand All @@ -507,7 +525,7 @@ def test_ingest_flow_for_preexisting_published_course_with_new_run_creation(self
'failure_count': 0,
'updated_products_count': 0,
'created_products_count': 1,
'created_products': [{
'created_products': [{
'uuid': str(course.uuid),
'external_course_marketing_type':
course.additional_metadata.external_course_marketing_type,
Expand Down

0 comments on commit 96b9a1f

Please sign in to comment.