Skip to content

Commit

Permalink
refactor: code validation
Browse files Browse the repository at this point in the history
  • Loading branch information
AfaqShuaib09 committed Jan 1, 2025
1 parent 93babef commit a32c714
Showing 1 changed file with 36 additions and 13 deletions.
49 changes: 36 additions & 13 deletions course_discovery/apps/course_metadata/data_loaders/csv_loader.py
Original file line number Diff line number Diff line change
Expand Up @@ -140,21 +140,24 @@ def ingest(self): # pylint: disable=too-many-statements
course_external_identifiers.add(row['external_identifier'])

logger.info('Starting data import flow for {}'.format(course_title)) # lint-amnesty, pylint: disable=logging-format-interpolation
if not self._validate_organization(org_key, course_title, self.org_cache):
continue
is_valid, course_type, course_run_type = self._validate_course_and_course_run_types(row, course_title)
is_valid, course_type, course_run_type = self._validate_and_process_row(row, course_title, org_key)
if not is_valid:
continue

missing_fields = self.validate_course_data(course_type, row)
if missing_fields:
self._log_and_continue(
CSVIngestionErrors.MISSING_REQUIRED_DATA,
CSVIngestionErrorMessages.MISSING_REQUIRED_DATA.format(
course_title=course_title, missing_data=missing_fields
),
)
continue
# if not self._validate_organization(org_key, course_title, self.org_cache):
# continue
# is_valid, course_type, course_run_type = self._validate_course_and_course_run_types(row, course_title)
# if not is_valid:
# continue

# missing_fields = self.validate_course_data(course_type, row)
# if missing_fields:
# self._log_and_continue(
# CSVIngestionErrors.MISSING_REQUIRED_DATA,
# CSVIngestionErrorMessages.MISSING_REQUIRED_DATA.format(
# course_title=course_title, missing_data=missing_fields
# ),
# )
# continue

course_key = self.get_course_key(org_key, row['number'])
course = Course.objects.filter_drafts(key=course_key, partner=self.partner).select_related('type').first()
Expand Down Expand Up @@ -343,6 +346,26 @@ def get_course_run_type(self, course_run_type_name):
except CourseRunType.DoesNotExist:
self.course_run_type_cache[course_run_type_name] = None
return self.course_run_type_cache[course_run_type_name]

def _validate_and_process_row(self, row, course_title, org_key):
if not self._validate_organization(org_key, course_title, self.org_cache):
return False, None, None

is_valid, course_type, course_run_type = self._validate_course_and_course_run_types(row, course_title)
if not is_valid:
return False, course_type, course_run_type

missing_fields = self.validate_course_data(course_type, row)
if missing_fields:
self._log_and_continue(
CSVIngestionErrors.MISSING_REQUIRED_DATA,
CSVIngestionErrorMessages.MISSING_REQUIRED_DATA.format(
course_title=course_title, missing_data=missing_fields
)
)
return False, course_type, course_run_type

return True, course_type, course_run_type

def _validate_course_and_course_run_types(self, row, course_title):
"""
Expand Down

0 comments on commit a32c714

Please sign in to comment.