From 2f93e3bd6c6389fb7d5f020255106d45eba468ce Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Agusti=CC=81n=20Benassi?= Date: Mon, 23 Apr 2018 12:02:28 -0300 Subject: [PATCH] =?UTF-8?q?Implemento=20fallback=20que=20busca=20un=20them?= =?UTF-8?q?e=20por=20identifier=20primero=20o=20por=20label=20despu=C3=A9s?= =?UTF-8?q?=20(si=20falla),=20previendo=20las=20fallas=20de=20algunos=20An?= =?UTF-8?q?dinos=20que=20muestran=20el=20label=20en=20lugar=20del=20id=20d?= =?UTF-8?q?e=20un=20tema=20en=20el=20data.json.=20Closes=20#148?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- pydatajson/ckan_utils.py | 39 +++++++++++++++++++++++++++------------ 1 file changed, 27 insertions(+), 12 deletions(-) diff --git a/pydatajson/ckan_utils.py b/pydatajson/ckan_utils.py index 043130e..6025210 100644 --- a/pydatajson/ckan_utils.py +++ b/pydatajson/ckan_utils.py @@ -19,9 +19,13 @@ def map_dataset_to_package(catalog, dataset, owner_org, catalog_id=None, demote_superThemes=True, demote_themes=True): package = dict() package['extras'] = [] -# Obligatorios - package['id'] = catalog_id+'_'+dataset['identifier'] if catalog_id else dataset['identifier'] - package['name'] = title_to_name(catalog_id+'-'+dataset['title'] if catalog_id else dataset['title']) + + # Obligatorios + package['id'] = catalog_id + '_' + \ + dataset['identifier'] if catalog_id else dataset['identifier'] + package['name'] = title_to_name( + catalog_id + '-' + dataset['title'] if catalog_id else dataset['title'] + ) package['title'] = dataset['title'] package['private'] = False package['notes'] = dataset['description'] @@ -32,15 +36,18 @@ def map_dataset_to_package(catalog, dataset, owner_org, catalog_id=None, append_attribute_to_extra(package, dataset, 'accrualPeriodicity') distributions = dataset['distribution'] - package['resources'] = map_distributions_to_resources(distributions, catalog_id) + package['resources'] = map_distributions_to_resources( + distributions, catalog_id) super_themes = dataset['superTheme'] append_attribute_to_extra(package, dataset, 'superTheme', serialize=True) if demote_superThemes: - package['groups'] = [{'name': title_to_name(super_theme, decode=False)} for super_theme in super_themes] - + package['groups'] = [ + {'name': title_to_name(super_theme, decode=False)} + for super_theme in super_themes + ] -# Recomendados y opcionales + # Recomendados y opcionales package['url'] = dataset.get('landingPage') package['author_email'] = dataset['publisher'].get('mbox') append_attribute_to_extra(package, dataset, 'modified') @@ -67,12 +74,17 @@ def map_dataset_to_package(catalog, dataset, owner_org, catalog_id=None, if themes and demote_themes: package['tags'] = package.get('tags', []) for theme in themes: - label = catalog.get_theme(identifier=theme)['label'] + try: + label = catalog.get_theme(identifier=theme)['label'] + except: + label = catalog.get_theme(label=theme)['label'] label = re.sub(r'[^\wá-úÁ-ÚñÑ .-]+', '', label, flags=re.UNICODE) package['tags'].append({'name': label}) else: - package['groups'] = package.get('groups', []) + [{'name': title_to_name(theme, decode=False)} - for theme in themes] + package['groups'] = package.get('groups', []) + [ + {'name': title_to_name(theme, decode=False)} + for theme in themes + ] return package @@ -95,7 +107,9 @@ def map_distributions_to_resources(distributions, catalog_id=None): for distribution in distributions: resource = dict() # Obligatorios - resource['id'] = catalog_id + '_' + distribution['identifier'] if catalog_id else distribution['identifier'] + resource['id'] = catalog_id + '_' + \ + distribution['identifier'] if catalog_id else distribution[ + 'identifier'] resource['name'] = distribution['title'] resource['url'] = distribution['downloadURL'] resource['created'] = convert_iso_string_to_utc(distribution['issued']) @@ -104,7 +118,8 @@ def map_distributions_to_resources(distributions, catalog_id=None): resource['format'] = distribution.get('format') last_modified = distribution.get('modified') if last_modified: - resource['last_modified'] = convert_iso_string_to_utc(last_modified) + resource['last_modified'] = convert_iso_string_to_utc( + last_modified) resource['mimetype'] = distribution.get('mediaType') resource['size'] = distribution.get('byteSize') resource['accessURL'] = distribution.get('accessURL')