Commit 70a95fcf by Clinton Blackburn Committed by Clinton Blackburn

Updated marketing site school loader

The UUID is now used as the primary identifier when refreshing schools. Usage of the title as the key poses issues with schools whose titles and keys differ on LMS (e.g. UC BerkeleyX vs. BerkeleyX). This change ensures that we can still pull changes to images and descriptions, while also supporting manual edits to keys.

EDUCATOR-1531
parent 10a2fcd7
......@@ -174,20 +174,36 @@ class SchoolMarketingSiteDataLoader(AbstractMarketingSiteDataLoader):
return 'school'
def process_node(self, data):
key = data['title']
# NOTE: Some titles in Drupal have the form "UC BerkeleyX" however, course keys (for which we use the
# organization key) cannot contain spaces.
key = data['title'].replace(' ', '')
uuid = UUID(data['uuid'])
defaults = {
'uuid': data['uuid'],
'name': data['field_school_name'],
'description': self.clean_html(data['field_school_description']['value']),
'logo_image_url': self._get_nested_url(data.get('field_school_image_logo')),
'banner_image_url': self._get_nested_url(data.get('field_school_image_banner')),
'marketing_url_path': 'school/' + data['field_school_url_slug'],
'partner': self.partner,
}
school, __ = Organization.objects.update_or_create(key=key, partner=self.partner, defaults=defaults)
try:
school = Organization.objects.get(uuid=uuid, partner=self.partner)
Organization.objects.filter(pk=school.pk).update(**defaults)
logger.info('Updated school with key [%s].', school.key)
except Organization.DoesNotExist:
# NOTE: Some organizations' keys do not match the title. For example, "UC BerkeleyX" courses use
# BerkeleyX as the key. Those fixes will be made manually after initial import, and we don't want to
# override them with subsequent imports. Thus, we only set the key when creating a new organization.
defaults['key'] = key
defaults['uuid'] = uuid
school = Organization.objects.create(**defaults)
logger.info('Created school with key [%s].', school.key)
self.set_tags(school, data)
logger.info('Processed school with key [%s].', key)
logger.info('Processed school with key [%s].', school.key)
return school
def set_tags(self, school, data):
......
......@@ -194,10 +194,9 @@ class SchoolMarketingSiteDataLoaderTests(AbstractMarketingSiteDataLoaderTestMixi
mocked_data = mock_data.MARKETING_SITE_API_SCHOOL_BODIES
def assert_school_loaded(self, data):
key = data['title']
school = Organization.objects.get(key=key, partner=self.partner)
school = Organization.objects.get(uuid=UUID(data['uuid']), partner=self.partner)
expected_values = {
'uuid': UUID(data['uuid']),
'key': data['title'],
'name': data['field_school_name'],
'description': self.loader.clean_html(data['field_school_description']['value']),
'logo_image_url': data['field_school_image_logo']['url'],
......@@ -220,6 +219,21 @@ class SchoolMarketingSiteDataLoaderTests(AbstractMarketingSiteDataLoaderTestMixi
for school in schools:
self.assert_school_loaded(school)
# If the key of an organization changes, the data loader should continue updating the
# organization by matching on the UUID.
school = Organization.objects.get(key='MITx', partner=self.partner)
# NOTE (CCB): As an MIT alum, this makes me feel dirty. IHTFT(est)!
modified_key = 'MassTechX'
school.key = modified_key
school.save()
count = Organization.objects.count()
self.loader.ingest()
school.refresh_from_db()
assert Organization.objects.count() == count
assert school.key == modified_key
class SponsorMarketingSiteDataLoaderTests(AbstractMarketingSiteDataLoaderTestMixin, TestCase):
loader_class = SponsorMarketingSiteDataLoader
......
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