def _define_new_admin_member(group_id): """ A data-container require a fallback admin. Check if this group has a parent and define its admin If container has no parent then pick one from sysadmins. """ data_container = toolkit.get_action('organization_show')( { 'ignore_auth': True }, { 'id': group_id, 'include_groups': True }) if len(data_container['groups']) > 0: for parent in data_container['groups']: if 'id' not in parent: parent = toolkit.get_action('organization_show')( { 'ignore_auth': True }, { 'id': parent['name'] }) admins = get_group_or_org_admin_ids(parent['id']) if len(admins) > 0: admin = model.User.get(admins[0]) return admin return get_random_sysadmin()
def review(self, id): """ sends review notification to all journal admins """ context = self._context() try: tk.check_access('package_update', context, {'id': id}) except tk.NotAuthorized: tk.abort(403, 'Unauthorized') c.pkg_dict = tk.get_action('package_show')(context, {'id': id}) # avoid multiple notifications (eg. when someone calls review directly) if c.pkg_dict.get('dara_edawax_review', 'false') == 'true': h.flash_error("Package has already been sent to review") redirect(id) user_name = tk.c.userobj.fullname or tk.c.userobj.email admins = get_group_or_org_admin_ids(c.pkg_dict['owner_org']) addresses = map(lambda admin_id: model.User.get(admin_id).email, admins) note = n.review(addresses, user_name, id) if note: c.pkg_dict['dara_edawax_review'] = 'true' tk.get_action('package_update')(context, c.pkg_dict) h.flash_success('Notification to Editors sent.') else: h.flash_error( 'ERROR: Mail could not be sent. Please try again later or contact the site admin.' ) redirect(id)
def review(self, id): """ sends review notification to all journal admins """ context = self._context() try: tk.check_access('package_update', context, {'id': id}) except tk.NotAuthorized: tk.abort(403, 'Unauthorized') c.pkg_dict = tk.get_action('package_show')(context, {'id': id}) # avoid multiple notifications (eg. when someone calls review directly) if c.pkg_dict.get('dara_edawax_review', 'false') == 'true': h.flash_error("Package has already been sent to review") redirect(id) user_name = tk.c.userobj.fullname or tk.c.userobj.email admins = get_group_or_org_admin_ids(c.pkg_dict['owner_org']) addresses = map(lambda admin_id: model.User.get(admin_id).email, admins) note = n.review(addresses, user_name, id) if note: c.pkg_dict['dara_edawax_review'] = 'true' tk.get_action('package_update')(context, c.pkg_dict) h.flash_success('Notification to Editors sent.') else: h.flash_error('ERROR: Mail could not be sent. Please try again later or contact the site admin.') redirect(id)
def get_user_admins(user_id): """ get containers admins for user datasets """ datasets = get_user_packages(user_id) user_admins = [] for dataset in datasets: for admin in get_group_or_org_admin_ids(dataset.owner_org): if admin not in user_admins: user_admins.append(admin) return user_admins
def admins(id, group_type, is_organization): extra_vars = {} set_org(is_organization) group_dict = _get_group_dict(id, group_type) admins = authz.get_group_or_org_admin_ids(id) # TODO: Remove g.group_dict = group_dict g.admins = admins extra_vars = { u"group_dict": group_dict, u'group_type': group_type, u"admins": admins } return base.render(_admins_template(group_dict['type']), extra_vars)
def is_admin(user, org): """ check if user is admin params: user: name or id, org : name or id, rtype: boolean """ if not org or not user: return False username = model.User.get(user).name user_id = model.User.get(user).id if authz.is_sysadmin(username): return True if org: org_id = model.Group.get(org).id admin_ids = authz.get_group_or_org_admin_ids(org_id) if user_id in admin_ids: return True return False
def admins(id, group_type, is_organization): extra_vars = {} set_org(is_organization) group_dict = _get_group_dict(id, group_type) admins = authz.get_group_or_org_admin_ids(id) # TODO: Remove # ckan 2.9: Adding variables that were removed from c object for # compatibility with templates in existing extensions g.group_dict = group_dict g.admins = admins extra_vars = { u"group_dict": group_dict, u'group_type': group_type, u"admins": admins } return base.render(_admins_template(group_dict['type']), extra_vars)
def admins(self, id): c.group_dict = self._get_group_dict(id) c.admins = authz.get_group_or_org_admin_ids(id) return render(self._admins_template(c.group_dict['type']))
def admins(self, id): group_type = self._ensure_controller_matches_group_type(id) c.group_dict = self._get_group_dict(id) c.admins = authz.get_group_or_org_admin_ids(id) return render(self._admins_template(c.group_dict['type']), extra_vars={'group_type': group_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'] 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 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 _read(self, id, limit, group_type): # noqa c.include_children_selected = False if not c.group_dict.get("is_organization"): return """ 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, } 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: q = c.q = request.params.get("q", "") fq = c.fq = request.params.get("fq", "") 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 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 # filter by organization with fq (filter query) c.include_children_selected = True children = _children_name_list( hierarchy_helpers.group_tree_section( c.group_dict.get("id"), include_parents=False, include_siblings=False ).get("children", []) ) if not children: fq += ' organization:"%s"' % c.group_dict.get("name") else: fq += ' organization:("%s"' % c.group_dict.get("name") for name in children: if name: fq += ' OR "%s"' % name fq += ")" 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): # noqa """ This controller replaces the HierarchyOrganizationController controller from ckanext-hierarchy. It makes sure, that datasets of sub-organizations are included on the organisation detail page and uses the filter query (fq) parameter of Solr to limit the search instead of the owner_org query used in CKAN core. In order to replace HierarchyOrganizationController it's important to load the ogdch_org_search plugin _after_ the hierarchy_display plugin in the plugin list of the active ini file. Unfortunately there are no clean extension points in the OrganizationController, so that the _read() method had to be overridden completely. """ c.include_children_selected = False if not c.group_dict.get('is_organization'): return ''' 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 } 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 = 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: q = c.q = request.params.get('q', '') fq = c.fq = request.params.get('fq', '') 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)) fq += ' %s: "%s"' % (param, value) else: search_extras[param] = value 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): context['ignore_capacity_check'] = True else: fq += ' capacity:"public"' facets = OrderedDict() default_facet_titles = { 'organization': _('Organizations'), 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license_id': _('Licenses') } for facet in h.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 # filter by organization with fq (filter query) c.include_children_selected = True children = _children_name_list( hierarchy_helpers.group_tree_section( c.group_dict.get('id'), include_parents=False, include_siblings=False).get('children', [])) if not children: fq += ' organization:"%s"' % c.group_dict.get('name') else: fq += ' organization:("%s"' % c.group_dict.get('name') for name in children: if name: fq += ' OR "%s"' % name fq += ")" data_dict = { 'q': q, 'fq': fq, 'facet.field': facets.keys(), 'rows': limit, 'sort': sort_by, 'start': (page - 1) * limit, 'extras': search_extras, 'include_private': True, } 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.search_facets = query['search_facets'] c.search_facets_limits = {} for facet in c.search_facets.keys(): limit = int( request.params.get('_%s_limit' % facet, config.get('search.facets.default', 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.search_facets = {} c.page = h.Page(collection=[])
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 = 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=[])
def _read(self, id, limit, group_type): # noqa """ This controller replaces the GroupController controller from CKAN. It uses the filter query (fq) parameter to query facets in Solr instead of the query parameter (q). If the query parameter is used the search always returns "no results found" when combining a search term with facets. Unfortunately there are no clean extension points in the GroupController, so that the _read() method had to be overridden completely. 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'): fq = 'owner_org:"%s"' % c.group_dict.get('id') else: fq = '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 = 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, string_types) 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): controller = lookup_group_controller(group_type) return h.remove_url_param(key, value=value, replace=replace, controller=controller, 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 = [] c.fields_grouped = {} 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)) 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 facets = OrderedDict() default_facet_titles = {'organization': _('Organizations'), 'groups': _('Groups'), 'tags': _('Tags'), 'res_format': _('Formats'), 'license_id': _('Licenses')} for facet in h.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': 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.search_facets = query['search_facets'] c.search_facets_limits = {} for facet in c.search_facets.keys(): limit = int(request.params.get('_%s_limit' % facet, config.get( 'search.facets.default', 10))) c.search_facets_limits[facet] = limit c.page.items = query['results'] c.sort_by_selected = sort_by except search.SearchError as se: log.error('Group search error: %r', se.args) c.query_error = True c.search_facets = {} c.page = h.Page(collection=[]) self._setup_template_variables( context, {'id': id}, group_type=group_type )