def index(self): try: # package search context = {'model': model, 'session': model.Session, 'user': c.user or c.author} data_dict = { 'q': '*:*', 'facet.field': g.facets, 'rows': 0, 'start': 0, 'fq': 'capacity:"public"' } query = ckan.logic.get_action('package_search')( context, data_dict) c.package_count = query['count'] c.facets = query['facets'] maintain.deprecate_context_item( 'facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] data_dict = {'order_by': 'packages', 'all_fields': 1} # only give the terms to group dictize that are returned in the # facets as full results take a lot longer if 'groups' in c.search_facets: data_dict['groups'] = [ item['name'] for item in c.search_facets['groups']['items'] ] c.groups = ckan.logic.get_action('group_list')(context, data_dict) except SearchError, se: c.package_count = 0 c.groups = []
def setup_template_variables(self, context, data_dict): authz_fn = logic.get_action("group_list_authz") c.groups_authz = authz_fn(context, data_dict) data_dict.update({"available_only": True}) c.groups_available = authz_fn(context, data_dict) c.licenses = [("", "")] + base.model.Package.get_license_options() # CS: bad_spelling ignore 2 lines c.licences = c.licenses maintain.deprecate_context_item("licences", "Use `c.licenses` instead") c.is_sysadmin = ckan.authz.is_sysadmin(c.user) if c.pkg: # Used by the disqus plugin c.related_count = c.pkg.related_count if context.get("revision_id") or context.get("revision_date"): if context.get("revision_id"): rev = base.model.Session.query(base.model.Revision).filter_by(id=context["revision_id"]).first() c.revision_date = rev.timestamp if rev else "?" else: c.revision_date = context.get("revision_date") ## This is messy as auths take domain object not data_dict context_pkg = context.get("package", None) pkg = context_pkg or c.pkg if pkg: try: if not context_pkg: context["package"] = pkg logic.check_access("package_change_state", context) c.auth_for_change_state = True except logic.NotAuthorized: c.auth_for_change_state = False
def index(self): try: # package search context = { 'model': model, 'session': model.Session, 'user': c.user or c.author } data_dict = { 'q': '*:*', 'facet.field': g.facets, 'rows': 0, 'start': 0, 'fq': 'capacity:"public"' } query = ckan.logic.get_action('package_search')(context, data_dict) c.package_count = query['count'] c.facets = query['facets'] maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] data_dict = {'order_by': 'packages', 'all_fields': 1} # only give the terms to group dictize that are returned in the # facets as full results take a lot longer if 'groups' in c.search_facets: data_dict['groups'] = [ item['name'] for item in c.search_facets['groups']['items'] ] c.groups = ckan.logic.get_action('group_list')(context, data_dict) except SearchError, se: c.package_count = 0 c.groups = []
def setup_template_variables(self, context, data_dict): authz_fn = logic.get_action('group_list_authz') c.groups_authz = authz_fn(context, data_dict) data_dict.update({'available_only': True}) c.groups_available = authz_fn(context, data_dict) c.licenses = [('', '')] + base.model.Package.get_license_options() # CS: bad_spelling ignore 2 lines c.licences = c.licenses maintain.deprecate_context_item('licences', 'Use `c.licenses` instead') c.is_sysadmin = ckan.new_authz.is_sysadmin(c.user) if c.pkg: c.related_count = c.pkg.related_count ## This is messy as auths take domain object not data_dict context_pkg = context.get('package', None) pkg = context_pkg or c.pkg if pkg: try: if not context_pkg: context['package'] = pkg logic.check_access('package_change_state', context) c.auth_for_change_state = True except logic.NotAuthorized: c.auth_for_change_state = False
def setup_template_variables(self, context, data_dict): authz_fn = logic.get_action('group_list_authz') c.groups_authz = authz_fn(context, data_dict) data_dict.update({'available_only': True}) c.groups_available = authz_fn(context, data_dict) c.licenses = [('', '')] + base.model.Package.get_license_options() # CS: bad_spelling ignore 2 lines c.licences = c.licenses maintain.deprecate_context_item('licences', 'Use `c.licenses` instead') c.is_sysadmin = ckan.authz.is_sysadmin(c.user) if context.get('revision_id') or context.get('revision_date'): if context.get('revision_id'): rev = base.model.Session.query(base.model.Revision) \ .filter_by(id=context['revision_id']) \ .first() c.revision_date = rev.timestamp if rev else '?' else: c.revision_date = context.get('revision_date') ## This is messy as auths take domain object not data_dict context_pkg = context.get('package', None) pkg = context_pkg or c.pkg if pkg: try: if not context_pkg: context['package'] = pkg logic.check_access('package_change_state', context) c.auth_for_change_state = True except logic.NotAuthorized: c.auth_for_change_state = False
def setup_template_variables(self, context, data_dict): from ckan.lib.helpers import render_markdown authz_fn = logic.get_action('group_list_authz') c.groups_authz = authz_fn(context, data_dict) data_dict.update({'available_only': True}) c.groups_available = authz_fn(context, data_dict) c.licenses = [('', '')] + base.model.Package.get_license_options() # CS: bad_spelling ignore 2 lines c.licences = c.licenses maintain.deprecate_context_item('licences', 'Use `c.licenses` instead') c.is_sysadmin = ckan.new_authz.is_sysadmin(c.user) if c.pkg: c.related_count = c.pkg.related_count c.pkg_notes_formatted = render_markdown(c.pkg.notes) if context.get('revision_id') or context.get('revision_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 ## This is messy as auths take domain object not data_dict context_pkg = context.get('package', None) pkg = context_pkg or c.pkg if pkg: try: if not context_pkg: context['package'] = pkg logic.check_access('package_change_state', context) c.auth_for_change_state = True except logic.NotAuthorized: c.auth_for_change_state = False
def __before__(self, action, **params): c.__timer = time.time() app_globals.app_globals._check_uptodate() self._identify_user() i18n.handle_request(request, c) maintain.deprecate_context_item("new_activities", "Use `h.new_activities` instead.")
def __before__(self, action, **params): c.__timer = time.time() app_globals.app_globals._check_uptodate() self._identify_user() i18n.handle_request(request, c) maintain.deprecate_context_item('new_activities', 'Use `h.new_activities` instead.')
def index(self): try: # package search context = {'model': model, 'session': model.Session, 'user': c.user or c.author} data_dict = { 'q': '*:*', 'facet.field': g.facets, 'rows': 0, 'start': 0, 'fq': 'capacity:"public"' } query = ckan.logic.get_action('package_search')( context, data_dict) c.package_count = query['count'] c.facets = query['facets'] maintain.deprecate_context_item( 'facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] tag_list = self._get_category_list(context) results = [] for tag in tag_list: tag_data_dict = {'id': tag['id'], 'package_details':False} try: tag_details = self._tag_show(context, tag_data_dict) results.append({'id':tag['id'], 'display_name':tag['display_name'], 'description':u'', 'packages':len(tag_details['packages'])}) #results.append(Category(tag['id'], tag['display_name'], u'', len(tag_details['packages']))) except NotFound: #abort(404, _('Tag not found')) pass #data_dict = {'order_by': 'packages', 'all_fields': 1} ## only give the terms to group dictize that are returned in the ## facets as full results take a lot longer #if 'groups' in c.search_facets: # data_dict['groups'] = [ item['name'] for item in # c.search_facets['groups']['items'] ] #c.groups = ckan.logic.get_action('group_list')(context, data_dict) #print "RESULTS", results sorted_results = sorted(results, key=lambda k: k['packages'], reverse=True) #print "SORTED RESULTS", sorted_results c.categories = sorted_results except SearchError, se: c.package_count = 0 #c.groups = [] c.categories = []
def index(self): try: # package search context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj } data_dict = { 'q': '*:*', 'facet.field': g.facets, 'rows': 4, 'start': 0, 'sort': 'views_recent desc', 'fq': 'capacity:"public"' } query = logic.get_action('package_search')(context, data_dict) c.search_facets = query['search_facets'] c.package_count = query['count'] c.datasets = query['results'] c.facets = query['facets'] maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.facet_titles = { 'organization': _('Organizations'), # 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license': _('Licenses'), } except search.SearchError: c.package_count = 0 if c.userobj and not c.userobj.email: url = h.url_for(controller='user', action='edit') msg = _('Please <a href="%s">update your profile</a>' ' and add your email address. ') % url + \ _('%s uses your email address' ' if you need to reset your password.') \ % g.site_title h.flash_notice(msg, allow_html=True) return base.render('home/index.html', cache_force=True)
def __before__(self, action, **params): c.__timer = time.time() c.__version__ = ckan.__version__ app_globals.app_globals._check_uptodate() self._identify_user() i18n.handle_request(request, c) maintain.deprecate_context_item('new_activities', 'Use `h.new_activities` instead.') # Prevents the variable interfering with the root_path logic if 'SCRIPT_NAME' in request.environ: request.environ['SCRIPT_NAME'] = ''
def __before__(self, action, **params): c.__timer = time.time() c.__version__ = ckan.__version__ app_globals.app_globals._check_uptodate() self._identify_user() i18n.handle_request(request, c) maintain.deprecate_context_item( 'new_activities', 'Use `h.new_activities` instead.') # Prevents the variable interfering with the root_path logic if 'SCRIPT_NAME' in request.environ: request.environ['SCRIPT_NAME'] = ''
def index(self): try: # package search context = {'model': model, 'session': model.Session, 'user': c.user, 'auth_user_obj': c.userobj} data_dict = { 'q': '*:*', 'facet.field': g.facets, 'rows': 4, 'start': 0, 'sort': 'views_recent desc', 'fq': 'capacity:"public"' } query = logic.get_action('package_search')( context, data_dict) c.search_facets = query['search_facets'] c.package_count = query['count'] c.datasets = query['results'] c.facets = query['facets'] maintain.deprecate_context_item( 'facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.facet_titles = { 'organization': _('Organizations'), 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license': _('Licenses'), } except search.SearchError: c.package_count = 0 if c.userobj and not c.userobj.email: url = h.url_for(controller='user', action='edit') msg = _('Please <a href="%s">update your profile</a>' ' and add your email address. ') % url + \ _('%s uses your email address' ' if you need to reset your password.') \ % g.site_title h.flash_notice(msg, allow_html=True) return base.render('home/index.html', cache_force=True)
def index(self): try: # package search context = { 'model': model, 'session': model.Session, 'user': c.user or c.author } data_dict = { 'q': '*:*', 'facet.field': g.facets, 'rows': 4, 'start': 0, 'sort': 'views_recent desc', 'fq': 'capacity:"public"' } query = logic.get_action('package_search')(context, data_dict) c.search_facets = query['search_facets'] c.package_count = query['count'] c.datasets = query['results'] c.facets = query['facets'] maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.facet_titles = { 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license': _('Licence'), } data_dict = {'sort': 'packages', 'all_fields': 1} # only give the terms to group dictize that are returned in the # facets as full results take a lot longer if 'groups' in c.search_facets: data_dict['groups'] = [ item['name'] for item in c.search_facets['groups']['items'] ] c.groups = logic.get_action('group_list')(context, data_dict) except search.SearchError, se: c.package_count = 0 c.groups = []
def index(self): try: # package search context = {'model': model, 'session': model.Session, 'user': c.user or c.author} data_dict = { 'q': '*:*', 'facet.field': g.facets, 'rows': 1000, 'start': 0, 'sort': 'views_recent desc', 'fq': 'capacity:"public"' } query = ckan.logic.get_action('package_search')( context, data_dict) c.search_facets = query['search_facets'] c.package_count = query['count'] c.datasets = query['results'] c.datasets = filter(lambda x: len(filter(lambda y: y['key'].lower() == 'destacado' and y['value'].lower() == '"si"', x['extras'])) > 0, c.datasets ) c.datasets = c.datasets[0:6] c.facets = query['facets'] maintain.deprecate_context_item( 'facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.facet_titles = {'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license': _('Licence'), } data_dict = {'sort': 'packages', 'all_fields': 1} # only give the terms to group dictize that are returned in the # facets as full results take a lot longer if 'groups' in c.search_facets: data_dict['groups'] = [ item['name'] for item in c.search_facets['groups']['items'] ] c.groups = ckan.logic.get_action('group_list')(context, data_dict) except SearchError, se: c.package_count = 0 c.groups = []
def index(self): ''' Simplified index function compared to CKAN's original one. :return: render home/index.html ''' try: # package search context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'schema': kata_schemas.package_search_schema()} data_dict = { 'q': '*:*', 'facet.field': g.facets, 'facet.limit': -1, 'rows': 0, 'start': 0, 'sort': 'title_string desc', 'fq': 'capacity:"public" +dataset_type:dataset' } query = logic.get_action('package_search')( context, data_dict) c.package_count = query['count'] c.facets = query['facets'] maintain.deprecate_context_item( 'facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.num_tags = len(c.facets.get('tags')) c.num_discipline = len(c.facets.get('extras_discipline')) except search.SearchError: c.package_count = 0 c.groups = [] c.num_tags = 0 c.num_discipline = 0 return render("home/index.html", cache_force=True)
c.search_facets = {} c.page = h.Page(collection=[]) c.search_facets_limits = {} for facet in c.search_facets.keys(): try: limit = int(request.params.get('_%s_limit' % facet, g.facets_default_number or 10)) except ValueError: p.toolkit.abort(400, _('Parameter "{parameter_name}" is not ' 'an integer').format( parameter_name='_%s_limit' % facet)) c.search_facets_limits[facet] = limit if g.facets_default_number: maintain.deprecate_context_item( 'facets', 'Use `c.search_facets` instead.') self._setup_template_variables(context, {}, package_type=package_type) data_dict = { 'org_name': org['name'], 'page_name': 'data' } data_page = p.toolkit.get_action('orgportals_pages_show')({}, data_dict) if len(data_page['topics']) > 0: data_page['topics'] = json.loads(data_page['topics']) data_page['topics'].sort(key=itemgetter('order')) else:
def _read(self, id, limit): ''' This is common code used by both read and bulk_process''' group_type = self._get_group_type(id.split('@')[0]) context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'schema': self._db_to_form_schema(group_type=group_type), 'for_view': True, 'extras_as_string': True} q = c.q = request.params.get('q', '') # Search within group if c.group_dict.get('is_organization'): q += ' owner_org:"%s"' % c.group_dict.get('id') else: q += ' groups:"%s"' % c.group_dict.get('name') description = c.group_dict.get('description') if description: if group_type == 'group': c.description_formatted = h.render_markdown(description.get(h.lang())) else: c.description_formatted = h.render_markdown(description) context['return_query'] = True # c.group_admins is used by CKAN's legacy (Genshi) templates only, # if we drop support for those then we can delete this line. c.group_admins = new_authz.get_group_or_org_admin_ids(c.group.id) page = self._get_page_number(request.params) # most search operations should reset the page counter: params_nopage = [(k, v) for k, v in request.params.items() if k != 'page'] sort_by = request.params.get('sort', None) def search_url(params): if group_type == 'organization': if c.action == 'bulk_process': url = self._url_for(controller='organization', action='bulk_process', id=id) else: url = self._url_for(controller='organization', action='read', id=id) else: url = self._url_for(controller='group', action='read', id=id) params = [(k, v.encode('utf-8') if isinstance(v, basestring) else str(v)) for k, v in params] return url + u'?' + urlencode(params) def drill_down_url(**by): return h.add_url_param(alternative_url=None, controller='group', action='read', extras=dict(id=c.group_dict.get('name')), new_params=by) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): return h.remove_url_param(key, value=value, replace=replace, controller='group', action='read', extras=dict(id=c.group_dict.get('name'))) c.remove_field = remove_field def pager_url(q=None, page=None): params = list(params_nopage) params.append(('page', page)) return search_url(params) try: c.fields = [] search_extras = {} for (param, value) in request.params.items(): if not param in ['q', 'page', 'sort'] \ and len(value) and not param.startswith('_'): if not param.startswith('ext_'): c.fields.append((param, value)) q += ' %s: "%s"' % (param, value) else: search_extras[param] = value fq = 'capacity:"public"' user_member_of_orgs = [org['id'] for org in h.organizations_available('read')] if (c.group and c.group.id in user_member_of_orgs): fq = '' context['ignore_capacity_check'] = True facets = OrderedDict() default_facet_titles = {'organization': _('Organizations'), 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license_id': _('Licenses')} for facet in g.facets: if facet in default_facet_titles: facets[facet] = default_facet_titles[facet] else: facets[facet] = facet # Facet titles for plugin in plugins.PluginImplementations(plugins.IFacets): if self.group_type == 'organization': facets = plugin.organization_facets( facets, self.group_type, None) else: facets = plugin.group_facets( facets, self.group_type, None) if 'capacity' in facets and (self.group_type != 'organization' or not user_member_of_orgs): del facets['capacity'] c.facet_titles = facets data_dict = { 'q': q, 'fq': fq, 'facet.field': facets.keys(), 'rows': limit, 'sort': sort_by, 'start': (page - 1) * limit, 'extras': search_extras } context_ = dict((k, v) for (k, v) in context.items() if k != 'schema') query = get_action('package_search')(context_, data_dict) c.page = h.Page( collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit ) c.group_dict['package_count'] = query['count'] c.facets = query['facets'] maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.search_facets_limits = {} for facet in c.facets.keys(): limit = int(request.params.get('_%s_limit' % facet, g.facets_default_number)) c.search_facets_limits[facet] = limit c.page.items = query['results'] c.sort_by_selected = sort_by except search.SearchError, se: log.error('Group search error: %r', se.args) c.query_error = True c.facets = {} c.page = h.Page(collection=[])
class CustomPackageController(package.PackageController): def _content_type_from_extension(self, ext): ct, ext = accept.parse_extension(ext) if not ct: return None, None return ct, ext def search(self): custom_base.g_analitics() from ckan.lib.search import SearchError package_type = self._guess_package_type() try: context = {'model': model, 'user': c.user or c.author, 'auth_user_obj': c.userobj} check_access('site_read', context) except NotAuthorized: abort(401, _('Not authorized to see this page')) # unicode format (decoded from utf8) q = c.q = request.params.get('q', u'') c.query_error = False page = self._get_page_number(request.params) limit = g.datasets_per_page # most search operations should reset the page counter: params_nopage = [(k, v) for k, v in request.params.items() if k != 'page'] def drill_down_url(alternative_url=None, **by): return h.add_url_param(alternative_url=alternative_url, controller='package', action='search', new_params=by) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): return h.remove_url_param(key, value=value, replace=replace, controller='package', action='search') c.remove_field = remove_field sort_by = request.params.get('sort', None) params_nosort = [(k, v) for k, v in params_nopage if k != 'sort'] def _sort_by(fields): """ Sort by the given list of fields. Each entry in the list is a 2-tuple: (fieldname, sort_order) eg - [('metadata_modified', 'desc'), ('name', 'asc')] If fields is empty, then the default ordering is used. """ params = params_nosort[:] if fields: sort_string = ', '.join('%s %s' % f for f in fields) params.append(('sort', sort_string)) return search_url(params, package_type) c.sort_by = _sort_by if not sort_by: c.sort_by_fields = [] else: c.sort_by_fields = [field.split()[0] for field in sort_by.split(',')] def pager_url(q=None, page=None): params = list(params_nopage) params.append(('page', page)) return search_url(params, package_type) c.search_url_params = urlencode(_encode_params(params_nopage)) try: c.fields = [] # c.fields_grouped will contain a dict of params containing # a list of values eg {'tags':['tag1', 'tag2']} c.fields_grouped = {} search_extras = {} fq = '' for (param, value) in request.params.items(): if param not in ['q', 'page', 'sort'] \ and len(value) and not param.startswith('_'): if not param.startswith('ext_'): c.fields.append((param, value)) fq += ' %s:"%s"' % (param, value) if param not in c.fields_grouped: c.fields_grouped[param] = [value] else: c.fields_grouped[param].append(value) else: search_extras[param] = value context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'for_view': True, 'auth_user_obj': c.userobj} if package_type and package_type != 'dataset': # Only show datasets of this particular type fq += ' +dataset_type:{type}'.format(type=package_type) else: # Unless changed via config options, don't show non standard # dataset types on the default search page if not asbool( config.get('ckan.search.show_all_types', 'False')): fq += ' +dataset_type:dataset' facets = OrderedDict() default_facet_titles = { 'organization': _('Organizations'), # 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license_id': _('Licenses'), } #remove the group facet from search if 'groups' in g.facets: g.facets.remove('groups') for facet in g.facets: if facet in default_facet_titles: facets[facet] = default_facet_titles[facet] else: facets[facet] = facet # Facet titles for plugin in p.PluginImplementations(p.IFacets): facets = plugin.dataset_facets(facets, package_type) c.facet_titles = facets data_dict = { 'q': q, 'fq': fq.strip(), 'facet.field': facets.keys(), 'rows': limit, 'start': (page - 1) * limit, 'sort': sort_by, 'extras': search_extras } query = get_action('package_search')(context, data_dict) c.sort_by_selected = query['sort'] c.page = h.Page( collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit ) c.facets = query['facets'] c.search_facets = query['search_facets'] c.page.items = query['results'] except SearchError, se: log.error('Dataset search error: %r', se.args) c.query_error = True c.facets = {} c.search_facets = {} c.page = h.Page(collection=[]) c.search_facets_limits = {} for facet in c.search_facets.keys(): try: limit = int(request.params.get('_%s_limit' % facet, g.facets_default_number)) except ValueError: abort(400, _('Parameter "{parameter_name}" is not ' 'an integer').format( parameter_name='_%s_limit' % facet)) c.search_facets_limits[facet] = limit maintain.deprecate_context_item( 'facets', 'Use `c.search_facets` instead.') self._setup_template_variables(context, {}, package_type=package_type) return render(self._search_template(package_type), extra_vars={'dataset_type': package_type})
def _read(self, id, limit, group_type): ''' This is common code used by both read and bulk_process''' context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'schema': self._db_to_form_schema(group_type=group_type), 'for_view': True, 'extras_as_string': True} q = c.q = request.params.get('q', '') # Search within group if c.group_dict.get('is_organization'): q += ' owner_org:"%s"' % c.group_dict.get('id') else: q += ' groups:"%s"' % c.group_dict.get('name') c.description_formatted = h.render_markdown(c.group_dict.get('description')) context['return_query'] = True # c.group_admins is used by CKAN's legacy (Genshi) templates only, # if we drop support for those then we can delete this line. c.group_admins = authz.get_group_or_org_admin_ids(c.group.id) page = self._get_page_number(request.params) # most search operations should reset the page counter: params_nopage = [(k, v) for k, v in request.params.items() if k != 'page'] sort_by = request.params.get('sort', None) def search_url(params): controller = lookup_group_controller(group_type) action = 'bulk_process' if c.action == 'bulk_process' else 'read' url = h.url_for(controller=controller, action=action, id=id) params = [(k, v.encode('utf-8') if isinstance(v, basestring) else str(v)) for k, v in params] return url + u'?' + urlencode(params) def drill_down_url(**by): return h.add_url_param(alternative_url=None, controller='group', action='read', extras=dict(id=c.group_dict.get('name')), new_params=by) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): return h.remove_url_param(key, value=value, replace=replace, controller='group', action='read', extras=dict(id=c.group_dict.get('name'))) c.remove_field = remove_field def pager_url(q=None, page=None): params = list(params_nopage) params.append(('page', page)) return search_url(params) try: c.fields = [] search_extras = {} for (param, value) in request.params.items(): if not param in ['q', 'page', 'sort'] \ and len(value) and not param.startswith('_'): if not param.startswith('ext_'): c.fields.append((param, value)) q += ' %s: "%s"' % (param, value) else: search_extras[param] = value fq = 'capacity:"public"' user_member_of_orgs = [org['id'] for org in h.organizations_available('read')] if (c.group and c.group.id in user_member_of_orgs): fq = '' context['ignore_capacity_check'] = True facets = OrderedDict() default_facet_titles = {'organization': _('Organizations'), 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license_id': _('Licenses')} for facet in g.facets: if facet in default_facet_titles: facets[facet] = default_facet_titles[facet] else: facets[facet] = facet # Facet titles self._update_facet_titles(facets, group_type) if 'capacity' in facets and (group_type != 'organization' or not user_member_of_orgs): del facets['capacity'] c.facet_titles = facets data_dict = { 'q': q, 'fq': fq, 'facet.field': facets.keys(), 'rows': limit, 'sort': sort_by, 'start': (page - 1) * limit, 'extras': search_extras } context_ = dict((k, v) for (k, v) in context.items() if k != 'schema') query = get_action('package_search')(context_, data_dict) c.page = h.Page( collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit ) c.group_dict['package_count'] = query['count'] lang = get_lang()[0] q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.group_id == c.group_dict.get('id'), GroupMultilang.lang == lang).all() if q_results: for result in q_results: c.group_dict[result.field] = result.text if result.field == 'title': c.group_dict['display_name'] = result.text c.facets = query['facets'] maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.search_facets_limits = {} for facet in c.facets.keys(): limit = int(request.params.get('_%s_limit' % facet, g.facets_default_number)) c.search_facets_limits[facet] = limit # MULTILANG - Localizing Organizations display names in Facet list organizations = c.search_facets.get('organization') for org in organizations.get('items'): q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.name == org.get('name'), GroupMultilang.lang == lang).all() if q_results: for result in q_results: if result.field == 'title': org['display_name'] = result.text # MULTILANG - Localizing Groups display names in Facet list groups = c.search_facets.get('groups') for group in groups.get('items'): q_results = model.Session.query(GroupMultilang).filter(GroupMultilang.name == group.get('name'), GroupMultilang.lang == lang).all() if q_results: for result in q_results: if result.field == 'title': group['display_name'] = result.text # MULTILANG - Localizing Tags display names in Facet list tags = c.search_facets.get('tags') for tag in tags.get('items'): localized_tag = TagMultilang.by_name(tag.get('name'), lang) if localized_tag: tag['display_name'] = localized_tag.text c.page.items = query['results'] # MULTILANG - Localizing Datasets names and descriptions in search list log.info(':::::::::::: Retrieving the corresponding localized title and abstract :::::::::::::::') for item in c.page.items: lang = get_lang()[0] q_results = model.Session.query(PackageMultilang).filter(PackageMultilang.package_id == item.get('id'), PackageMultilang.lang == lang).all() if q_results: for result in q_results: item[result.field] = result.text c.sort_by_selected = sort_by except search.SearchError, se: log.error('Group search error: %r', se.args) c.query_error = True c.facets = {} c.page = h.Page(collection=[])
def index(self): try: # package search context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj } data_dict = { 'q': '*:*', 'facet.field': g.facets, 'rows': 4, 'start': 0, 'sort': 'views_recent desc', 'fq': 'capacity:"public"' } query = logic.get_action('package_search')(context, data_dict) c.search_facets = query['search_facets'] c.package_count = query['count'] c.datasets = query['results'] c.facets = query['facets'] maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.facet_titles = { 'organization': _('Organizations'), 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license': _('Licenses'), } except search.SearchError: c.package_count = 0 if c.userobj is not None: msg = None url = h.url_for(controller='user', action='edit') is_google_id = \ c.userobj.name.startswith( 'https://www.google.com/accounts/o8/id') if not c.userobj.email and (is_google_id and not c.userobj.fullname): msg = _(u'Please <a href="{link}">update your profile</a>' u' and add your email address and your full name. ' u'{site} uses your email address' u' if you need to reset your password.'.format( link=url, site=g.site_title)) elif not c.userobj.email: msg = _('Please <a href="%s">update your profile</a>' ' and add your email address. ') % url + \ _('%s uses your email address' ' if you need to reset your password.') \ % g.site_title elif is_google_id and not c.userobj.fullname: msg = _('Please <a href="%s">update your profile</a>' ' and add your full name.') % (url) if msg: h.flash_notice(msg, allow_html=True) return base.render('home/index.html', cache_force=True)
class OrganizationController(GroupController): def source_list(self, id, limit=20): self.group_type = 'organization' context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'schema': self._db_to_form_schema(group_type=self.group_type), 'for_view': True} data_dict = {'id': id} # unicode format (decoded from utf8) q = c.q = request.params.get('q', '') try: c.group_dict = self._action('group_show')(context, data_dict) c.group = context['group'] except p.toolkit.ObjectNotFound: abort(404, p.toolkit._('Group not found')) except p.toolkit.NotAuthorized: abort(401, p.toolkit._('Unauthorized to read group %s') % id) self._read(id, limit, dataset_type=DATASET_TYPE_NAME) return render('source/org_source_list.html') def _read(self, id, limit, dataset_type=None): ''' This is common code used by both read and bulk_process''' self.group_type = 'organization' context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'schema': self._db_to_form_schema(group_type=self.group_type), 'for_view': True, 'extras_as_string': True} q = c.q = request.params.get('q', '') # Search within group if c.group_dict.get('is_organization'): q += ' owner_org: "%s"' % c.group_dict.get('id') else: q += ' groups: "%s"' % c.group_dict.get('name') context['return_query'] = True try: page = int(request.params.get('page', 1)) except ValueError, e: abort(400, ('"page" parameter must be an integer')) # most search operations should reset the page counter: params_nopage = [(k, v) for k, v in request.params.items() if k != 'page'] #sort_by = request.params.get('sort', 'name asc') sort_by = request.params.get('sort', None) def search_url(params): if self.group_type == 'organization': if c.action == 'bulk_process': url = self._url_for(controller='organization', action='bulk_process', id=id) elif c.action == 'source_list': url = self._url_for('harvest_org_list', id=id) else: url = self._url_for(controller='organization', action='read', id=id) else: url = self._url_for(controller='group', action='read', id=id) params = [(k, v.encode('utf-8') if isinstance(v, basestring) else str(v)) for k, v in params] return url + u'?' + urlencode(params) def drill_down_url(**by): return h.add_url_param(alternative_url=None, controller='group', action='read', extras=dict(id=c.group_dict.get('name')), new_params=by) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): return h.remove_url_param(key, value=value, replace=replace, controller='group', action='read', extras=dict(id=c.group_dict.get('name'))) c.remove_field = remove_field def pager_url(q=None, page=None): params = list(params_nopage) params.append(('page', page)) return search_url(params) try: c.fields = [] search_extras = {} for (param, value) in request.params.items(): if not param in ['q', 'page', 'sort'] \ and len(value) and not param.startswith('_'): if not param.startswith('ext_'): c.fields.append((param, value)) q += ' %s: "%s"' % (param, value) else: search_extras[param] = value fq = 'capacity:"public"' user_member_of_orgs = [org['id'] for org in h.organizations_available('read')] if (c.group and c.group.id in user_member_of_orgs): fq = '' context['ignore_capacity_check'] = True facets = OrderedDict() default_facet_titles = {'groups': p.toolkit._('Groups'), 'tags': p.toolkit._('Tags'), 'res_format': p.toolkit._('Formats'), 'license': p.toolkit._('Licence'), } for facet in g.facets: if facet in default_facet_titles: facets[facet] = default_facet_titles[facet] else: facets[facet] = facet if dataset_type: fq = fq + 'dataset_type:"{dataset_type}"'.format(dataset_type=dataset_type) # Facet titles for plugin in p.PluginImplementations(p.IFacets): if self.group_type == 'organization': facets = plugin.organization_facets( facets, self.group_type, dataset_type) else: facets = plugin.group_facets( facets, self.group_type, dataset_type) if 'capacity' in facets and (self.group_type != 'organization' or not user_member_of_orgs): del facets['capacity'] c.facet_titles = facets data_dict = { 'q': q, 'fq': fq, 'facet.field': facets.keys(), 'rows': limit, 'sort': sort_by, 'start': (page - 1) * limit, 'extras': search_extras } query = p.toolkit.get_action('package_search')(context, data_dict) c.page = h.Page( collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit ) c.facets = query['facets'] maintain.deprecate_context_item( 'facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.search_facets_limits = {} for facet in c.facets.keys(): limit = int(request.params.get('_%s_limit' % facet, 10)) c.search_facets_limits[facet] = limit c.page.items = query['results'] c.sort_by_selected = sort_by except search.SearchError, se: log.error('Group search error: %r', se.args) c.query_error = True c.facets = {} c.page = h.Page(collection=[])
class EDCUserController(UserController): def dashboard_unpublished(self): if not c.userobj : abort(401, _('You must be logged-in to access the dashboard.')) user_id = c.userobj.id fq = ' +edc_state:("DRAFT" OR "PENDING PUBLISH" OR "REJECTED")' #Get the list of organizations that this user is the admin if not c.userobj.sysadmin : user_orgs = get_orgs_user_can_edit(c.userobj)#['"' + org + '"' for org in get_orgs_user_can_edit()] #user_orgs = ['"' + org.get('id') + '"' for org in get_user_orgs(user_id, 'admin')] #user_orgs += ['"' + org.get('id') + '"' for org in get_user_orgs(user_id, 'editor')] if len(user_orgs) > 0 : fq += ' +owner_org:(' + ' OR '.join(user_orgs) + ')' self._user_datasets('dashboard_unpublished', c.userobj.id, fq) return render('user/dashboard_unpublished.html') def dashboard_datasets(self): if not c.userobj : abort(401, _('You must be logged-in to access the dashboard.')) fq = ' +author:("%s")' % (c.userobj.id) self._user_datasets('dashboard_datasets', c.userobj.id, fq) return render('user/dashboard_datasets.html') def read(self, id=None): if c.userobj and c.userobj.sysadmin == True: fq = '' else: fq = ' +(edc_state:("PUBLISHED" OR "PENDING ARCHIVE")' if c.userobj: user_id = c.userobj.id user_orgs = get_orgs_user_can_edit(c.userobj)#['"' + org + '"' for org in get_orgs_user_can_edit()] #user_orgs = ['"' + org.get('id') + '"' for org in get_user_orgs(user_id, 'admin')] #user_orgs += ['"' + org.get('id') + '"' for org in get_user_orgs(user_id, 'editor')] if len(user_orgs) > 0: fq += ' OR owner_org:(' + ' OR '.join(user_orgs) + ')' fq += ')' self._user_datasets('read',id, fq) return render('user/read.html') def _user_datasets(self, action, id=None, filter_query=None): from ckan.lib.search import SearchError context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj, 'for_view': True} user_dict = {'id': id, 'user_obj': c.userobj, 'include_datasets': False} # unicode format (decoded from utf8) q = c.q = request.params.get('q', u'') context['return_query'] = True try: page = int(request.params.get('page', 1)) except ValueError, e: abort(400, ('"page" parameter must be an integer')) limit = g.datasets_per_page # most search operations should reset the page counter: params_nopage = [(k, v) for k, v in request.params.items() if k != 'page'] sort_by = request.params.get('sort', None) def search_url(params): base_url = config.get('ckan.site_url') if action == 'dashboard_datasets': url = base_url + '/dashboard/datasets' elif action == 'dashboard_unpublished': url = base_url + '/dashboard/unpublished' elif action == 'read': url = h.url_for(controller='user', action=action, id=id) else : url = h.url_for(controller='user', action=action) params = [(k, v.encode('utf-8') if isinstance(v, basestring) else str(v)) for k, v in params] return url + u'?' + urlencode(params) def drill_down_url(alternative_url=None, **by): return h.add_url_param(alternative_url=alternative_url, controller='user', action=action, extras=dict(id=c.userobj.id), new_params=by) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): return h.remove_url_param(key, value=value, replace=replace, controller='user', action=action, extras=dict(id=c.userobj.id)) c.remove_field = remove_field def pager_url(q=None, page=None): params = list(params_nopage) params.append(('page', page)) return search_url(params) try: c.fields = [] search_extras = {} for (param, value) in request.params.items(): if param not in ['q', 'page', 'sort'] \ and len(value) and not param.startswith('_'): if not param.startswith('ext_'): c.fields.append((param, value)) q += ' %s:"%s"' % (param, value) else: search_extras[param] = value facets = OrderedDict() default_facet_titles = { 'organization': _('Organizations'), 'edc_state': _('States'), 'tags': _('Tags'), 'res_format': _('Formats'), } for facet in default_facet_titles: facets[facet] = default_facet_titles[facet] c.facet_titles = facets fq = filter_query or '' data_dict = { 'q': q, 'fq': fq.strip(), 'facet.field': facets.keys(), 'rows': limit, 'start': (page - 1) * limit, 'sort': sort_by, 'extras': search_extras } query = get_action('package_search')(context, data_dict) c.page = h.Page( collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit ) user_dict['package_count'] = query['count'] c.facets = query['facets'] maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.search_facets_limits = {} for facet in c.facets.keys(): limit = int(request.params.get('_%s_limit' % facet, g.facets_default_number)) c.search_facets_limits[facet] = limit c.page.items = query['results'] c.sort_by_selected = sort_by except SearchError, se: log.error('User search error: %r', se.args) c.query_error = True c.facets = {} c.page = h.Page(collection=[])
'extras': search_extras } query = get_action('package_search')(context, data_dict) c.page = h.Page( collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit ) c.facets = query['facets'] maintain.deprecate_context_item( 'facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.page.items = query['results'] c.sort_by_selected = sort_by except SearchError, se: log.error('Group search error: %r', se.args) c.query_error = True c.facets = {} c.page = h.Page(collection=[]) # Add the group's activity stream (already rendered to HTML) to the # template context for the group/read.html template to retrieve later.
def index(self): try: # package search context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj} data_dict = { 'q': '*:*', 'facet.field': g.facets, 'rows': 4, 'start': 0, 'sort': 'views_recent desc', 'fq': 'capacity:"public"' } query = logic.get_action('package_search')( context, data_dict) c.search_facets = query['search_facets'] c.package_count = query['count'] c.datasets = query['results'] c.facets = query['facets'] maintain.deprecate_context_item( 'facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.facet_titles = { 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license': _('License'), } data_dict = {'sort': 'packages', 'all_fields': 1} # only give the terms to group dictize that are returned in the # facets as full results take a lot longer if 'groups' in c.search_facets: data_dict['groups'] = [ item['name'] for item in c.search_facets['groups']['items'] ] c.groups = logic.get_action('group_list')(context, data_dict) except search.SearchError: c.package_count = 0 c.groups = [] if c.userobj is not None: msg = None url = h.url_for(controller='user', action='edit') is_google_id = \ c.userobj.name.startswith( 'https://www.google.com/accounts/o8/id') if not c.userobj.email and (is_google_id and not c.userobj.fullname): msg = _(u'Please <a href="{link}">update your profile</a>' u' and add your email address and your full name. ' u'{site} uses your email address' u' if you need to reset your password.'.format( link=url, site=g.site_title)) elif not c.userobj.email: msg = _('Please <a href="%s">update your profile</a>' ' and add your email address. ') % url + \ _('%s uses your email address' ' if you need to reset your password.') \ % g.site_title elif is_google_id and not c.userobj.fullname: msg = _('Please <a href="%s">update your profile</a>' ' and add your full name.') % (url) if msg: h.flash_notice(msg, allow_html=True) # START OF DIRTYNESS def get_group(id): def _get_group_type(id): """ Given the id of a group it determines the type of a group given a valid id/name for the group. """ group = model.Group.get(id) if not group: return None return group.type def db_to_form_schema(group_type=None): from ckan.lib.plugins import lookup_group_plugin return lookup_group_plugin(group_type).db_to_form_schema() group_type = _get_group_type(id.split('@')[0]) context = {'model': model, 'session': model.Session, 'ignore_auth': True, 'user': c.user or c.author, 'auth_user_obj': c.userobj, 'schema': db_to_form_schema(group_type=group_type), 'limits': {'packages': 2}, 'for_view': True} data_dict = {'id': id} try: group_dict = logic.get_action('group_show')(context, data_dict) except logic.NotFound: return None return {'group_dict': group_dict} global dirty_cached_group_stuff if not dirty_cached_group_stuff: groups_data = [] groups = config.get('ckan.featured_groups', '').split() for group_name in groups: group = get_group(group_name) if group: groups_data.append(group) if len(groups_data) == 2: break # c.groups is from the solr query above if len(groups_data) < 2 and len(c.groups) > 0: group = get_group(c.groups[0]['name']) if group: groups_data.append(group) if len(groups_data) < 2 and len(c.groups) > 1: group = get_group(c.groups[1]['name']) if group: groups_data.append(group) # We get all the packages or at least too many so # limit it to just 2 for group in groups_data: group['group_dict']['packages'] = \ group['group_dict']['packages'][:2] #now add blanks so we have two while len(groups_data) < 2: groups_data.append({'group_dict': {}}) # cache for later use dirty_cached_group_stuff = groups_data c.group_package_stuff = dirty_cached_group_stuff # END OF DIRTYNESS return base.render('home/index.html', cache_force=True)
class EDCOrganizationController(OrganizationController): def index(self): # FIXME: index copied from GroupController and modified to # show only parent groups context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'for_view': True, 'with_private': False } data_dict = {'all_fields': False} try: self._check_access('site_read', context) except NotAuthorized: abort(401, _('Not authorized to see this page')) # pass user info to context as needed to view private datasets of # orgs correctly if c.userobj: context['user_id'] = c.userobj.id context['user_is_admin'] = c.userobj.sysadmin search_result = self._action('organization_list')(context, data_dict) org_model = context['model'] #Get the list of all groups of type "organization" that have no parents. top_level_orgs = org_model.Group.get_top_level_groups( type="organization") top_results = [ org for org in top_level_orgs if org.name in search_result ] facets = OrderedDict() facets['organization'] = _('Organizations') data_dict = { 'facet.field': facets.keys(), } query = get_action('package_search')(context, data_dict) c.org_pkg_count = query['facets'].get('organization') c.top_orgs_items = top_results return render('organization/index.html') def _read(self, id, limit, group_type='organization'): # FIXME: copied and modified from GroupController to collect # sub organizations, create c.fields_grouped and hard-code # search facets ''' This is common code used by both read and bulk_process''' group_type = self._get_group_type(id.split('@')[0]) context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'schema': self._db_to_form_schema(group_type=group_type), 'for_view': True, 'extras_as_string': True } # Get the subgorgs of this org org_id = c.group_dict.get('id') q = c.q = request.params.get('q', '') # XXX: unfortunate hack, copy sort default behaviour from # before_search because we're using the q parameter below # even when no text query was submitted if not q and request.params.get('sort') in (None, 'rank'): sort_by = 'record_publish_date desc, metadata_modified desc' else: sort_by = request.params.get('sort', None) suborgs = ['"' + org + '"' for org in get_suborgs(org_id)] if suborgs != []: q += ' owner_org:("' + org_id + '" OR ' + ' OR '.join( suborgs) + ')' else: q += ' owner_org:"%s"' % org_id c.description_formatted = h.render_markdown( c.group_dict.get('description')) context['return_query'] = True try: page = int(request.params.get('page', 1)) except ValueError, e: abort(400, ('"page" parameter must be an integer')) # most search operations should reset the page counter: params_nopage = [(k, v) for k, v in request.params.items() if k != 'page'] def search_url(params): action = 'bulk_process' if c.action == 'bulk_process' else 'read' url = h.url_for(controller='organization', action=action, id=id) params = [ (k, v.encode('utf-8') if isinstance(v, basestring) else str(v)) for k, v in params ] return url + u'?' + urlencode(params) def drill_down_url(**by): return h.add_url_param(alternative_url=None, controller='organization', action='read', extras=dict(id=c.group_dict.get('name')), new_params=by) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): return h.remove_url_param(key, value=value, replace=replace, controller='organization', action='read', extras=dict(id=c.group_dict.get('name'))) c.remove_field = remove_field def pager_url(q=None, page=None): params = list(params_nopage) params.append(('page', page)) return search_url(params) try: c.fields = [] search_extras = {} c.fields_grouped = {} for (param, value) in request.params.items(): if not param in ['q', 'page', 'sort'] \ and len(value) and not param.startswith('_'): if not param.startswith('ext_'): c.fields.append((param, value)) q += ' %s: "%s"' % (param, value) if param not in c.fields_grouped: c.fields_grouped[param] = [value] else: c.fields_grouped[param].append(value) else: search_extras[param] = value fq = 'capacity:"public"' user_member_of_orgs = [ org['id'] for org in h.organizations_available('read') ] if (c.group and c.group.id in user_member_of_orgs): fq = '' context['ignore_capacity_check'] = True facets = OrderedDict() default_facet_titles = { 'organization': _('Organizations'), 'edc_state': _('States'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license_id': _('Licenses'), 'type': _('Dataset types') } for facet in default_facet_titles: facets[facet] = default_facet_titles[facet] c.facet_titles = facets data_dict = { 'q': q, 'fq': fq, 'facet.field': facets.keys(), 'rows': limit, 'sort': sort_by, 'start': (page - 1) * limit, 'extras': search_extras } query = get_action('package_search')(context, data_dict) c.page = h.Page(collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit) c.group_dict['package_count'] = query['count'] c.facets = query['facets'] maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.search_facets_limits = {} for facet in c.facets.keys(): limit = int( request.params.get('_%s_limit' % facet, g.facets_default_number)) c.search_facets_limits[facet] = limit c.page.items = query['results'] c.sort_by_selected = sort_by except search.SearchError, se: log.error('Group search error: %r', se.args) c.query_error = True c.facets = {} c.page = h.Page(collection=[])
def _read(self, id, limit, group_type): """ This is common code used by both read and bulk_process""" context = { "model": model, "session": model.Session, "user": c.user, "schema": self._db_to_form_schema(group_type=group_type), "for_view": True, "extras_as_string": True, } q = c.q = request.params.get("q", "") # Search within group if c.group_dict.get("is_organization"): q += ' owner_org:"%s"' % c.group_dict.get("id") else: q += ' groups:"%s"' % c.group_dict.get("name") c.description_formatted = h.render_markdown(c.group_dict.get("description")) context["return_query"] = True page = self._get_page_number(request.params) # most search operations should reset the page counter: params_nopage = [(k, v) for k, v in request.params.items() if k != "page"] sort_by = request.params.get("sort", None) def search_url(params): controller = lookup_group_controller(group_type) action = "bulk_process" if c.action == "bulk_process" else "read" url = h.url_for(controller=controller, action=action, id=id) params = [(k, v.encode("utf-8") if isinstance(v, basestring) else str(v)) for k, v in params] return url + u"?" + urlencode(params) def drill_down_url(**by): return h.add_url_param( alternative_url=None, controller="group", action="read", extras=dict(id=c.group_dict.get("name")), new_params=by, ) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): return h.remove_url_param( key, value=value, replace=replace, controller="group", action="read", extras=dict(id=c.group_dict.get("name")), ) c.remove_field = remove_field def pager_url(q=None, page=None): params = list(params_nopage) params.append(("page", page)) return search_url(params) try: c.fields = [] search_extras = {} for (param, value) in request.params.items(): if not param in ["q", "page", "sort"] and len(value) and not param.startswith("_"): if not param.startswith("ext_"): c.fields.append((param, value)) q += ' %s: "%s"' % (param, value) else: search_extras[param] = value fq = 'capacity:"public"' user_member_of_orgs = [org["id"] for org in h.organizations_available("read")] if c.group and c.group.id in user_member_of_orgs: fq = "" context["ignore_capacity_check"] = True facets = OrderedDict() default_facet_titles = { "organization": _("Organizations"), "groups": _("Groups"), "tags": _("Tags"), "res_format": _("Formats"), "license_id": _("Licenses"), } for facet in g.facets: if facet in default_facet_titles: facets[facet] = default_facet_titles[facet] else: facets[facet] = facet # Facet titles self._update_facet_titles(facets, group_type) if "capacity" in facets and (group_type != "organization" or not user_member_of_orgs): del facets["capacity"] c.facet_titles = facets data_dict = { "q": q, "fq": fq, "facet.field": facets.keys(), "rows": limit, "sort": sort_by, "start": (page - 1) * limit, "extras": search_extras, } context_ = dict((k, v) for (k, v) in context.items() if k != "schema") query = get_action("package_search")(context_, data_dict) c.page = h.Page( collection=query["results"], page=page, url=pager_url, item_count=query["count"], items_per_page=limit ) c.group_dict["package_count"] = query["count"] c.facets = query["facets"] maintain.deprecate_context_item("facets", "Use `c.search_facets` instead.") c.search_facets = query["search_facets"] c.search_facets_limits = {} for facet in c.facets.keys(): limit = int(request.params.get("_%s_limit" % facet, g.facets_default_number)) c.search_facets_limits[facet] = limit c.page.items = query["results"] c.sort_by_selected = sort_by except search.SearchError, se: log.error("Group search error: %r", se.args) c.query_error = True c.facets = {} c.page = h.Page(collection=[])
def _read(self, id, limit, group_type): ''' This is common code used by both read and bulk_process''' context = {'model': model, 'session': model.Session, 'user': c.user, 'schema': self._db_to_form_schema(group_type=group_type), 'for_view': True, 'extras_as_string': True} q = c.q = request.params.get('q', '') # Search within group if c.group_dict.get('is_organization'): q += ' owner_org:"%s"' % c.group_dict.get('id') else: q += ' groups:"%s"' % c.group_dict.get('name') c.description_formatted = \ h.render_markdown(c.group_dict.get('description')) context['return_query'] = True page = h.get_page_number(request.params) # most search operations should reset the page counter: params_nopage = [(k, v) for k, v in request.params.items() if k != 'page'] sort_by = request.params.get('sort', None) def search_url(params): controller = lookup_group_controller(group_type) action = 'bulk_process' if c.action == 'bulk_process' else 'read' url = h.url_for(controller=controller, action=action, id=id) params = [(k, v.encode('utf-8') if isinstance(v, basestring) else str(v)) for k, v in params] return url + u'?' + urlencode(params) def drill_down_url(**by): return h.add_url_param(alternative_url=None, controller='group', action='read', extras=dict(id=c.group_dict.get('name')), new_params=by) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): return h.remove_url_param(key, value=value, replace=replace, controller='group', action='read', extras=dict(id=c.group_dict.get('name'))) c.remove_field = remove_field def pager_url(q=None, page=None): params = list(params_nopage) params.append(('page', page)) return search_url(params) try: c.fields = [] search_extras = {} for (param, value) in request.params.items(): if param not in ['q', 'page', 'sort'] \ and len(value) and not param.startswith('_'): if not param.startswith('ext_'): c.fields.append((param, value)) q += ' %s: "%s"' % (param, value) else: search_extras[param] = value facets = OrderedDict() default_facet_titles = {'organization': _('Organizations'), 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license_id': _('Licenses')} for facet in g.facets: if facet in default_facet_titles: facets[facet] = default_facet_titles[facet] else: facets[facet] = facet # Facet titles self._update_facet_titles(facets, group_type) c.facet_titles = facets data_dict = { 'q': q, 'fq': '', 'include_private': True, 'facet.field': facets.keys(), 'rows': limit, 'sort': sort_by, 'start': (page - 1) * limit, 'extras': search_extras } context_ = dict((k, v) for (k, v) in context.items() if k != 'schema') query = get_action('package_search')(context_, data_dict) c.page = h.Page( collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit ) c.group_dict['package_count'] = query['count'] c.facets = query['facets'] maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.search_facets_limits = {} for facet in c.facets.keys(): limit = int(request.params.get('_%s_limit' % facet, g.facets_default_number)) c.search_facets_limits[facet] = limit c.page.items = query['results'] c.sort_by_selected = sort_by except search.SearchError, se: log.error('Group search error: %r', se.args) c.query_error = True c.facets = {} c.page = h.Page(collection=[])
def index(self): try: # package search context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj } data_dict = { 'q': '*:*', 'facet.field': g.facets, 'rows': 4, 'start': 0, 'sort': 'views_recent desc', 'fq': 'capacity:"public"' } query = logic.get_action('package_search')(context, data_dict) c.search_facets = query['search_facets'] c.package_count = query['count'] c.datasets = query['results'] c.facets = query['facets'] maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.facet_titles = { 'organization': _('Organizations'), 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license': _('Licenses'), } data_dict = {'sort': 'packages', 'all_fields': 1} # only give the terms to group dictize that are returned in the # facets as full results take a lot longer if 'groups' in c.search_facets: data_dict['groups'] = [ item['name'] for item in c.search_facets['groups']['items'] ] c.groups = logic.get_action('group_list')(context, data_dict) except search.SearchError: c.package_count = 0 c.groups = [] if c.userobj is not None: msg = None url = h.url_for(controller='user', action='edit') is_google_id = \ c.userobj.name.startswith( 'https://www.google.com/accounts/o8/id') if not c.userobj.email and (is_google_id and not c.userobj.fullname): msg = _(u'Please <a href="{link}">update your profile</a>' u' and add your email address and your full name. ' u'{site} uses your email address' u' if you need to reset your password.'.format( link=url, site=g.site_title)) elif not c.userobj.email: msg = _('Please <a href="%s">update your profile</a>' ' and add your email address. ') % url + \ _('%s uses your email address' ' if you need to reset your password.') \ % g.site_title elif is_google_id and not c.userobj.fullname: msg = _('Please <a href="%s">update your profile</a>' ' and add your full name.') % (url) if msg: h.flash_notice(msg, allow_html=True) # START OF DIRTYNESS def get_group(id): def _get_group_type(id): """ Given the id of a group it determines the type of a group given a valid id/name for the group. """ group = model.Group.get(id) if not group: return None return group.type def db_to_form_schema(group_type=None): from ckan.lib.plugins import lookup_group_plugin return lookup_group_plugin(group_type).db_to_form_schema() group_type = _get_group_type(id.split('@')[0]) context = { 'model': model, 'session': model.Session, 'ignore_auth': True, 'user': c.user or c.author, 'auth_user_obj': c.userobj, 'schema': db_to_form_schema(group_type=group_type), 'limits': { 'packages': 2 }, 'for_view': True } data_dict = {'id': id} try: group_dict = logic.get_action('group_show')(context, data_dict) except logic.NotFound: return None return {'group_dict': group_dict} global dirty_cached_group_stuff if not dirty_cached_group_stuff: groups_data = [] groups = config.get('ckan.featured_groups', '').split() for group_name in groups: group = get_group(group_name) if group: groups_data.append(group) if len(groups_data) == 2: break # c.groups is from the solr query above if len(groups_data) < 2 and len(c.groups) > 0: group = get_group(c.groups[0]['name']) if group: groups_data.append(group) if len(groups_data) < 2 and len(c.groups) > 1: group = get_group(c.groups[1]['name']) if group: groups_data.append(group) # We get all the packages or at least too many so # limit it to just 2 for group in groups_data: group['group_dict']['packages'] = \ group['group_dict']['packages'][:2] #now add blanks so we have two while len(groups_data) < 2: groups_data.append({'group_dict': {}}) # cache for later use dirty_cached_group_stuff = groups_data c.group_package_stuff = dirty_cached_group_stuff # END OF DIRTYNESS return base.render('home/index.html', cache_force=True)
class GobArPackageController(PackageController): def search(self): package_type = self._guess_package_type() try: context = { 'model': model, 'user': c.user or c.author, 'auth_user_obj': c.userobj } check_access('site_read', context) except NotAuthorized: abort(401, _('Not authorized to see this page')) q = c.q = request.params.get('q', u'') c.query_error = False page = self._get_page_number(request.params) limit = g.datasets_per_page params_nopage = [(k, v) for k, v in request.params.items() if k != 'page'] def drill_down_url(alternative_url=None, **by): return h.add_url_param(alternative_url=alternative_url, controller='package', action='search', new_params=by) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): return h.remove_url_param(key, value=value, replace=replace, controller='package', action='search') c.remove_field = remove_field sort_by = request.params.get('sort', None) params_nosort = [(k, v) for k, v in params_nopage if k != 'sort'] def _sort_by(fields): params = params_nosort[:] if fields: sort_string = ', '.join('%s %s' % f for f in fields) params.append(('sort', sort_string)) return search_url(params, package_type) c.sort_by = _sort_by if not sort_by: c.sort_by_fields = [] else: c.sort_by_fields = [ field.split()[0] for field in sort_by.split(',') ] def pager_url(q=None, page=None): params = list(params_nopage) params.append(('page', page)) return search_url(params, package_type) c.search_url_params = urlencode(_encode_params(params_nopage)) try: c.fields = [] c.fields_grouped = {} search_extras = {} fq = '' for (param, value) in request.params.items(): if param not in ['q', 'page', 'sort'] \ and len(value) and not param.startswith('_'): if not param.startswith('ext_'): c.fields.append((param, value)) if param != 'organization': fq += ' %s:"%s"' % (param, value) else: fq += custom_organization_filter(value) if param not in c.fields_grouped: c.fields_grouped[param] = [value] else: c.fields_grouped[param].append(value) else: search_extras[param] = value context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'for_view': True, 'auth_user_obj': c.userobj } if package_type and package_type != 'dataset': fq += ' +dataset_type:{type}'.format(type=package_type) elif not asbool(config.get('ckan.search.show_all_types', 'False')): fq += ' +dataset_type:dataset' facets = OrderedDict() default_facet_titles = { 'organization': _('Organizations'), 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license_id': _('Licenses'), } for facet in g.facets: if facet in default_facet_titles: facets[facet] = default_facet_titles[facet] else: facets[facet] = facet for plugin in p.PluginImplementations(p.IFacets): facets = plugin.dataset_facets(facets, package_type) c.facet_titles = facets data_dict = { 'q': q, 'fq': fq.strip(), 'facet.field': facets.keys(), 'rows': limit, 'start': (page - 1) * limit, 'sort': sort_by, 'extras': search_extras } query = get_action('package_search')(context, data_dict) c.sort_by_selected = query['sort'] c.page = h.Page(collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit) c.facets = query['facets'] c.search_facets = query['search_facets'] c.page.items = query['results'] except SearchError, se: log.error('Dataset search error: %r', se.args) c.query_error = True c.facets = {} c.search_facets = {} c.page = h.Page(collection=[]) c.search_facets_limits = {} for facet in c.search_facets.keys(): try: if facet != 'organization': limit = int( request.params.get('_%s_limit' % facet, g.facets_default_number)) else: limit = None except ValueError: error_description = _( 'Parameter "{parameter_name}" is not an integer') error_description = error_description.format( parameter_name='_%s_limit' % facet) abort(400, error_description) c.search_facets_limits[facet] = limit maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') self._setup_template_variables(context, {}, package_type=package_type) return render(self._search_template(package_type), extra_vars={'dataset_type': package_type})
def index(self): try: # package search context = {'model': model, 'session': model.Session, 'user': c.user or c.author, 'auth_user_obj': c.userobj} data_dict = { 'q': '*:*', 'facet.field': g.facets, 'rows': 4, 'start': 0, 'sort': 'views_recent desc', 'fq': 'capacity:"public"' } query = logic.get_action('package_search')( context, data_dict) c.search_facets = query['search_facets'] c.package_count = query['count'] c.datasets = query['results'] c.facets = query['facets'] maintain.deprecate_context_item( 'facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.facet_titles = { 'organization': _('Organizations'), 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license': _('Licenses'), } except search.SearchError: c.package_count = 0 if c.userobj is not None: msg = None url = h.url_for(controller='user', action='edit') is_google_id = \ c.userobj.name.startswith( 'https://www.google.com/accounts/o8/id') if not c.userobj.email and (is_google_id and not c.userobj.fullname): msg = _(u'Please <a href="{link}">update your profile</a>' u' and add your email address and your full name. ' u'{site} uses your email address' u' if you need to reset your password.'.format( link=url, site=g.site_title)) elif not c.userobj.email: msg = _('Please <a href="%s">update your profile</a>' ' and add your email address. ') % url + \ _('%s uses your email address' ' if you need to reset your password.') \ % g.site_title elif is_google_id and not c.userobj.fullname: msg = _('Please <a href="%s">update your profile</a>' ' and add your full name.') % (url) if msg: h.flash_notice(msg, allow_html=True) return base.render('home/index.html', cache_force=True)
def _read(self, id, limit): ''' This is common code used by both read and bulk_process''' group_type = self._get_group_type(id.split('@')[0]) context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'schema': self._db_to_form_schema(group_type=group_type), 'for_view': True, 'extras_as_string': True } q = c.q = request.params.get('q', '') # Search within group if c.group_dict.get('is_organization'): q += ' owner_org:"%s"' % c.group_dict.get('id') else: q += ' groups:"%s"' % c.group_dict.get('name') description = c.group_dict.get('description') if description: if group_type == 'group': c.description_formatted = h.render_markdown( description.get(h.lang())) else: c.description_formatted = h.render_markdown(description) context['return_query'] = True # c.group_admins is used by CKAN's legacy (Genshi) templates only, # if we drop support for those then we can delete this line. c.group_admins = new_authz.get_group_or_org_admin_ids(c.group.id) page = self._get_page_number(request.params) # most search operations should reset the page counter: params_nopage = [(k, v) for k, v in request.params.items() if k != 'page'] default_sort_by = 'metadata_modified desc' sort_by = request.params.get('sort', default_sort_by) def search_url(params): if group_type == 'organization': if c.action == 'bulk_process': url = self._url_for(controller='organization', action='bulk_process', id=id) else: url = self._url_for(controller='organization', action='read', id=id) else: url = self._url_for(controller='group', action='read', id=id) params = [ (k, v.encode('utf-8') if isinstance(v, basestring) else str(v)) for k, v in params ] return url + u'?' + urlencode(params) def drill_down_url(**by): return h.add_url_param(alternative_url=None, controller='group', action='read', extras=dict(id=c.group_dict.get('name')), new_params=by) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): return h.remove_url_param(key, value=value, replace=replace, controller='group', action='read', extras=dict(id=c.group_dict.get('name'))) c.remove_field = remove_field def pager_url(q=None, page=None): params = list(params_nopage) params.append(('page', page)) return search_url(params) try: c.fields = [] search_extras = {} for (param, value) in request.params.items(): if not param in ['q', 'page', 'sort'] \ and len(value) and not param.startswith('_'): if not param.startswith('ext_'): c.fields.append((param, value)) q += ' %s: "%s"' % (param, value) else: search_extras[param] = value fq = 'capacity:"public"' user_member_of_orgs = [ org['id'] for org in h.organizations_available('read') ] if (c.group and c.group.id in user_member_of_orgs): fq = '' context['ignore_capacity_check'] = True facets = OrderedDict() default_facet_titles = { 'organization': _('Organizations'), 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license_id': _('Licenses') } for facet in g.facets: if facet in default_facet_titles: facets[facet] = default_facet_titles[facet] else: facets[facet] = facet # Facet titles for plugin in plugins.PluginImplementations(plugins.IFacets): if self.group_type == 'organization': facets = plugin.organization_facets( facets, self.group_type, None) else: facets = plugin.group_facets(facets, self.group_type, None) if 'capacity' in facets and (self.group_type != 'organization' or not user_member_of_orgs): del facets['capacity'] c.facet_titles = facets data_dict = { 'q': q, 'fq': fq, 'facet.field': facets.keys(), 'rows': limit, 'sort': sort_by, 'start': (page - 1) * limit, 'extras': search_extras } context_ = dict( (k, v) for (k, v) in context.items() if k != 'schema') query = get_action('package_search')(context_, data_dict) c.page = h.Page(collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit) c.group_dict['package_count'] = query['count'] c.facets = query['facets'] maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.search_facets_limits = {} for facet in c.search_facets.keys(): limit = int( request.params.get('_%s_limit' % facet, g.facets_default_number)) c.search_facets_limits[facet] = limit c.page.items = query['results'] c.sort_by_selected = sort_by except search.SearchError, se: log.error('Group search error: %r', se.args) c.query_error = True c.facets = {} c.page = h.Page(collection=[])
def _read(self, id, limit, group_type): ''' This is common code used by both read and bulk_process''' context = { 'model': model, 'session': model.Session, 'user': c.user, 'schema': self._db_to_form_schema(group_type=group_type), 'for_view': True, 'extras_as_string': True } q = c.q = request.params.get('q', '') # Search within group if c.group_dict.get('is_organization'): q += ' owner_org:"%s"' % c.group_dict.get('id') else: q += ' groups:"%s"' % c.group_dict.get('name') c.description_formatted = \ h.render_markdown(c.group_dict.get('description')) context['return_query'] = True page = h.get_page_number(request.params) # most search operations should reset the page counter: params_nopage = [(k, v) for k, v in request.params.items() if k != 'page'] sort_by = request.params.get('sort', None) def search_url(params): controller = lookup_group_controller(group_type) action = 'bulk_process' if c.action == 'bulk_process' else 'read' url = h.url_for(controller=controller, action=action, id=id) params = [ (k, v.encode('utf-8') if isinstance(v, basestring) else str(v)) for k, v in params ] return url + u'?' + urlencode(params) def drill_down_url(**by): return h.add_url_param(alternative_url=None, controller='group', action='read', extras=dict(id=c.group_dict.get('name')), new_params=by) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): return h.remove_url_param(key, value=value, replace=replace, controller='group', action='read', extras=dict(id=c.group_dict.get('name'))) c.remove_field = remove_field def pager_url(q=None, page=None): params = list(params_nopage) params.append(('page', page)) return search_url(params) try: c.fields = [] search_extras = {} for (param, value) in request.params.items(): if not param in ['q', 'page', 'sort'] \ and len(value) and not param.startswith('_'): if not param.startswith('ext_'): c.fields.append((param, value)) q += ' %s: "%s"' % (param, value) else: search_extras[param] = value include_private = False user_member_of_orgs = [ org['id'] for org in h.organizations_available('read') ] if (c.group and c.group.id in user_member_of_orgs): include_private = True facets = OrderedDict() default_facet_titles = { 'organization': _('Organizations'), 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license_id': _('Licenses') } for facet in g.facets: if facet in default_facet_titles: facets[facet] = default_facet_titles[facet] else: facets[facet] = facet # Facet titles self._update_facet_titles(facets, group_type) if 'capacity' in facets and (group_type != 'organization' or not user_member_of_orgs): del facets['capacity'] c.facet_titles = facets data_dict = { 'q': q, 'fq': '', 'include_private': include_private, 'facet.field': facets.keys(), 'rows': limit, 'sort': sort_by, 'start': (page - 1) * limit, 'extras': search_extras } context_ = dict( (k, v) for (k, v) in context.items() if k != 'schema') query = get_action('package_search')(context_, data_dict) c.page = h.Page(collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit) c.group_dict['package_count'] = query['count'] c.facets = query['facets'] maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.search_facets_limits = {} for facet in c.facets.keys(): limit = int( request.params.get('_%s_limit' % facet, g.facets_default_number)) c.search_facets_limits[facet] = limit c.page.items = query['results'] c.sort_by_selected = sort_by except search.SearchError, se: log.error('Group search error: %r', se.args) c.query_error = True c.facets = {} c.page = h.Page(collection=[])
query = get_action("package_search")(context, data_dict) c.page = h.Page( collection=query["results"], page=page, url=pager_url, item_count=query["count"], items_per_page=limit ) c.facets = query["facets"] c.search_facets = query["search_facets"] c.page.items = query["results"] except SearchError, se: log.error("Dataset search error: %r", se.args) c.query_error = True c.facets = {} c.page = h.Page(collection=[]) maintain.deprecate_context_item("facets", "Use `c.search_facets` instead.") return render(self._search_template(package_type)) def _content_type_from_extension(self, ext): ct, mu, ext = accept.parse_extension(ext) if not ct: return None, None, None return ct, ext, (NewTextTemplate, MarkupTemplate)[mu] def _content_type_from_accept(self): """ Given a requested format this method determines the content-type to set and the genshi template loader to use in order to render it accurately. TextTemplate must be used for non-xml templates whilst all that are some sort of XML should use MarkupTemplate. """
class GroupController(base.BaseController): group_type = 'group' ## hooks for subclasses def _group_form(self, group_type=None): return lookup_group_plugin(group_type).group_form() def _form_to_db_schema(self, group_type=None): return lookup_group_plugin(group_type).form_to_db_schema() def _db_to_form_schema(self, group_type=None): '''This is an interface to manipulate data from the database into a format suitable for the form (optional)''' return lookup_group_plugin(group_type).db_to_form_schema() def _setup_template_variables(self, context, data_dict, group_type=None): return lookup_group_plugin(group_type).\ setup_template_variables(context, data_dict) def _new_template(self, group_type): return lookup_group_plugin(group_type).new_template() def _index_template(self, group_type): return lookup_group_plugin(group_type).index_template() def _about_template(self, group_type): return lookup_group_plugin(group_type).about_template() def _read_template(self, group_type): return lookup_group_plugin(group_type).read_template() def _history_template(self, group_type): return lookup_group_plugin(group_type).history_template() def _edit_template(self, group_type): return lookup_group_plugin(group_type).edit_template() def _activity_template(self, group_type): return lookup_group_plugin(group_type).activity_template() def _admins_template(self, group_type): return lookup_group_plugin(group_type).admins_template() def _bulk_process_template(self, group_type): return lookup_group_plugin(group_type).bulk_process_template() ## end hooks def _replace_group_org(self, string): ''' substitute organization for group if this is an org''' if self.group_type == 'organization': string = re.sub('^group', 'organization', string) return string def _action(self, action_name): ''' select the correct group/org action ''' return get_action(self._replace_group_org(action_name)) def _check_access(self, action_name, *args, **kw): ''' select the correct group/org check_access ''' return check_access(self._replace_group_org(action_name), *args, **kw) def _render_template(self, template_name): ''' render the correct group/org template ''' return render(self._replace_group_org(template_name)) def _redirect_to(self, *args, **kw): ''' wrapper to ensue the correct controller is used ''' if self.group_type == 'organization' and 'controller' in kw: kw['controller'] = 'organization' return h.redirect_to(*args, **kw) def _url_for(self, *args, **kw): ''' wrapper to ensue the correct controller is used ''' if self.group_type == 'organization' and 'controller' in kw: kw['controller'] = 'organization' return h.url_for(*args, **kw) def _guess_group_type(self, expecting_name=False): """ Guess the type of group from the URL handling the case where there is a prefix on the URL (such as /data/organization) """ parts = [x for x in request.path.split('/') if x] idx = -1 if expecting_name: idx = -2 gt = parts[idx] if gt == 'group': gt = None return gt def index(self): group_type = self._guess_group_type() context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'for_view': True, 'with_private': False } q = c.q = request.params.get('q', '') data_dict = {'all_fields': True, 'q': q} sort_by = c.sort_by_selected = request.params.get('sort') if sort_by: data_dict['sort'] = sort_by try: self._check_access('site_read', context) except NotAuthorized: abort(401, _('Not authorized to see this page')) # pass user info to context as needed to view private datasets of # orgs correctly if c.userobj: context['user_id'] = c.userobj.id context['user_is_admin'] = c.userobj.sysadmin results = self._action('group_list')(context, data_dict) c.amount_group_diplayed = amount_group_displayed c.groups = results[:amount_group_displayed] c.hasmoregroups = len(results) > amount_group_displayed #c.page = h.Page( # collection=results, # page=request.params.get('page', 1), # url=h.pager_url, # items_per_page=21 #) return render(self._index_template(group_type)) def read(self, id, limit=20): if request.GET.get('ext_boolean') in ['all', 'any', 'exact']: base.session['ext_boolean'] = request.GET['ext_boolean'] base.session.save() group_type = self._get_group_type(id.split('@')[0]) if (group_type != self.group_type) and (group_type != "eurovoc_domain"): abort(404, _('Incorrect group type')) context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'schema': self._db_to_form_schema(group_type=group_type), 'for_view': True } # Do not query for the group datasets when dictizing, as they will # be ignored and get requested on the controller anyway data_dict = {'id': id, 'include_datasets': False} # unicode format (decoded from utf8) q = c.q = request.params.get('q', '') try: c.group_dict = self._action('group_show')(context, data_dict) c.group = context['group'] except NotFound: abort(404, _('Group not found')) except NotAuthorized: abort(401, _('Unauthorized to read group %s') % id) self._read(id, limit) return render(self._read_template(c.group_dict['type'])) def _read(self, id, limit): ''' This is common code used by both read and bulk_process''' group_type = self._get_group_type(id.split('@')[0]) context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'schema': self._db_to_form_schema(group_type=group_type), 'for_view': True, 'extras_as_string': True } q = c.q = request.params.get('q', '') # Search within group if c.group_dict.get('is_organization'): q += ' owner_org:"%s"' % c.group_dict.get('id') else: q += ' groups:"%s"' % c.group_dict.get('name') c.description_formatted = h.render_markdown( c.group_dict.get('description')) context['return_query'] = True # c.group_admins is used by CKAN's legacy (Genshi) templates only, # if we drop support for those then we can delete this line. c.group_admins = new_authz.get_group_or_org_admin_ids(c.group.id) try: page = int(request.params.get('page', 1)) except ValueError, e: abort(400, ('"page" parameter must be an integer')) # most search operations should reset the page counter: params_nopage = [(k, v) for k, v in request.params.items() if k != 'page'] new_params_nopage = [] for key, value in params_nopage: if key == 'eurovoc_domains': new_params_nopage.append(('groups', value)) else: new_params_nopage.append((key, value)) params_nopage = new_params_nopage #sort_by = request.params.get('sort', 'name asc') sort_by = request.params.get('sort', None) def search_url(params): if group_type == 'organization': if c.action == 'bulk_process': url = self._url_for(controller='organization', action='bulk_process', id=id) else: url = self._url_for(controller='organization', action='read', id=id) else: url = self._url_for(controller='group', action='read', id=id) params = [ (k, v.encode('utf-8') if isinstance(v, basestring) else str(v)) for k, v in params ] return url + u'?' + urlencode(params) def drill_down_url(**by): return h.add_url_param(alternative_url=None, controller='group', action='read', extras=dict(id=c.group_dict.get('name')), new_params=by) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): if c.group_dict.get('is_organization'): return h.remove_url_param( key, value=value, replace=replace, controller='organization', action='read', extras=dict(id=c.group_dict.get('id'))) else: return h.remove_url_param( key, value=value, replace=replace, controller='group', action='read', extras=dict(id=c.group_dict.get('name'))) c.remove_field = remove_field def pager_url(q=None, page=None): params = list(params_nopage) params.append(('page', page)) return search_url(params) try: c.fields = [] search_extras = {} for (param, value) in request.params.items(): if not param in ['q', 'page', 'sort'] \ and len(value) and not param.startswith('_'): if not param.startswith('ext_'): c.fields.append((param, value)) param = 'groups' if (param == 'eurovoc_domains') else param q += ' %s: "%s"' % (param, value) else: search_extras[param] = value fq = 'capacity:"public"' user_member_of_orgs = [ org['id'] for org in h.organizations_available('read') ] if (c.group and c.group.id in user_member_of_orgs): fq = '' context['ignore_capacity_check'] = True facets = OrderedDict() default_facet_titles = { 'organization': _('Organizations'), 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license_id': _('Licenses') } for facet in g.facets: if facet in default_facet_titles: facets[facet] = default_facet_titles[facet] else: facets[facet] = facet # Facet titles for plugin in plugins.PluginImplementations(plugins.IFacets): if self.group_type == 'organization': facets = plugin.organization_facets( facets, self.group_type, None) else: facets = plugin.group_facets(facets, self.group_type, None) if 'capacity' in facets and (self.group_type != 'organization' or not user_member_of_orgs): del facets['capacity'] c.facet_titles = facets data_dict = { 'q': q, 'fq': fq, 'facet.field': facets.keys(), 'rows': limit, 'sort': sort_by, 'start': (page - 1) * limit, 'extras': search_extras } context_ = dict( (k, v) for (k, v) in context.items() if k != 'schema') query = get_action('package_search')(context_, data_dict) c.search_url_params = urlencode(_encode_params(params_nopage)) c.page = h.Page(collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit) c.group_dict['package_count'] = query['count'] c.facets = query['facets'] maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.search_facets_limits = {} for facet in c.facets.keys(): limit = int( request.params.get('_%s_limit' % facet, g.facets_default_number)) c.search_facets_limits[facet] = limit c.page.items = query['results'] c.sort_by_selected = sort_by except search.SearchError, se: log.error('Group search error: %r', se.args) c.query_error = True c.facets = {} c.page = h.Page(collection=[]) c.search_url_params = ''
class ECODPOrganizationController(OrganizationController): def _url_for(self, *args, **kw): ''' Overwrite to use the publisher link, not organization (as derived from the core's group controller otherwise. ''' if 'controller' in kw: kw['controller'] = 'ckanext.ecportal.controllers.organization:ECODPOrganizationController' return h.url_for(*args, **kw) def index(self, max_element=None): group_type = self._guess_group_type() context = { 'model': model, 'session': model.Session, 'user': c.user, 'for_view': True, 'with_private': False } if c.user: context['with_private'] = True locale = request.environ['CKAN_LANG'] or config.get( 'ckan.locale_default', 'en') start = time.time() result = get_action('get_skos_hierarchy')(context, max_element) duration = time.time() - start factory = ControlledVocabularyFactory() publ_mdr = factory.get_controlled_vocabulary_util( ControlledVocabularyFactory.CORPORATE_BODY ) #type: CorporateBodiesUtil for top_level, item in result.items(): translation = publ_mdr.get_translation_for_language( top_level, locale) item['name'] = top_level.split('/')[-1].lower() item['label'] = translation interim = [] for child in item.get('children', []): translation = publ_mdr.get_translation_for_language( child[0], locale) interim.append( (child[0].split('/')[-1].lower(), translation, child[1])) item['children'] = sorted(interim, key=lambda child: child[1]) log.info("get SKOS hierachy took {0}".format(duration)) c.items = result return render(self._index_template(group_type)) def _read(self, id, limit): ''' This is common code used by both read and bulk_process''' group_type = self._get_group_type(id.split('@')[0]) context = { 'model': model, 'session': model.Session, 'user': c.user or c.author, 'schema': self._db_to_form_schema(group_type=group_type), 'for_view': True, 'extras_as_string': True } q = c.q # = request.params.get('q', '') log.info('Organization read q: %s in organization: %s' % (q, c.group_dict.get('id', ''))) if q != u'': result = '' if request.params.get('ext_boolean') == 'any': result = build_search_for_any_words(q) q = result elif request.params.get('ext_boolean') == 'exact': result = '"%s"' % (q) q = result log.info("%s" % (request.params.get('ext_boolean'))) c.description_formatted = h.render_markdown( c.group_dict.get('description')) context['return_query'] = True # c.group_admins is used by CKAN's legacy (Genshi) templates only, # if we drop support for those then we can delete this line. c.group_admins = new_authz.get_group_or_org_admin_ids(c.group.id) try: page = int(request.params.get('page', 1)) except ValueError, e: abort(400, ('"page" parameter must be an integer')) # most search operations should reset the page counter: params_nopage = [(k, v) for k, v in request.params.items() if k != 'page'] new_params_nopage = [] for key, value in params_nopage: if key == 'eurovoc_domains': new_params_nopage.append(('groups', value)) else: new_params_nopage.append((key, value)) # todo check why it is added # params_nopage = new_params_nopage; # sort_by = request.params.get('sort', 'name asc') sort_by = request.params.get('sort', None) def search_url(params): if group_type == 'organization': if c.action == 'bulk_process': url = self._url_for(controller='organization', action='bulk_process', id=id) else: url = self._url_for(controller='organization', action='read', id=id) else: url = self._url_for(controller='group', action='read', id=id) params = [ (k, v.encode('utf-8') if isinstance(v, basestring) else str(v)) for k, v in params ] return url + u'?' + urlencode(params) def drill_down_url(**by): return h.add_url_param(alternative_url=None, controller='group', action='read', extras=dict(id=c.group_dict.get('name')), new_params=by) c.drill_down_url = drill_down_url def remove_field(key, value=None, replace=None): if c.group_dict.get('is_organization'): # ORGANIZATION_CONTROLLER return h.remove_url_param( key, value=value, replace=replace, controller=ORGANIZATION_CONTROLLER, action='read', extras=dict(id=c.group_dict.get('id'))) else: return h.remove_url_param( key, value=value, replace=replace, controller='group', action='read', extras=dict(id=c.group_dict.get('name'))) c.remove_field = remove_field def pager_url(q=None, page=None): params = list(params_nopage) params.append(('page', page)) return search_url(params) try: c.fields = [] search_extras = {} for (param, value) in request.params.items(): if not param in ['q', 'page', 'sort'] \ and len(value) and not param.startswith('_'): if not param.startswith('ext_'): c.fields.append((param, value)) # todo: facet group eurovoc doamin check why we replace eurovoc_domain with groups # param = 'groups' if (param == 'eurovoc_domains') else param; q += ' %s:"%s"' % (param, value) else: search_extras[param] = value # Search within group add group id herel if c.group_dict.get('is_organization'): q += ' owner_org:"%s"' % c.group_dict.get('id') elif c.group_dict.get('name') not in q: q += ' groups:"%s"' % c.group_dict.get('name') fq = 'capacity:"public"' user_member_of_orgs = [ org['id'] for org in h.organizations_available('read') ] if (c.group and c.group.id in user_member_of_orgs): fq = '' context['ignore_capacity_check'] = True facets = OrderedDict() default_facet_titles = { 'organization': _('Organizations'), 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license_id': _('Licenses') } for facet in g.facets: if facet in default_facet_titles: facets[facet] = default_facet_titles[facet] else: facets[facet] = facet # Facet titles for plugin in plugins.PluginImplementations(plugins.IFacets): if self.group_type == 'organization': facets = plugin.organization_facets( facets, self.group_type, None) else: facets = plugin.group_facets(facets, self.group_type, None) if 'capacity' in facets and (self.group_type != 'organization' or not user_member_of_orgs): del facets['capacity'] c.facet_titles = facets data_dict = { 'q': q, 'fq': fq, 'facet.field': facets.keys(), 'rows': limit, 'sort': sort_by, 'start': (page - 1) * limit, 'extras': search_extras, 'defType': 'edismax' } context_ = dict( (k, v) for (k, v) in context.items() if k != 'schema') query = get_action('package_search')(context_, data_dict) c.search_url_params = urlencode(_encode_params(params_nopage)) c.page = page_util.Page(collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit) c.group_dict['package_count'] = query['count'] c.facets = query['facets'] maintain.deprecate_context_item('facets', 'Use `c.search_facets` instead.') c.search_facets = query['search_facets'] c.search_facets_limits = {} for facet in c.facets.keys(): limit = int( request.params.get('_%s_limit' % facet, g.facets_default_number)) c.search_facets_limits[facet] = limit c.page.items = query['results'] c.sort_by_selected = sort_by except search.SearchError, se: log.error('Organization search error: %r', se.args) c.query_error = True c.facets = {} c.page = page_util.Page(collection=[]) c.search_url_params = ''