# encoding: utf-8 import re import ckan.controllers.group as group import ckan.plugins as plugins import logging import datetime from urllib import urlencode from pylons.i18n import get_lang import ckan.lib.base as base import ckan.lib.helpers as h import ckan.lib.maintain as maintain import ckan.lib.navl.dictization_functions as dict_fns import ckan.logic as logic import ckan.lib.search as search import ckan.model as model import ckan.authz as authz import ckan.lib.plugins import ckan.plugins as plugins from ckan.common import OrderedDict, c, g, request, _ ''' Created by Francesco Mangiacrapa, see: #8964 ''' class OrganizationVREController(group.GroupController): ''' The organization controller is for Organizations, which are implemented as Groups with is_organization=True and group_type='organization'. It works the same as the group controller apart from: * templates and logic action/auth functions are sometimes customized (switched using _replace_group_org) * 'bulk_process' action only works for organizations Nearly all the code for both is in the GroupController (for historical reasons). ''' group_types = ['organization'] def _guess_group_type(self, expecting_name=False): return 'organization' def _replace_group_org(self, string): ''' substitute organization for group if this is an org''' return re.sub('^group', 'organization', string) def _update_facet_titles(self, facets, group_type): for plugin in plugins.PluginImplementations(plugins.IFacets): facets = plugin.organization_facets( facets, group_type, None) def index(self): group_type = self._guess_group_type() page = h.get_page_number(request.params) or 1 items_per_page = 21 context = {'model': model, 'session': model.Session, 'user': c.user, 'for_view': True, 'with_private': False} q = c.q = request.params.get('q', '') sort_by = c.sort_by_selected = request.params.get('sort') try: self._check_access('site_read', context) self._check_access('group_list', context) except NotAuthorized: abort(403, _('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 data_dict_global_results = { 'all_fields': False, 'q': q, 'sort': sort_by, 'type': group_type or 'group', } global_results = self._action('group_list')(context, data_dict_global_results) data_dict_page_results = { 'all_fields': True, 'q': q, 'sort': sort_by, 'type': group_type or 'group', 'limit': items_per_page, 'offset': items_per_page * (page - 1), } page_results = self._action('group_list')(context, data_dict_page_results) c.page = h.Page( collection=global_results, page=page, url=h.pager_url, items_per_page=items_per_page, ) c.page.items = page_results return base.render('organization_vre/index.html', extra_vars={'group_type': group_type}) def read(self, id, limit=20): group_type = self._ensure_controller_matches_group_type( id.split('@')[0]) context = {'model': model, 'session': model.Session, 'user': c.user, 'schema': self._db_to_form_schema(group_type=group_type), 'for_view': True} data_dict = {'id': id, 'type': group_type} # unicode format (decoded from utf8) c.q = request.params.get('q', '') try: # Do not query for the group datasets when dictizing, as they will # be ignored and get requested on the controller anyway data_dict['include_datasets'] = False c.group_dict = self._action('group_show')(context, data_dict) c.group = context['group'] except (NotFound, NotAuthorized): abort(404, _('Group not found')) self._read(id, limit, group_type) return base.render('organization_vre/read.html', extra_vars={'group_type': group_type})