spatial-d4science/ckanext/spatial/helpers.py

59 lines
2.1 KiB
Python

import logging
from ckan import plugins as p
from ckan.lib import helpers as h
log = logging.getLogger(__name__)
def get_reference_date(date_str):
'''
Gets a reference date extra created by the harvesters and formats it
nicely for the UI.
Examples:
[{"type": "creation", "value": "1977"}, {"type": "revision", "value": "1981-05-15"}]
[{"type": "publication", "value": "1977"}]
[{"type": "publication", "value": "NaN-NaN-NaN"}]
Results
1977 (creation), May 15, 1981 (revision)
1977 (publication)
NaN-NaN-NaN (publication)
'''
try:
out = []
for date in h.json.loads(date_str):
value = h.render_datetime(date['value']) or date['value']
out.append('{0} ({1})'.format(value, date['type']))
return ', '.join(out)
except (ValueError, TypeError):
return date_str
def get_responsible_party(value):
'''
Gets a responsible party extra created by the harvesters and formats it
nicely for the UI.
Examples:
[{"name": "Complex Systems Research Center", "roles": ["pointOfContact"]}]
[{"name": "British Geological Survey", "roles": ["custodian", "pointOfContact"]}, {"name": "Natural England", "roles": ["publisher"]}]
Results
Complex Systems Research Center (pointOfContact)
British Geological Survey (custodian, pointOfContact); Natural England (publisher)
'''
formatted = {
'resourceProvider': p.toolkit._('Resource Provider'),
'pointOfContact': p.toolkit._('Point of Contact'),
'principalInvestigator': p.toolkit._('Principal Investigator'),
}
try:
out = []
parties = h.json.loads(value)
for party in parties:
roles = [formatted[role] if role in formatted.keys() else p.toolkit._(role.capitalize()) for role in party['roles']]
out.append('{0} ({1})'.format(party['name'], ', '.join(roles)))
return '; '.join(out)
except (ValueError, TypeError):
return value