Skip to content

Commit

Permalink
Implemento fallback que busca un theme por identifier primero o por l…
Browse files Browse the repository at this point in the history
…abel después (si falla), previendo las fallas de algunos Andinos que muestran el label en lugar del id de un tema en el data.json. Closes #148
  • Loading branch information
abenassi committed Apr 23, 2018
1 parent db1418e commit 2f93e3b
Showing 1 changed file with 27 additions and 12 deletions.
39 changes: 27 additions & 12 deletions pydatajson/ckan_utils.py
Expand Up @@ -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']
Expand All @@ -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')
Expand All @@ -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

Expand All @@ -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'])
Expand All @@ -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')
Expand Down

0 comments on commit 2f93e3b

Please sign in to comment.