Exemple #1
0
    def before_search(self, search_params):
        ''' If include children selected the query string is modified '''
        def _children_name_list(children):
            name_list = []
            for child in children:
                name = child.get('name', "")
                name_list += [name] + _children_name_list(
                    child.get('children', []))
            return name_list

        query = search_params.get('q', None)
        c.include_children_selected = False

        # fix the issues with multiple times repeated fields
        # remove the param from the fields
        new_fields = set()
        for field, value in c.fields:
            if (field != 'include_children'):
                new_fields.add((field, value))
        c.fields = list(new_fields)

        # parse the query string to check if children are requested
        if query:
            base_query = []
            #  remove whitespaces between fields and values
            query = re.sub(': +', ':', query)
            for item in query.split(' '):
                field = item.split(':')[0]
                value = item.split(':')[-1]
                # skip organization
                if (field == 'owner_org'):
                    org_id = value
                    continue
                # skip include children andset option value
                if (field == 'include_children'):
                    if (value.upper() != "FALSE"):
                        c.include_children_selected = True
                    continue
                base_query += [item]
        if c.include_children_selected:
            # add all the children organizations in an 'or' join
            children = _children_name_list(
                helpers.group_tree_section(c.group_dict.get('id'),
                                           include_parents=False,
                                           include_siblings=False).get(
                                               'children', []))
            if (children):
                search_params['q'] = " ".join(base_query)
                if (len(search_params['q'].strip()) > 0):
                    search_params['q'] += ' AND '
                search_params['q'] += '(organization:%s' % c.group_dict.get(
                    'name')
                for name in children:
                    if name:
                        search_params['q'] += ' OR organization:%s' % name
                search_params['q'] += ")"
            # add it back to fields
            c.fields += [('include_children', 'True')]

        return search_params
Exemple #2
0
    def before_search(self, search_params):
        if toolkit.c.controller == 'ckanext.unhcr.controllers.extended_organization:ExtendedOrganizationController':
            toolkit.c.include_children_selected = True

            # helper function
            def _children_name_list(children):
                name_list = []
                for child in children:
                    name = child.get('name', "")
                    name_list += [name] + _children_name_list(
                        child.get('children', []))
                return name_list

            # update filter query
            children = _children_name_list(
                group_tree_section(toolkit.c.id,
                                   type_='data-container',
                                   include_parents=False,
                                   include_siblings=False).get('children', []))
            if children:
                search_params['fq'] = 'organization:%s' % toolkit.c.id
                for name in children:
                    if name:
                        search_params['fq'] += ' OR organization:%s' % name

        return search_params
    def before_search(self, search_params):

        ''' If include children selected the query string is modified '''

        def _children_name_list(children):
            name_list = []
            for child in children:
                name = child.get('name', "")
                name_list += [name] + _children_name_list(child.get('children', []))
            return name_list

        query = search_params.get('q', None)
        c.include_children_selected = False

        # fix the issues with multiple times repeated fields
        # remove the param from the fields
        new_fields = set()
        for field,value in c.fields:
            if (field != 'include_children'):
                new_fields.add((field,value))
        c.fields = list(new_fields)

        # parse the query string to check if children are requested
        if query:
            base_query = []
            #  remove whitespaces between fields and values
            query = re.sub(': +', ':',  query)
            for item in query.split(' '):
                field = item.split(':')[0]
                value = item.split(':')[-1]
                # skip organization 
                if (field == 'owner_org'):
                    org_id = value
                    continue
                # skip include children andset option value
                if (field == 'include_children'):
                    if (value.upper() != "FALSE"):
                        c.include_children_selected = True
                    continue
                base_query += [item]
        if c.include_children_selected:
            # add all the children organizations in an 'or' join
            children = _children_name_list(helpers.group_tree_section(c.group_dict.get('id'), include_parents=False, include_siblings=False).get('children',[]))
            if(children):
                search_params['q'] = " ".join(base_query)
                if (len(search_params['q'].strip())>0):
                    search_params['q'] += ' AND '
                search_params['q'] += '(organization:%s' % c.group_dict.get('name')
                for name in children:
                    if name:
                        search_params['q'] += ' OR organization:%s' %  name
                search_params['q'] += ")"
            # add it back to fields
            c.fields += [('include_children','True')]

        return search_params
def envidat_theme_get_children_packages(organization, count=2):

    def _children_name_list(children):
        name_list = []
        for child in children:
            name = child.get('name', "")
            name_list += [name] + _children_name_list(child.get('children', []))
        return name_list

    packages = organization.get('packages', [])
    children_organizations = _children_name_list(helpers.group_tree_section(organization['id'], include_parents=False, include_siblings=False).get('children',[]))
    for children in children_organizations:
        packages += p.toolkit.get_action('organization_show')({},{'id':children, 'include_datasets':True}).get('packages', [])
    return packages[:count]
Exemple #5
0
    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))
                        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 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
        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=[])