def dge_package_list_for_source(source_id): ''' Creates a dataset list with the ones belonging to a particular harvest source. It calls the package_list snippet and the pager. ''' DATASET_TYPE_NAME = 'harvest' limit = 20 page = int(request.params.get('page', 1)) fq = 'harvest_source_id:"{0}"'.format(source_id) search_dict = { 'fq': fq, 'rows': limit, 'sort': 'metadata_modified desc', 'start': (page - 1) * limit, } context = {'model': model, 'session': model.Session} owner_org = toolkit.c.harvest_source.get('owner_org', '') if owner_org: user_member_of_orgs = [ org['id'] for org in h.organizations_available('read') ] if (toolkit.c.harvest_source and owner_org in user_member_of_orgs): context['ignore_capacity_check'] = True query = logic.get_action('package_search')(context, search_dict) base_url = h.url_for('{0}_read'.format(DATASET_TYPE_NAME), id=source_id) def pager_url(q=None, page=None): url = base_url if page: url += '?page={0}'.format(page) return url pager = h.Page(collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit) pager.items = query['results'] if query['results']: out = h.snippet('snippets/dge_package_list.html', packages=query['results']) out += pager.pager() else: out = h.snippet('snippets/package_list_empty.html') return out
def package_list_for_source(source_id): ''' Creates a dataset list with the ones belonging to a particular harvest source. It calls the package_list snippet and the pager. ''' limit = 20 page = int(request.params.get('page', 1)) fq = '+harvest_source_id:"{0}"'.format(source_id) search_dict = { 'fq': fq, 'rows': limit, 'sort': 'metadata_modified desc', 'start': (page - 1) * limit, } context = {'model': model, 'session': model.Session} harvest_source = get_harvest_source(source_id) owner_org = harvest_source.get('owner_org', '') if owner_org: user_member_of_orgs = [org['id'] for org in h.organizations_available('read')] if (harvest_source and owner_org in user_member_of_orgs): context['ignore_capacity_check'] = True query = logic.get_action('package_search')(context, search_dict) base_url = h.url_for('{0}_read'.format(DATASET_TYPE_NAME), id=source_id) def pager_url(q=None, page=None): url = base_url if page: url += '?page={0}'.format(page) return url pager = h.Page( collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit ) pager.items = query['results'] if query['results']: out = h.snippet('snippets/package_list.html', packages=query['results']) out += pager.pager() else: out = h.snippet('snippets/package_list_empty.html') return out
def get_datapreview_recombinant(resource_name, res_id): from ckanext.recombinant.tables import get_chromo chromo = get_chromo(resource_name) default_preview_args = {} lc = ckanapi.LocalCKAN(username=c.user) results = lc.action.datastore_search( resource_id=res_id, limit=0, ) priority = len(chromo['datastore_primary_key']) pk_priority = 0 fields = [] for f in chromo['fields']: out = { 'type': f['datastore_type'], 'id': f['datastore_id'], 'label': h._(f['label'])} if out['id'] in chromo['datastore_primary_key']: out['priority'] = pk_priority pk_priority += 1 else: out['priority'] = priority priority += 1 fields.append(out) return h.snippet('package/wet_datatable.html', resource_name=resource_name, resource_id=res_id, ds_fields=fields)
def get_datapreview_recombinant(dataset_type, res_id): from ckanext.recombinant.plugins import get_table t = get_table(dataset_type) default_preview_args = {} if 'default_preview_sort' in t: default_preview_args['sort'] = t['default_preview_sort'] lc = ckanapi.LocalCKAN(username=c.user) results = lc.action.datastore_search( resource_id=res_id, limit=0, **default_preview_args) lang = h.lang() field_label = {} for f in t['fields']: label = f['label'].split(' / ') label = label[0] if lang == 'en' else label[-1] field_label[f['datastore_id']] = label fields = [{ 'type': f['type'], 'id': f['id'], 'label': field_label.get(f['id'], f['id'])} for f in results['fields']] return h.snippet('package/wet_datatable.html', resource_id=res_id, ds_fields=fields)
def hdx_resource_preview(resource, package): ## COPY OF THE DEFAULT HELPER BY THE SAME NAME BUT FORCES URLS OVER HTTPS ''' Returns a rendered snippet for a embedded resource preview. Depending on the type, different previews are loadeSd. This could be an img tag where the image is loaded directly or an iframe that embeds a web page, recline or a pdf preview. ''' if not resource['url']: return False format_lower = datapreview.res_format(resource) directly = False data_dict = {'resource': resource, 'package': package} if datapreview.get_preview_plugin(data_dict, return_first=True): url = tk.url_for(controller='package', action='resource_datapreview', resource_id=resource['id'], id=package['id'], qualified=True) else: return False return h.snippet("dataviewer/snippets/data_preview.html", embed=directly, resource_url=url, raw_resource_url=https_load(resource.get('url')))
def hdx_follow_button(obj_type, obj_id, **kw): ''' This is a modified version of the ckan core follow_button() helper It returns a simple link for a bootstrap dropdown menu Return a follow button for the given object type and id. If the user is not logged in return an empty string instead. :param obj_type: the type of the object to be followed when the follow button is clicked, e.g. 'user' or 'dataset' :type obj_type: string :param obj_id: the id of the object to be followed when the follow button is clicked :type obj_id: string :returns: a follow button as an HTML snippet :rtype: string ''' obj_type = obj_type.lower() assert obj_type in h._follow_objects # If the user is logged in show the follow/unfollow button if c.user: context = {'model': model, 'session': model.Session, 'user': c.user} action = 'am_following_%s' % obj_type following = logic.get_action(action)(context, {'id': obj_id}) return h.snippet('snippets/hdx_follow_button.html', following=following, obj_id=obj_id, obj_type=obj_type, params=kw) return ''
def get_datapreview_ati(res_id): lc = ckanapi.LocalCKAN(username=c.user) results = lc.action.datastore_search(resource_id=res_id, sort='year,month desc', limit=3000) return h.snippet('package/wet_datatable.html', ds_fields=results['fields'], ds_records=results['records'])
def get_datapreview_contracts(res_id): lc = ckanapi.LocalCKAN(username=c.user) results = lc.action.datastore_search( resource_id=res_id, sort='contract_period_start,contract_period_end desc', limit=3000) return h.snippet('package/wet_datatable.html', ds_fields=results['fields'], ds_records=results['records'])
def get_datapreview_ati(res_id): lc = ckanapi.LocalCKAN(username=c.user) results = lc.action.datastore_search( resource_id=res_id, sort='year desc, month desc', limit=DATAPREVIEW_MAX) return h.snippet('package/wet_datatable.html', ds_fields=results['fields'], ds_records=results['records'])
def hdx_resource_preview(resource, package): ## COPY OF THE DEFAULT HELPER BY THE SAME NAME BUT FORCES URLS OVER HTTPS ''' Returns a rendered snippet for a embedded resource preview. Depending on the type, different previews are loadeSd. This could be an img tag where the image is loaded directly or an iframe that embeds a web page, recline or a pdf preview. ''' if not resource['url']: return h.snippet("dataviewer/snippets/no_preview.html", resource_type=format_lower, reason=_(u'The resource url is not specified.')) format_lower = datapreview.res_format(resource) directly = False data_dict = {'resource': resource, 'package': package} if datapreview.get_preview_plugin(data_dict, return_first=True): url = h.url_for(controller='package', action='resource_datapreview', resource_id=resource['id'], id=package['id'], qualified=True) elif format_lower in datapreview.direct(): directly = True url = resource['url'] elif format_lower in datapreview.loadable(): url = resource['url'] else: reason = None if format_lower: log.info( _(u'No preview handler for resource of type {0}'.format( format_lower)) ) else: reason = _(u'The resource format is not specified.') return h.snippet("dataviewer/snippets/no_preview.html", reason=reason, resource_type=format_lower) return h.snippet("dataviewer/snippets/data_preview.html", embed=directly, resource_url=url, raw_resource_url=https_load(resource.get('url')))
def get_datapreview(self, res_id): #import pdb; pdb.set_trace() dsq_results = ckan.logic.get_action('datastore_search')({}, { 'resource_id': res_id, 'limit': 100 }) return h.snippet('package/wet_datatable.html', ds_fields=dsq_results['fields'], ds_records=dsq_results['records'])
def hdx_popular(type_, number, min=1, title=None): ''' display a popular icon. ''' from ckan.lib.helpers import snippet as snippet if type_ == 'views': title = ungettext('{number} view', '{number} views', number) elif type_ == 'recent views': title = ungettext('{number} recent view', '{number} recent views', number) elif type_ == 'downloads': title = ungettext('{number} download', '{number} downloads', number) elif not title: raise Exception('popular() did not recieve a valid type_ or title') return snippet('snippets/popular.html', title=title, number=number, min=min)
def persistent_follow_button(obj_type, obj_id): '''Return a follow button for the given object type and id. Replaces ckan.lib.follow_button which returns an empty string for anonymous users For anon users this function outputs a follow button which links through to the login page ''' obj_type = obj_type.lower() assert obj_type in _follow_objects if c.user: context = {'model': model, 'session': model.Session, 'user': c.user} action = 'am_following_%s' % obj_type following = logic.get_action(action)(context, {'id': obj_id}) return snippet('snippets/follow_button.html', following=following, obj_id=obj_id, obj_type=obj_type) return snippet('snippets/anon_follow_button.html', obj_id=obj_id, obj_type=obj_type)
def hdx_follow_link(obj_type, obj_id, extra_text, cls=None): obj_type = obj_type.lower() assert obj_type in h._follow_objects # If the user is logged in show the follow/unfollow button if c.user: context = {'model': model, 'session': model.Session, 'user': c.user} action = 'am_following_%s' % obj_type following = logic.get_action(action)(context, {'id': obj_id}) return h.snippet('search/snippets/follow_link.html', following=following, obj_id=obj_id, obj_type=obj_type, extra_text=extra_text, cls=cls) return ''
def hdx_follow_link(obj_type, obj_id, extra_text, cls=None, confirm_text=None): obj_type = obj_type.lower() assert obj_type in h._follow_objects # If the user is logged in show the follow/unfollow button if c.user: context = {'model': model, 'session': model.Session, 'user': c.user} action = 'am_following_%s' % obj_type following = logic.get_action(action)(context, {'id': obj_id}) return h.snippet('search/snippets/follow_link.html', following=following, obj_id=obj_id, obj_type=obj_type, extra_text=extra_text, confirm_text=confirm_text, cls=cls) return ''
def hdx_follow_button(obj_type, obj_id, **kw): ''' This is a modified version of the ckan core follow_button() helper It returns a simple link for a bootstrap dropdown menu Return a follow button for the given object type and id. If the user is not logged in return an empty string instead. :param obj_type: the type of the object to be followed when the follow button is clicked, e.g. 'user' or 'dataset' :type obj_type: string :param obj_id: the id of the object to be followed when the follow button is clicked :type obj_id: string :returns: a follow button as an HTML snippet :rtype: string ''' obj_type = obj_type.lower() assert obj_type in h._follow_objects # If the user is logged in show the follow/unfollow button if c.user: context = {'model': model, 'session': model.Session, 'user': c.user} action = 'am_following_%s' % obj_type following = logic.get_action(action)(context, {'id': obj_id}) follow_extra_text = _('This Data') if kw and 'follow_extra_text' in kw: follow_extra_text = kw.pop('follow_extra_text') return h.snippet('snippets/hdx_follow_button.html', following=following, obj_id=obj_id, obj_type=obj_type, follow_extra_text=follow_extra_text, params=kw) return ''
def get_datapreview(self, res_id): #import pdb; pdb.set_trace() dsq_results = ckan.logic.get_action('datastore_search')({}, {'resource_id': res_id, 'limit' : 100}) return h.snippet('package/wet_datatable.html', ds_fields=dsq_results['fields'], ds_records=dsq_results['records'])