def test_link_to(self): link = h.link_to("Example Link", "https://www.example.com", cls='css-class', target='_blank') assert link == '<a class="css-class" href="https://www.example.com" target="_blank">Example Link</a>' link2 = h.link_to('display_name', h.url_for('dataset.search', tags='name'), class_='tag') link2 == '<a class="tag" href="/dataset/?tags=name">display_name</a>'
def build_pages_nav_main(*args): about_menu = p.toolkit.asbool(config.get('ckanext.pages.about_menu', True)) group_menu = p.toolkit.asbool(config.get('ckanext.pages.group_menu', True)) org_menu = p.toolkit.asbool( config.get('ckanext.pages.organization_menu', True)) new_args = [] for arg in args: if arg[0] == 'about' and not about_menu: continue if arg[0] == 'organizations_index' and not org_menu: continue if arg[0] == 'group_index' and not group_menu: continue new_args.append(arg) output = h.build_nav_main(*new_args) # do not display any private datasets in menu even for sysadmins pages_list = p.toolkit.get_action('ckanext_pages_list')(None, { 'order': True, 'private': False }) page_name = '' if (p.toolkit.c.action in ('pages_show', 'blog_show') and p.toolkit.c.controller == 'ckanext.pages.controller:PagesController'): page_name = p.toolkit.c.environ['routes.url'].current().split('/')[-1] for page in pages_list: if page['page_type'] == 'blog': # cdc link = h.link_to(h.getLangLabel(page['ename'], page['cname']), h.url_for('/blog/' + str(page['name']))) else: # cdc link = h.link_to(h.getLangLabel(page['ename'], page['cname']), h.url_for('/pages/' + str(page['name']))) if page['name'] == page_name: li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = output + li return output
def get_images(self, raw_images, record, data_dict): images = [] title_field = data_dict['resource_view'].get('image_title', None) for image in raw_images: title = [] if title_field and title_field in record: title.append(record[title_field]) title.append(image.get('title', str(image['_id']))) copyright = '%s<br />© %s' % ( h.link_to(image['license'], image['license'], target='_blank'), image['rightsHolder'] ) images.append({ 'href': image['identifier'], 'thumbnail': image['identifier'].replace('preview', 'thumbnail'), 'link': h.url_for( controller='ckanext.nhm.controllers.record:RecordController', action='view', package_name=data_dict['package']['name'], resource_id=data_dict['resource']['id'], record_id=record['_id'] ), 'copyright': copyright, # Description of image in gallery view 'description': literal(''.join(['<span>%s</span>' % t for t in title])), 'title': ' - '.join(title), 'record_id': record['_id'] }) return images
def api_doc_link(): """ Link to API documentation @return: """ attr = {'class': 'external', 'target': '_blank'} return link_to(_('API guide'), 'http://docs.ckan.org/en/latest/api/index.html', **attr)
def get_images(self, raw_images, record, data_dict): images = [] try: image_json = json.loads(raw_images) except ValueError: pass else: for i in image_json: title = [] for title_field in ['scientificName', 'catalogNumber']: if record.get(title_field, None): title.append(record.get(title_field)) copyright = '%s<br />© %s' % ( h.link_to(i['license'], i['license'], target='_blank'), i['rightsHolder'] ) images.append({ 'href': i['identifier'], 'thumbnail': i['identifier'].replace('preview', 'thumbnail'), 'link': h.url_for( controller='ckanext.nhm.controllers.record:RecordController', action='view', package_name=data_dict['package']['name'], resource_id=data_dict['resource']['id'], record_id=record['_id'] ), 'copyright': copyright, # Description of image in gallery view 'description': literal(''.join(['<span>%s</span>' % t for t in title])), 'title': ' - '.join(title), 'record_id': record['_id'] }) return images
class PackageSaver(object): '''Use this to validate and save packages to the db. @param log_message: optional - only supply this if you want it validated @param author: optional - only supply this if you want it validated ''' # TODO: rename to something more correct like prepare_for_render @classmethod def render_package(cls, pkg, context): '''Prepares for rendering a package. Takes a Package object and formats it for the various context variables required to call render. Note that the actual calling of render('package/read') is left to the caller.''' try: notes_formatted = ckan.misc.MarkdownFormat().to_html( pkg.get('notes', '')) c.pkg_notes_formatted = genshi.HTML(notes_formatted) except Exception, e: error_msg = "<span class='inline-warning'>%s</span>" % _( "Cannot render package description") c.pkg_notes_formatted = genshi.HTML(error_msg) c.current_rating, c.num_ratings = ckan.rating.get_rating( context['package']) url = pkg.get('url', '') c.pkg_url_link = h.link_to(url, url, rel='foaf:homepage', target='_blank') \ if url else _("No web page given") if pkg.get('author_email', False): c.pkg_author_link = cls._person_email_link( pkg.get('author', ''), pkg.get('author_email', ''), "Author") else: c.pkg_author_link = _("Author not given") maintainer = pkg.get('maintainer', '') maintainer_email = pkg.get('maintainer_email', '') if maintainer_email: c.pkg_maintainer_link = cls._person_email_link( maintainer, maintainer_email, "Maintainer") else: c.pkg_maintainer_link = _("Maintainer not given") c.package_relationships = context[ 'package'].get_relationships_printable() c.pkg_extras = [] for extra in sorted(pkg.get('extras', []), key=lambda x: x['key']): if extra.get('state') == 'deleted': continue k, v = extra['key'], extra['value'] if k in g.package_hide_extras: continue if isinstance(v, (list, tuple)): v = ", ".join(map(unicode, v)) c.pkg_extras.append((k, v)) if context.get('revision_id') or context.get('revision_date'): # request was for a specific revision id or date c.pkg_revision_id = c.pkg_dict[u'revision_id'] c.pkg_revision_timestamp = c.pkg_dict[u'revision_timestamp'] c.pkg_revision_not_latest = c.pkg_dict[ u'revision_id'] != c.pkg.revision.id
def _tag_links(self): tags = self.value tag_links = [ h.link_to( tagname, h.url_for(controller='tag', action='read', id=tagname)) for tagname in tags ] return literal(' '.join(tag_links))
def build_pages_nav_main(*args): about_menu = p.toolkit.asbool(config.get('ckanext.pages.about_menu', True)) group_menu = p.toolkit.asbool(config.get('ckanext.pages.group_menu', True)) org_menu = p.toolkit.asbool(config.get('ckanext.pages.organization_menu', True)) new_args = [] for arg in args: if arg[0] == 'about' and not about_menu: continue if arg[0] == 'organizations_index' and not org_menu: continue if arg[0] == 'group_index' and not group_menu: continue new_args.append(arg) output = h.build_nav_main(*new_args) # do not display any private datasets in menu even for sysadmins pages_list = p.toolkit.get_action('ckanext_pages_list')(None, {'order': True, 'private': False}) page_name = '' if (p.toolkit.c.action in ('pages_show', 'blog_show') and p.toolkit.c.controller == 'ckanext.pages.controller:PagesController'): page_name = p.toolkit.c.environ['routes.url'].current().split('/')[-1] for page in pages_list: if page['page_type'] == 'blog': # cdc link = h.link_to( h.getLangLabel(page['ename'], page['cname']), h.url_for('/blog/' + str( page['name'] ))) else: # cdc link = h.link_to( h.getLangLabel(page['ename'], page['cname']), h.url_for('/pages/' + str( page['name'] ))) if page['name'] == page_name: li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = output + li return output
def render_package(cls, pkg, context): '''Prepares for rendering a package. Takes a Package object and formats it for the various context variables required to call render. Note that the actual calling of render('package/read') is left to the caller.''' c.pkg_notes_formatted = h.render_markdown(pkg.get('notes')) c.current_rating, c.num_ratings = ckan.rating.get_rating(context['package']) url = pkg.get('url', '') c.pkg_url_link = h.link_to(url, url, rel='foaf:homepage', target='_blank') \ if url else _("No web page given") c.pkg_author_link = cls._person_email_link( name=pkg.get('author'), email=pkg.get('author_email'), fallback=_("Author not given")) c.pkg_maintainer_link = cls._person_email_link( name=pkg.get('maintainer'), email=pkg.get('maintainer_email'), fallback=_("Maintainer not given")) c.package_relationships = context['package'].get_relationships_printable() c.pkg_extras = [] for extra in sorted(pkg.get('extras',[]), key=lambda x:x['key']): if extra.get('state') == 'deleted': continue k, v = extra['key'], extra['value'] if k in g.package_hide_extras: continue if isinstance(v, (list, tuple)): v = ", ".join(map(unicode, v)) c.pkg_extras.append((k, v)) #dportoles: duplicate and format markdown for if k == 'Data Dictionary': c.pkg_dataDictionary_formatted = h.render_markdown(v) if k == 'Data Quality': c.pkg_dataQuality_formatted = h.render_markdown(v) # lorena Extras IAEST c.pkg_extrasIAEST = [] for extra in sorted(pkg.get('extrasIAEST',[]), key=lambda x:x['key']): if extra.get('state') == 'deleted': continue k, v = extra['key'], extra['value'] if k in g.package_hide_extras: continue if isinstance(v, (list, tuple)): v = ", ".join(map(unicode, v)) c.pkg_extrasIAEST.append((k, v)) if context.get('revision_id') or context.get('revision_date'): # request was for a specific revision id or date c.pkg_revision_id = c.pkg_dict[u'revision_id'] c.pkg_revision_timestamp = c.pkg_dict[u'revision_timestamp'] c.pkg_revision_not_latest = c.pkg_dict[u'revision_id'] != c.pkg.revision.id
def delineate_build_main_navigation(*args): output = h.build_nav_main(*args) if c.user: link = h.link_to('Delineate', h.url_for(controller='ckanext.delineate.controllers.delineatewatershed:DelineatewatershedController', action='show_form')) if p.toolkit.c.action == 'show_form': li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = li + output return output
def delineate_build_main_navigation(*args): output = h.build_nav_main(*args) if c.user: link = h.link_to( 'Delineate', h.url_for( controller= 'ckanext.delineate.controllers.delineatewatershed:DelineatewatershedController', action='show_form')) if p.toolkit.c.action == 'show_form': li = h.literal('<li class="active">') + link + h.literal('</li>') else: li = h.literal('<li>') + link + h.literal('</li>') output = li + output return output
def linked_org_for(user, maxlength=0, avatar=20): if user in [model.PSEUDO_USER__LOGGED_IN, model.PSEUDO_USER__VISITOR]: return user if not isinstance(user, model.User): user_name = unicode(user) user = model.User.get(user_name) if not user: return user_name if user: groups = user.get_groups('organization') if not groups or len(groups) > 1: return user.name if model.User.VALID_NAME.match(user.name) else user.id org = groups[0] # Assuming user only in one org displayname = org.display_name if maxlength and len(displayname) > maxlength: displayname = displayname[:maxlength] + '...' return h.link_to(displayname, h.url_for(controller='organization', action='read', id=org.name))
def filter(self, stream): routes = request.environ.get('pylons.routes_dict') stream = stream | Transformer('head').append(HTML(html.HEAD_CODE)) if routes.get('controller') == 'package' \ and routes.get('action') == 'resource_read': star_html = self.get_star_html(c.resource.get('id')) if star_html: stream = stream | Transformer('body//div[@class="quick-info"]//dl')\ .append(HTML(html.DL_HTML % star_html)) if routes.get('controller') == 'package' \ and routes.get('action') == 'read' \ and c.pkg.id: for resource in c.pkg_dict.get('resources', []): resource_id = resource.get('id') star_html = self.get_star_html(resource_id) if star_html: stream = stream | Transformer('body//div[@id="%s"]//p[@class="extra-links"]' % resource_id)\ .append(HTML(star_html)) # show organization info if self.enable_organisations: if (routes.get('controller') == 'ckanext.qa.controllers.view:ViewController' and routes.get('action') == 'index'): link_text = "Organizations who have published datasets with broken resource links." data = dict(link=h.link_to( link_text, h.url_for( controller= 'ckanext.qa.controllers.qa_organisation:QAOrganisationController', action='broken_resource_links'))) stream = stream | Transformer('body//div[@class="qa-content"]')\ .append(HTML(html.ORGANIZATION_LINK % data)) return stream
def linked_user(user, maxlength=0, avatar=20): '''Brute force disable gravatar, mostly copied from ckan/lib/helpers''' from ckan import model if not isinstance(user, model.User): user_name = unicode(user) user = model.User.get(user_name) if not user: return user_name if user: name = user.name if model.User.VALID_NAME.match(user.name) else user.id displayname = user.display_name if maxlength and len(user.display_name) > maxlength: displayname = displayname[:maxlength] + '...' return h.literal(h.link_to( displayname, h.url_for(controller='user', action='read', id=name) ) )
def linked_user(user, maxlength=0, avatar=20): if not isinstance(user, model.User): user_name = helpers.text_type(user) user = model.User.get(user_name) if not user: return user_name if user: name = user.name if model.User.VALID_NAME.match(user.name) else user.id displayname = user.display_name if maxlength and len(user.display_name) > maxlength: displayname = displayname[:maxlength] + '...' if user.state == 'deleted' and not authz.is_sysadmin(c.user): return helpers.literal(helpers.text_type("Anonymous")) return helpers.literal(u'{icon} {link}'.format( icon=helpers.user_image(user.id, size=avatar), link=helpers.link_to(displayname, helpers.url_for('user.read', id=name))))
def render_package(cls, pkg, context): '''Prepares for rendering a package. Takes a Package object and formats it for the various context variables required to call render. Note that the actual calling of render('package/read') is left to the caller.''' c.pkg_notes_formatted = h.render_markdown(pkg.get('notes')) c.current_rating, c.num_ratings = ckan.rating.get_rating( context['package']) url = pkg.get('url', '') c.pkg_url_link = h.link_to(url, url, rel='foaf:homepage', target='_blank') \ if url else _("No web page given") c.pkg_author_link = cls._person_email_link( name=pkg.get('author'), email=pkg.get('author_email'), fallback=_("Author not given")) c.pkg_maintainer_link = cls._person_email_link( name=pkg.get('maintainer'), email=pkg.get('maintainer_email'), fallback=_("Maintainer not given")) c.package_relationships = context[ 'package'].get_relationships_printable() c.pkg_extras = [] for extra in sorted(pkg.get('extras', []), key=lambda x: x['key']): if extra.get('state') == 'deleted': continue k, v = extra['key'], extra['value'] if k in g.package_hide_extras: continue if isinstance(v, (list, tuple)): v = ", ".join(map(unicode, v)) c.pkg_extras.append((k, v)) if context.get('revision_id') or context.get('revision_date'): # request was for a specific revision id or date c.pkg_revision_id = c.pkg_dict[u'revision_id'] c.pkg_revision_timestamp = c.pkg_dict[u'revision_timestamp'] c.pkg_revision_not_latest = c.pkg_dict[ u'revision_id'] != c.pkg.revision.id
def curation_activity_message(activity): activity_name = activity['data']['curation_activity'] output = '' if activity_name == 'dataset_deposited': output = toolkit._("{actor} deposited dataset {dataset}") elif activity_name == 'dataset_submitted': output = toolkit._("{actor} submitted dataset {dataset} for curation") elif activity_name == 'curator_assigned': curator_link = core_helpers.link_to( activity['data']['curator_name'], toolkit.url_for('user.read', id=activity['data']['curator_name'])) output = toolkit._( "{actor} assigned %s as Curator for dataset {dataset}" % curator_link) elif activity_name == 'curator_removed': output = toolkit._( "{actor} removed the assigned Curator from dataset {dataset}") elif activity_name == 'changes_requested': output = toolkit._( "{actor} unlocked {dataset} for further changes by the Depositor") elif activity_name == 'final_review_requested': output = toolkit._( "{actor} requested a final review of {dataset} from the Depositor") elif activity_name == 'dataset_rejected': output = toolkit._( "{actor} rejected dataset {dataset} for publication") elif activity_name == 'dataset_withdrawn': output = toolkit._( "{actor} withdrew dataset {dataset} from the data deposit") elif activity_name == 'dataset_approved': output = toolkit._( "{actor} approved dataset {dataset} for publication") if activity['data'].get('message'): output = output + ' with the following message: <q class="curation-message">%s</q>' % activity[ 'data']['message'] return output
def filter(self, stream): routes = request.environ.get('pylons.routes_dict') stream = stream | Transformer('head').append(HTML(html.HEAD_CODE)) if routes.get('controller') == 'package' \ and routes.get('action') == 'resource_read': star_html = self.get_star_html(c.resource.get('id')) if star_html: stream = stream | Transformer('body//div[@class="quick-info"]//dl')\ .append(HTML(html.DL_HTML % star_html)) if routes.get('controller') == 'package' \ and routes.get('action') == 'read' \ and c.pkg.id: for resource in c.pkg_dict.get('resources',[]): resource_id = resource.get('id') star_html = self.get_star_html(resource_id) if star_html: stream = stream | Transformer('body//div[@id="%s"]//p[@class="extra-links"]' % resource_id)\ .append(HTML(star_html)) # show organization info if self.enable_organisations: if(routes.get('controller') == 'ckanext.qa.controllers.view:ViewController' and routes.get('action') == 'index'): link_text = "Organizations who have published datasets with broken resource links." data = dict(link = h.link_to(link_text, h.url_for(controller='ckanext.qa.controllers.qa_organisation:QAOrganisationController', action='broken_resource_links') )) stream = stream | Transformer('body//div[@class="qa-content"]')\ .append(HTML(html.ORGANIZATION_LINK % data)) return stream
def linked_org_for(user, maxlength=0, avatar=20): if user in [model.PSEUDO_USER__LOGGED_IN, model.PSEUDO_USER__VISITOR]: return user if not isinstance(user, model.User): user_name = unicode(user) user = model.User.get(user_name) if not user: return user_name if user: groups = user.get_groups('organization') if not groups or len(groups) > 1: return user.name if model.User.VALID_NAME.match( user.name) else user.id org = groups[0] # Assuming user only in one org displayname = org.display_name if maxlength and len(displayname) > maxlength: displayname = displayname[:maxlength] + '...' return h.link_to( displayname, h.url_for(controller='organization', action='read', id=org.name))
def package_matrix(packages, core_fields): html = u'' html += u'<table class="table table-bordered table-condensed packages">' + u"\n" table_rows = [] table_heads = {} for pkg_dict in packages: dic = {} for key, value in pkg_dict.iteritems(): if key == 'tags': tags = [] for tag_dict in pkg_dict['tags']: tags += [tag_dict['name']] dic['tags'] = tags table_heads['tags'] = "" elif key == 'groups': groups = [] #for group_dict in pkg_dict['groups']: # groups += [group_dict['id']] #dic['groups'] = groups dic['groups'] = pkg_dict['groups'] table_heads['groups'] = "" elif key == 'extras': for extra_dict in pkg_dict['extras']: if not extra_dict['key'] in dic.keys(): dic[extra_dict['key']] = extra_dict['value'] table_heads[extra_dict['key']] = "" elif key in core_fields and key not in dic.keys(): dic[key] = value table_heads[key] = "" table_rows.append(dic) if 'title' in table_heads: del table_heads['title'] if 'id' in table_heads: del table_heads['id'] table_heads_sorted = sorted(table_heads.iterkeys()) html += u'<thead>' + u"\n" html += u'<tr>' + u"\n" html += u'<th class="edit narrowTh" style="width: 15px;"><input type="checkbox" name="checkall" value="checkall" class="checkall"/></th>' + u"\n" html += u'<th class="title wideTh" style="max-width: 250px;">Title</th>' + u"\n" for key in table_heads_sorted: html += u'<th class="' + unicode(key) + u' wideTh">' + unicode( _(key)) + u'</th>' + u"\n" html += u'<th class="single_edit narrowTh" style="width: 35px;">Edit</th>' + u"\n" html += u'</tr>' + u"\n" html += u'</thead>' + u"\n" html += u'<tbody>' for row in table_rows: html += u'<tr>' html += u'<td><input type="checkbox" name="package_select" class="package_select" value="' + unicode( row['id']) + u'" /></td>' html += u'<td class="title ' + row['id'] + '">' html += unicode( h.link_to( row['title'] or row['name'], h.url_for(controller='package', action='read', id=row['name']))) html += u'</td>' for key in table_heads_sorted: if key in row: import json try: row_key = json.loads(row[key]) except (ValueError, TypeError): row_key = row[key] if key == "notes": val = h.markdown_extract(row_key) if key == "groups": group_ids = [] group_names = [] for group_dict in row[key]: group_ids += [group_dict['id']] group_names += [ h.group_name_to_title(group_dict['name']) ] row_key = ", ".join(group_ids) val = ", ".join(group_names) elif isinstance(row_key, list): val = ", ".join(row_key) else: val = row_key full_val = row_key html += u'<td class="' + unicode(key) + u' ' + unicode( row['id'] ) + u'" title="' + unicode( full_val ) + u'" style="max-height: 100px; display: block; overflow-y: auto;">' html += unicode(val) html += u'</td>' else: html += u'<td class="' + unicode(key) + u' ' + unicode( row['id'] ) + u'" style="max-height: 100px; display: block; overflow-y: scroll;"></td>' html += u'<td class="single_edit">' + unicode( h.subnav_link(h.icon('package_edit'), controller='package', action='edit', id=row['name'])) + u'</td>' html += u'</tr>' html += u'</tbody>' html += u'</table>' return toolkit.literal(html)
class PackageSaver(object): '''Use this to validate, preview and save packages to the db. @param log_message: optional - only supply this if you want it validated @param author: optional - only supply this if you want it validated ''' @classmethod def render_preview(cls, fs, log_message=None, author=None, client=None): '''Renders a package on the basis of a fieldset - perfect for preview of form data. Note that the actual calling of render('package/read') is left to the caller.''' pkg = cls._preview_pkg(fs, log_message, author, client=client) pkg_dict = table_dictize(pkg, {'model': model}) pkg_dict['extras'] = [] c.pkg = pkg for key, value in pkg.extras.iteritems(): pkg_dict['extras'].append(dict(key=key, value=value)) cls.render_package(pkg_dict, {'package': pkg}) # TODO: rename to something more correct like prepare_for_render @classmethod def render_package(cls, pkg, context): '''Prepares for rendering a package. Takes a Package object and formats it for the various context variables required to call render. Note that the actual calling of render('package/read') is left to the caller.''' try: notes_formatted = ckan.misc.MarkdownFormat().to_html( pkg.get('notes', '')) c.pkg_notes_formatted = genshi.HTML(notes_formatted) except Exception, e: error_msg = "<span class='inline-warning'>%s</span>" % _( "Cannot render package description") c.pkg_notes_formatted = genshi.HTML(error_msg) c.current_rating, c.num_ratings = ckan.rating.get_rating( context['package']) url = pkg.get('url', '') c.pkg_url_link = h.link_to(url, url, rel='foaf:homepage', target='_blank') \ if url else _("No web page given") c.pkg_author_link = cls._person_email_link(pkg.get('author', ''), pkg.get('author_email', ''), "Author") maintainer = pkg.get('maintainer', '') maintainer_email = pkg.get('maintainer_email', '') c.pkg_maintainer_link = cls._person_email_link(maintainer, maintainer_email, "Maintainer") c.package_relationships = context[ 'package'].get_relationships_printable() c.pkg_extras = [] for extra in sorted(pkg.get('extras', []), key=lambda x: x['key']): if extra.get('state') == 'deleted': continue k, v = extra['key'], extra['value'] if k in g.package_hide_extras: continue if isinstance(v, (list, tuple)): v = ", ".join(map(unicode, v)) c.pkg_extras.append((k, v))
def _tag_links(self): tags = self.value tag_links = [h.link_to(tagname, h.url_for(controller='tag', action='read', id=tagname)) for tagname in tags] return literal(' '.join(tag_links))
def _load_data(self, package_name, resource_id, record_id, version=None): """ Load the data for dataset, resource and record (into C var) @param package_name: @param resource_id: @param record_id: @return: """ self.context = {'model': model, 'session': model.Session, 'user': c.user or c.author} # Try & get the resource try: c.resource = get_action('resource_show')(self.context, {'id': resource_id}) c.package = get_action('package_show')(self.context, {'id': package_name}) # required for nav menu c.pkg = self.context['package'] c.pkg_dict = c.package record_data_dict = {'resource_id': resource_id, 'record_id': record_id} if version is not None: version = int(version) record_data_dict['version'] = version c.version = version record = get_action('record_show')(self.context, record_data_dict) c.record_dict = record['data'] except NotFound: abort(404, _('Resource not found')) except NotAuthorized: abort(401, _('Unauthorized to read resource %s') % package_name) field_names = { 'image': c.resource.get('_image_field', None), 'title': c.resource.get('_title_field', None), 'latitude': c.resource.get('_latitude_field', None), 'longitude': c.resource.get('_longitude_field', None), } # if this is a DwC dataset, add some default for image and lat/lon fields if c.resource['format'].lower() == 'dwc': for field_name, dwc_field in [('latitude', 'decimalLatitude'), ('longitude', 'decimalLongitude')]: if dwc_field in c.record_dict: field_names[field_name] = dwc_field # assign title based on the title field c.record_title = c.record_dict.get(field_names['title'], 'Record {}'.format(c.record_dict.get('_id'))) # sanity check: image field hasn't been set to _id if field_names['image'] and field_names['image'] != '_id': default_copyright = '<small>© The Trustees of the Natural History Museum, London</small>' licence_id = c.resource.get('_image_licence') or 'cc-by' short_licence_id = licence_id[:5].lower() # try and overwrite default licence with more specific one for l_id in [licence_id, short_licence_id]: try: licence = model.Package.get_license_register()[l_id] break except KeyError: continue default_licence = 'Licence: %s' % link_to(licence.title, licence.url, target='_blank') # pop the image field so it isn't output as part of the record_dict/field_data dict # (see self.view()) image_field_value = c.record_dict.pop(field_names['image'], None) if image_field_value: # init the images list on the context var c.images = [] if isinstance(image_field_value, list): for image in image_field_value: href = image.get('identifier', None) if href: license_link = link_to(image.get('license'), image.get('license')) if image.get('license', None) else None c.images.append({ 'title': image.get('title', None) or c.record_title, 'href': href, 'copyright': '%s<br />%s' % (license_link or default_licence, image.get('rightsHolder', None) or default_copyright), 'record_id': record_id, 'resource_id': resource_id, 'link': url_for( controller='ckanext.nhm.controllers.record:RecordController', action='view', package_name=package_name, resource_id=resource_id, record_id=record_id ), }) else: # it's a string field value, use the delimiter to split up the field value (if # there is one!) delimiter = c.resource.get('_image_delimiter', None) if delimiter: images = image_field_value.split(delimiter) else: images = [image_field_value] # loop through the images, adding dicts with their details to the context for image in images: if image.strip(): c.images.append({ 'title': c.record_title, 'href': image.strip(), 'copyright': '%s<br />%s' % (default_licence, default_copyright) }) if field_names['latitude'] and field_names['longitude']: latitude = c.record_dict.get(field_names['latitude']) longitude = c.record_dict.get(field_names['longitude']) if latitude and longitude: # create a piece of GeoJSON to point at the specific record location on a map c.record_map = json.dumps({ 'type': 'Point', 'coordinates': [float(longitude), float(latitude)] })
def _load_data(self, package_name, resource_id, record_id): """ Load the data for dataset, resource and record (into C var) @param package_name: @param resource_id: @param record_id: @return: """ self.context = {'model': model, 'session': model.Session, 'user': c.user or c.author} # Try & get the resource try: c.resource = get_action('resource_show')(self.context, {'id': resource_id}) c.package = get_action('package_show')(self.context, {'id': package_name}) # required for nav menu c.pkg = self.context['package'] c.pkg_dict = c.package record = get_action('record_show')(self.context, {'resource_id': resource_id, 'record_id': record_id}) c.record_dict = record['data'] record_field_types = {f['id']: f['type'] for f in record['fields']} except NotFound: abort(404, _('Resource not found')) except NotAuthorized: abort(401, _('Unauthorized to read resource %s') % package_name) field_names = { 'image': c.resource.get('_image_field', None), 'title': c.resource.get('_title_field', None), 'latitude': None, 'longitude': None } # Get lat/long fields # Loop through all the views - if we have a tiled map view with lat/lon fields # We'll use those fields to add the map views = p.toolkit.get_action('resource_view_list')(self.context, {'id': resource_id}) for view in views: if view['view_type'] == TILED_MAP_TYPE: field_names['latitude'] = view[u'latitude_field'] field_names['longitude'] = view[u'longitude_field'] break # If this is a DwC dataset, add some default for image and lat/lon fields if c.resource['format'].lower() == 'dwc': for field_name, dwc_field in [('latitude', 'decimalLatitude'), ('longitude', 'decimalLongitude')]: if dwc_field in c.record_dict: field_names[field_name] = dwc_field # Assign title based on the title field c.record_title = c.record_dict.get(field_names['title'], 'Record %s' % c.record_dict.get('_id')) # Sanity check: image field hasn't been set to _id if field_names['image'] and field_names['image'] != '_id': try: image_field_type = record_field_types[field_names['image']] except KeyError: pass else: default_copyright = '<small>© The Trustees of the Natural History Museum, London</small>' licence_id = c.resource.get('_image_licence') or 'ODC-BY-1.0' licence = model.Package.get_license_register()[licence_id] default_licence = 'Licence: %s' % link_to(licence.title, licence.url, target='_blank') image_field_value = c.record_dict.pop(field_names['image']) if image_field_value: if image_field_type == 'json': c.images = [] try: images = json.loads(image_field_value) except ValueError: pass else: for image in images: href = image.get('identifier', None) if href: license = link_to(image.get('license'), image.get('license')) if image.get('license', None) else None c.images.append({ 'title': image.get('title', None) or c.record_title, 'href': href, 'copyright': '%s<br />%s' % (license or default_licence, image.get('rightsHolder', None) or default_copyright), 'record_id': record_id, 'resource_id': resource_id, 'link': url_for( controller='ckanext.nhm.controllers.record:RecordController', action='view', package_name=package_name, resource_id=resource_id, record_id=record_id ), }) else: try: # Pop the image field so it won't be output as part of the record_dict / field_data dict (see self.view()) c.images = [{'title': c.record_title, 'href': image.strip(), 'copyright': '%s<br />%s' % (default_licence, default_copyright)} for image in image_field_value.split(';') if image.strip()] except (KeyError, AttributeError): # Skip errors - there are no images pass if field_names['latitude'] and field_names['longitude']: latitude, longitude = c.record_dict.get(field_names['latitude']), c.record_dict.get(field_names['longitude']) if latitude and longitude: c.record_map = json.dumps({ 'type': 'Point', 'coordinates': [longitude, latitude] })