diff --git a/app/logic/__init__.py b/app/logic/__init__.py index 2c27c83..4c4eab3 100644 --- a/app/logic/__init__.py +++ b/app/logic/__init__.py @@ -31,6 +31,7 @@ def get_package(publisher, package): return None descriptor = metadata.get('descriptor') + descriptor = validate_for_template(descriptor) readme = metadata.get('readme') descriptor['owner'] = publisher readme_variables_replaced = dp_in_readme(readme, descriptor) @@ -218,3 +219,25 @@ def generate_signed_url(): props=filedata[relative_path]) res_payload['filedata'][relative_path] = response.build_file_information() return res_payload + + +#### helpers + +def validate_for_template(descriptor): + ''' + Validates field types in the descriptor for template, e.g. licenses property should be a list. + ''' + licenses = descriptor.get('licenses') + + if licenses is None or type(licenses) is list: + return descriptor + + if type(licenses) is dict: + license = descriptor.pop('licenses') + license = license.get('type') + descriptor['license'] = license + return descriptor + + descriptor.pop('licenses') + + return descriptor diff --git a/app/templates/_snippets.html b/app/templates/_snippets.html index db3d53c..0cd9103 100644 --- a/app/templates/_snippets.html +++ b/app/templates/_snippets.html @@ -69,6 +69,12 @@

KEY INFO


+ {% elif dataset.license %} +
+ {{ dataset.license }} +
+ +
{% endif %} diff --git a/tests/logic/test_logic.py b/tests/logic/test_logic.py index 58b66af..214f99a 100644 --- a/tests/logic/test_logic.py +++ b/tests/logic/test_logic.py @@ -175,3 +175,34 @@ def tearDown(self): db.session.remove() db.drop_all() db.engine.dispose() + +class HelpersTest(unittest.TestCase): + + + def setUp(self): + self.descriptor = json.loads(open('fixtures/datapackage.json').read()) + + + def test_validate_for_jinja_returns_descriptor_if_no_licenses(self): + descriptor = validate_for_template(self.descriptor) + self.assertEqual(self.descriptor, descriptor) + + def test_validate_for_jinja_returns_descriptor_if_licenses_is_list(self): + self.descriptor['licenses'] = [] + descriptor = validate_for_template(self.descriptor) + self.assertEqual(self.descriptor, descriptor) + + def test_validate_for_jinja_modifies_descriptor_if_licenses_is_dict(self): + self.descriptor['licenses'] = {'url': 'test/url', 'type': 'Test'} + descriptor = validate_for_template(self.descriptor) + self.assertEqual(descriptor['license'], 'Test') + + def test_validate_for_not_errors_if_licenses_is_dict_and_has_no_type_key(self): + self.descriptor['licenses'] = {'url': 'test/url'} + descriptor = validate_for_template(self.descriptor) + self.assertEqual(descriptor['license'], None) + + def test_validate_for_jinja_removes_licenses_if_invalid_type(self): + self.descriptor['licenses'] = 1 + descriptor = validate_for_template(self.descriptor) + self.assertEqual(descriptor.get('licenses'), None) diff --git a/tests/site/test_controller.py b/tests/site/test_controller.py index 8d68baf..dd7dc2b 100644 --- a/tests/site/test_controller.py +++ b/tests/site/test_controller.py @@ -83,6 +83,21 @@ def test_data_package_page(self): rv = self.client.get('/non-existing/demo-package') self.assertEqual(404, rv.status_code) + + def test_data_package_page_loads_if_descriptor_has_bad_licenses(self): + descriptor = json.loads(open('fixtures/datapackage.json').read()) + descriptor['licenses'] = {'url': 'test/url', 'type': 'Test'} + with self.app.app_context(): + publisher = Publisher(name=self.publisher) + metadata = Package(name=self.package) + metadata.tags.append(PackageTag(descriptor=descriptor)) + publisher.packages.append(metadata) + db.session.add(publisher) + db.session.commit() + rv = self.client.get('/%s/%s' %(self.publisher,self.package)) + self.assertEqual(200, rv.status_code) + + def test_data_package_page_load_without_views(self): descriptor = {"data": [], "resources": []} with self.app.app_context():