def update_template_ctx_after_query(query_res,
                                    page_limit=20,
                                    facets_default_number=10):
    c.page = page_util.Page(collection=query_res['results'],
                            page=c.num_page,
                            url=pager_url,
                            item_count=query_res['count'],
                            items_per_page=page_limit)
    c.page.items = query_res['results']
    # Compute limit for displaying facets #
    c.facets = query_res['facets']
    c.search_facets = query_res['search_facets']
    c.search_facets_limits = {}
    for facet in c.facets.keys():
        try:
            limit = int(
                request.params.get('_%s_limit' % facet, facets_default_number))
            c.search_facets_limits[facet] = limit
        except ValueError:
            abort(
                400,
                _('Parameter "{param}" is not an integer').format(
                    param='_%s_limit' % facet))
Exemple #2
0
class ECPortalUserController(core_user.UserController):
    def logged_in(self):
        #override the logged_in method after the access was granted or denied
        #if login was successfull check the group membership
        if c.user:
            util.add_missing_user_to_all_groups()
        return super(ECPortalUserController, self).logged_in()

    def login_user(self, tup):
        if tup:
            user = tup[0]

            user_object = model.User.get(user)
            if not user_object:
                user_object = model.User.get('api')
                c.userobj = user_object
                c.user = '******'

                try:
                    # Create user
                    user_attributes = tup[1]
                    user_name = user
                    user_email = user_attributes["email"].text
                    user_password = "******"
                    user_fullname = user_attributes[
                        "firstName"].text + " " + user_attributes[
                            "lastName"].text

                    h.get_action(
                        'user_create', {
                            "name": user_name,
                            "email": user_email,
                            "password": user_password,
                            "fullname": user_fullname
                        })

                    rememberer = request.environ['repoze.who.plugins'][
                        'friendlyform']
                    identity = {'repoze.who.userid': user}
                    response.headerlist += rememberer.remember(
                        request.environ, identity)
                except logic.ActionError:
                    base.abort(401, _('Error while creating new user'))
            else:
                c.userobj = user_object
                c.user = user

                rememberer = request.environ['repoze.who.plugins'][
                    'friendlyform']
                identity = {'repoze.who.userid': user}
                response.headerlist += rememberer.remember(
                    request.environ, identity)

            self.logged_in()

    def read(self, id=None):
        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'auth_user_obj': c.userobj,
            'for_view': True,
            'return_minimal': True,
            'save': 'save' in request.params
        }
        data_dict = {'id': id, 'user_obj': c.userobj}

        context['with_related'] = True

        self._setup_user_template_variables(context, data_dict)

        if context['save']:
            request.POST.pop('save')
            data = ecportal_logic.transform_to_data_dict(request.POST)

            credential_validator = Validator({
                'contact_mailbox': [email]
                #'contact_phone_number': [IntPhoneNumberRule]
            })
            errors = {}
            succeed = credential_validator.validate(data, results=errors)

            if succeed is True:
                contact_info = Package_contact_info.get_by_user(c.userobj.id)
                if not contact_info:
                    contact_info = Package_contact_info(c.userobj.id)

                contact_info.from_dict(data)
                contact_info.save()
                h.flash_success(_('ecodp.user.save.success'))
            elif errors:
                h.flash_error(_('ecodp.user.save.error'))

            c.user_dict.update(data)

            c.errors = errors

        # The legacy templates have the user's activity stream on the user
        # profile page, new templates do not.
        if h.asbool(config.get('ckan.legacy_templates', False)):
            c.user_activity_stream = get_action('user_activity_list_html')(
                context, {
                    'id': c.user_dict['id']
                })

        #vars = {'data': {}, 'errors': {}, 'error_summary': {}}
        #c.form = base.render('user/edit_contact_info_form.html', extra_vars=vars)
        return base.render('user/read.html')

    def read_contact_info(self, id=None):
        context = {
            'model': model,
            'session': model.Session,
            'user': c.user or c.author,
            'auth_user_obj': c.userobj,
            'for_view': True,
            'return_minimal': True,
            'save': 'save' in request.params
        }
        data_dict = {'id': id, 'user_obj': c.userobj}

        context['with_related'] = True

        self._setup_user_template_variables(context, data_dict)

        if context['save']:
            request.POST.pop('save')
            data = ecportal_logic.transform_to_data_dict(request.POST)

            credential_validator = Validator({
                'contact_mailbox': [email]
                #'contact_phone_number': [IntPhoneNumberRule]
            })
            errors = {}
            succeed = credential_validator.validate(data, results=errors)

            if succeed is True:
                contact_info = Package_contact_info.get_by_user(c.userobj.id)
                if not contact_info:
                    contact_info = Package_contact_info(c.userobj.id)

                contact_info.from_dict(data)
                contact_info.save()
                h.flash_success(_('ecodp.user.save.success'))
            elif errors:
                h.flash_error(_('ecodp.user.save.error'))

            c.user_dict.update(data)

            c.errors = errors

        # The legacy templates have the user's activity stream on the user
        # profile page, new templates do not.
        if h.asbool(config.get('ckan.legacy_templates', False)):
            c.user_activity_stream = get_action('user_activity_list_html')(
                context, {
                    'id': c.user_dict['id']
                })

        #vars = {'data': {}, 'errors': {}, 'error_summary': {}}
        #c.form = base.render('user/edit_contact_info_form.html', extra_vars=vars)
        return base.render('user/read_contact_info.html')

    def dashboard(self):
        def query_for_datasets():
            query = ''

            if not new_authz.is_sysadmin(c.user):
                organisation_ids = self.__get_users_organizations_ids(c.user)

                if not organisation_ids:
                    # return an empty query result with the needed (i.e.: accessed later) elements
                    return {'results': [], 'count': 0}

                query = build_owner_filter(organisation_ids)

            if c.q != u'':
                search = c.q.strip().split(' ')
                result = ''
                if request.params.get('ext_boolean') == 'any':
                    result = build_search_for_any_words(search)
                elif request.params.get('ext_boolean') == 'all':
                    result = build_search_for_all_words(search)
                elif request.params.get('ext_boolean') == 'exact':
                    result = '("%s")' % (c.q)
                    log.info("%s" % (request.params.get('ext_boolean')))

                query = query + " + title:" + result

            return query

        log.debug("Entering MDT dashboard")

        c.is_sysadmin = new_authz.is_sysadmin(c.user)

        context = {
            'for_view': True,
            'user': c.user or c.author,
            'auth_user_obj': c.userobj,
            'return_minimal': True,
            'ignore_capacity_check': True  # Display also private datasets
        }
        user_data = {"id": c.userobj.id, 'user_obj': c.userobj}

        try:
            user_dict = logic.get_action('user_show')(context, user_data)
        except logic.NotFound:
            base.abort(404, _('User not found'))
        except logic.NotAuthorized:
            base.abort(401, _('Not authorized to see this page'))

        c.user_dict = user_dict
        c.is_myself = user_dict['name'] == c.user
        c.about_formatted = h.render_markdown(user_dict['about'])
        c.search_choice = request.params.get('ext_boolean')

        # Save the used search paramters in a proper format to the exchange variable
        user = user_dict['name']

        log.debug("Start getting the datasets")
        # ---------------------------------------------------------------------------------------------------------------
        # Get the datasets
        from ckan.lib.search import SearchError

        package_type = self._guess_package_type()

        if request.GET.get('ext_boolean') in ['all', 'any', 'exact']:
            session['ext_boolean'] = request.GET['ext_boolean']
            session.save()

        # unicode format (decoded from utf8)
        c.q = request.params.get('q', u'')
        c.query_error = False

        q = query_for_datasets()

        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']

        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

        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=
                'ckanext.ecportal.controllers.user:ECPortalUserController',
                action='dashboard')

        c.remove_field = remove_field

        sort_by = request.params.get('sort', 'views_total desc')
        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(
            core_package._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 = ''

            # if request.params.get('private'):
            #    fq = 'private: %s' % request.params.get('private')

            for (param, value) in request.params.items():
                if param not in ['q', 'page', 'sort', 'id'] \
                        and len(value) and not param.startswith('_'):
                    if not param.startswith('ext_'):
                        c.fields.append((param, value))
                        # paramFQ = 'groups' if (param == 'eurovoc_domains') else param;
                        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

                        # 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'),
            }

            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.dashboard_facets(facets, package_type)

            c.facet_titles = facets

            facet_fields = facets.keys()
            facet_fields.append('private')
            data_dict = {
                'q': q,
                'fq': fq.strip(),
                'facet.field': facet_fields,
                'rows': limit,
                'start': (page - 1) * limit,
                'sort': sort_by,
                'extras': search_extras
            }

            if sort_by == 'modified_date desc':
                # This is the customized part for ODP-570
                # add the group parameter to the solr query
                data_dict['group'] = 'true'
                data_dict['group.query'] = [
                    '-organization:estat AND -organization:comp AND -organization:grow',
                    'organization:estat', 'organization:comp',
                    'organization:grow'
                ]
                data_dict['group.format'] = 'simple'
                data_dict['rows'] = 2147483646

                query = get_action('custom_package_search')(context, data_dict)

                cached_result = []

                for name, group in query['groups'].iteritems():
                    cached_result += group['doclist']['docs']

                start = (page - 1) * limit
                result_list = customsearch.check_solr_result(
                    context, cached_result[start:], limit)

            else:
                query = get_action('package_search')(context, data_dict)
                result_list = query['results']

            c.sort_by_selected = query['sort']

            c.page = page_util.Page(collection=result_list,
                                    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 = result_list
        except SearchError, se:
            log.error('Dataset search error: %r', se.args)
            c.query_error = True
            c.facets = {}
            c.search_facets = {}
            c.page = page_util.Page(collection=[])
Exemple #3
0
class ECODPGroupController(GroupController):


    def index(self):
        start = time.time()
        group_type = self._guess_group_type()
        language = tk.request.environ['CKAN_LANG'] or config.get('ckan.locale_default', 'en')
        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_displayed = amount_group_displayed

        c.groups = results[:amount_group_displayed]
        c.hasmoregroups = len(results) > amount_group_displayed
        c.themes = self._action('theme_list')(context, {})
        c.catalogs = CatalogDcatApOp.get_ui_list_catalogs(config.get('ckan.locale_default', 'en'))
        c.amount_catalog_displayed = amount_catalog_displayed

        #c.page = h.Page(
        #    collection=results,
        #    page=request.params.get('page', 1),
        #    url=h.pager_url,
        #    items_per_page=21
        #)
        # @cache.cache('cached_render', expire=3600)
        def cached_render(user, languge, group_type):
            _render = base.render(group_type)
            return _render
        start_render = time.time()
        _render = cached_render(context.get('user'), language, self._index_template(group_type))

        duration_render= time.time() - start_render
        log.info("Duration index  render. {0}".format(duration_render))
        duration = time.time() - start
        log.info("Duration index. {0}".format(duration))
        return _render

    def read(self, id, limit=20):

        start = time.time()
        if request.GET.get('ext_boolean') in ['all', 'any', 'exact']:
            base.session['ext_boolean'] = request.GET['ext_boolean']
            base.session.save()
        language = tk.request.environ['CKAN_LANG'] or config.get('ckan.locale_default', 'en')

        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:
            stratqs = time.time()
            if 'eurovoc_domain' in data_dict.get('id','') :
                raise NotFound('EurovocDomains are not available any more')
            result = self._action('group_show_read')(context, data_dict)
            c.group_dict = result.get('group_dict', None)
            context["group"] = result.get('group')
            durationgs = time.time() - stratqs
            log.info("Duration group show read. {0}".format(durationgs))
            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)

        start_render = time.time()

        # @cache.cache('render_cached_read', expire=3600)
        def render_cached(user, id, language,  group_type):
            _render = base.render(self._read_template(c.group_dict['type']))
            return _render

        _render = render_cached(context.get('user'), id, language, c.group_dict['type'])
        duration_render = time.time() - start_render



        # _render = base.render(self._read_template(c.group_dict['type']))
        duration = time.time() - start
        log.info("Duration read_group. {0}".format(duration))
        return _render

    def _read(self, id, limit=20):
        import  time
        language = tk.request.environ['CKAN_LANG'] or config.get('ckan.locale_default', 'en')
        start = time.time()
        ''' 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 q != u'':
            qTab = q.split(' ')
            checkbox = request.params.get('ext_boolean')
            if checkbox == 'all':
                q = '(' + ' AND '.join(qTab) + ')'
            elif checkbox == 'any':
                q = '(' + ' OR '.join(qTab) + ')'
            else: #checkbox == 'exact'
                q = '"' + q + '"'

        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)

        @cache.cache('search_url', expire=3600)
        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)

        @cache.cache('drill_down_url', expire=3600)
        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 = 'eurovoc_domains' if (param == 'eurovoc_domains') else param;
                        q += ' %s:"%s"' % (param, value)
                    else:
                        search_extras[param] = value

            fq = ''
            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 = 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'
            }

            active_cache = config.get('ckan.cache.active', 'false')

            context_ = dict((k, v) for (k, v) in context.items() if k != 'schema')

            has_result = False
            dict_as_pickle = None

            if active_cache == 'true':
                dict_as_pickle = pickle.dumps(data_dict)
                query_json = redis.get_from_cache(dict_as_pickle, pool=redis.MISC_POOL)
                if query_json:
                    query = pickle.loads(query_json)
                    has_result = True

            if has_result == False:
                query = get_action('package_search')(context_, data_dict)
                if active_cache == 'true':
                    redis.set_value_in_cache(dict_as_pickle, pickle.dumps(query), pool=redis.MISC_POOL)


            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
            duration = time.time() - start
            log.info("Duration _read. {0}".format(duration))


        except search.SearchError, se:
            log.error('Group search error: %r', se.args)
            c.query_error = True
            c.facets = {}
            c.page = page_util.Page(collection=[])
            c.search_url_params = ''
Exemple #4
0
            c.sort_by_selected = sort_by
            duration = time.time() - start
            log.info("Duration _read. {0}".format(duration))


        except search.SearchError, se:
            log.error('Group search error: %r', se.args)
            c.query_error = True
            c.facets = {}
            c.page = page_util.Page(collection=[])
            c.search_url_params = ''
        except ValueError, se:
            log.error('Group search error: %r', se.args)
            c.query_error = True
            c.facets = {}
            c.page = page_util.Page(collection=[])
            c.search_url_params = ''

        self._setup_template_variables(context, {'id':id},
            group_type=group_type)

    def edit(self, id, data=None, errors=None, error_summary=None):
        import ckan.lib.base as base
        group_type = self._get_group_type(id.split('@')[0])
        context = {'model': model, 'session': model.Session,
                   'user': c.user or c.author,
                   'save': 'save' in request.params,
                   'for_edit': True,
                   'parent': request.params.get('parent', None)
                   }
        data_dict = {'id': id}
def treat_search_error_exception(error_msg, se):
    log.error(error_msg + ': %r', se.args)
    c.query_error = True
    c.facets = {}
    c.page = page_util.Page(collection=[])
    c.search_url_params = ''
Exemple #6
0
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 = ''