Skip to content

Commit

Permalink
Merge pull request #518 from edx/ziafazal/WL-1946
Browse files Browse the repository at this point in the history
WL-1946: Added ability to skip keys if their value is None for content exporter
  • Loading branch information
ziafazal authored Jul 9, 2019
2 parents d1477f5 + f6609a0 commit f139540
Show file tree
Hide file tree
Showing 5 changed files with 43 additions and 4 deletions.
5 changes: 5 additions & 0 deletions CHANGELOG.rst
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,11 @@ Change Log
Unreleased
----------

[1.6.19] - 2019-07-09
---------------------

* Added ability to skip keys if their value is None for content exporter

[1.6.18] - 2019-06-24
---------------------

Expand Down
2 changes: 1 addition & 1 deletion enterprise/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,6 @@

from __future__ import absolute_import, unicode_literals

__version__ = "1.6.18"
__version__ = "1.6.19"

default_app_config = "enterprise.apps.EnterpriseConfig" # pylint: disable=invalid-name
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class CornerstoneContentMetadataExporter(ContentMetadataExporter): # pylint: di
'Languages': 'languages',
'Subjects': 'subjects',
}
SKIP_KEY_IF_NONE = True

def transform_organizations(self, content_metadata_item):
"""
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,7 @@ class ContentMetadataExporter(Exporter):
#
# TODO: Move this to the EnterpriseCustomerPluginConfiguration model as a JSONField.
DATA_TRANSFORM_MAPPING = {}
SKIP_KEY_IF_NONE = False

def __init__(self, user, enterprise_configuration):
"""
Expand Down Expand Up @@ -116,12 +117,12 @@ def _transform_item(self, content_metadata_item):
)
)
if transformer:
transformed_item[integrated_channel_schema_key] = transformer(content_metadata_item)
transformed_value = transformer(content_metadata_item)
else:
# The concrete subclass does not define an override for the given field,
# so just use the data key to index the content metadata item dictionary.
try:
transformed_item[integrated_channel_schema_key] = content_metadata_item[edx_data_schema_key]
transformed_value = content_metadata_item[edx_data_schema_key]
except KeyError:
# There may be a problem with the DATA_TRANSFORM_MAPPING on
# the concrete subclass or the concrete subclass does not implement
Expand All @@ -132,6 +133,12 @@ def _transform_item(self, content_metadata_item):
self.enterprise_customer.name,
content_metadata_item,
)
continue

if transformed_value is None and self.SKIP_KEY_IF_NONE:
continue
else:
transformed_item[integrated_channel_schema_key] = transformed_value

return transformed_item

Expand Down
28 changes: 27 additions & 1 deletion tests/test_integrated_channels/test_cornerstone/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,6 +58,32 @@ def test_course_list_invalid_ciid(self):
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_404_NOT_FOUND)

@responses.activate
def test_course_list_with_skip_key_if_none_false(self):
"""
Test courses list view produces desired json when SKIP_KEY_IF_NONE is set to False
"""
url = '{path}?ciid={customer_uuid}'.format(
path=self.course_list_url,
customer_uuid=self.enterprise_customer_catalog.enterprise_customer.uuid
)
with mock.patch(
'integrated_channels.cornerstone.models.CornerstoneContentMetadataExporter.SKIP_KEY_IF_NONE',
new_callable=mock.PropertyMock

) as mock_skip_key_if_none:
mock_skip_key_if_none.return_value = False
response = self.client.get(url)
self.assertEqual(response.status_code, status.HTTP_200_OK)
self.assertEqual(len(response.data), 3)
keys = set([key for item in response.data for key in item.keys()])
expected_keys = [
"ID", "URL", "IsActive", "LastModifiedUTC", "Title", "Description",
"Thumbnail", "Duration", "Owners", "Languages", "Subjects",
]
for key in expected_keys:
self.assertIn(key, keys)

@responses.activate
def test_course_list(self):
"""
Expand All @@ -73,7 +99,7 @@ def test_course_list(self):
keys = set([key for item in response.data for key in item.keys()])
expected_keys = [
"ID", "URL", "IsActive", "LastModifiedUTC", "Title", "Description",
"Thumbnail", "Duration", "Owners", "Languages", "Subjects",
"Thumbnail", "Owners", "Languages", "Subjects",
]
for key in expected_keys:
self.assertIn(key, keys)

0 comments on commit f139540

Please sign in to comment.