def index(): u'''display home page''' try: context = { u'model': model, u'session': model.Session, u'user': g.user, u'auth_user_obj': g.userobj } data_dict = { u'q': u'*:*', u'facet.field': h.facets(), u'rows': 4, u'start': 0, u'sort': u'view_recent desc', u'fq': u'capacity:"public"' } query = logic.get_action(u'package_search')(context, data_dict) g.search_facets = query['search_facets'] g.package_count = query['count'] g.datasets = query['results'] org_label = h.humanize_entity_type( u'organization', h.default_group_type(u'organization'), u'facet label') or _(u'Organizations') group_label = h.humanize_entity_type(u'group', h.default_group_type(u'group'), u'facet label') or _(u'Groups') g.facet_titles = { u'organization': org_label, u'groups': group_label, u'tags': _(u'Tags'), u'res_format': _(u'Formats'), u'license': _(u'Licenses'), } except search.SearchError: g.package_count = 0 if g.userobj and not g.userobj.email: url = h.url_for(controller=u'user', action=u'edit') msg = _(u'Please <a href="%s">update your profile</a>' u' and add your email address. ') % url + \ _(u'%s uses your email address' u' if you need to reset your password.') \ % config.get(u'ckan.site_title') h.flash_notice(msg, allow_html=True) return base.render(u'home/index.html', extra_vars={})
def post(self, group_type, is_organization, id=None): set_org(is_organization) context = self._prepare(id) try: _action(u'group_delete')(context, {u'id': id}) group_label = h.humanize_entity_type( u'group', group_type, u'has been deleted') or _(u'Group') h.flash_notice( _(u'%s has been deleted.') % _(group_label)) group_dict = _action(u'group_show')(context, {u'id': id}) except NotAuthorized: base.abort(403, _(u'Unauthorized to delete group %s') % u'') except NotFound: base.abort(404, _(u'Group not found')) except ValidationError as e: h.flash_error(e.error_dict['message']) return h.redirect_to(u'organization.read', id=id) return h.redirect_to(u'{}.read'.format(group_type), id=id) # TODO: Remove g.group_dict = group_dict return h.redirect_to(u'{}.index'.format(group_type))
def post(self, group_type: str, is_organization: bool, id: Optional[str] = None) -> Response: set_org(is_organization) context = self._prepare(id) try: _action(u'group_delete')(context, {u'id': id}) group_label = h.humanize_entity_type( u'group', group_type, u'has been deleted') or _(u'Group') h.flash_notice( _(u'%s has been deleted.') % _(group_label)) except NotAuthorized: base.abort(403, _(u'Unauthorized to delete group %s') % u'') except NotFound: base.abort(404, _(u'Group not found')) except ValidationError as e: base.abort(403, _(e.error_dict['message'])) return h.redirect_to(u'{}.index'.format(group_type))
def search(package_type): extra_vars = {} try: context = { u'model': model, u'user': g.user, u'auth_user_obj': g.userobj } check_access(u'site_read', context) except NotAuthorized: base.abort(403, _(u'Not authorized to see this page')) # unicode format (decoded from utf8) extra_vars[u'q'] = q = request.args.get(u'q', u'') extra_vars['query_error'] = False page = h.get_page_number(request.args) limit = int(config.get(u'ckan.datasets_per_page', 20)) # most search operations should reset the page counter: params_nopage = [(k, v) for k, v in request.args.items() if k != u'page'] extra_vars[u'drill_down_url'] = drill_down_url extra_vars[u'remove_field'] = partial(remove_field, package_type) sort_by = request.args.get(u'sort', None) params_nosort = [(k, v) for k, v in params_nopage if k != u'sort'] extra_vars[u'sort_by'] = partial(_sort_by, params_nosort, package_type) if not sort_by: sort_by_fields = [] else: sort_by_fields = [field.split()[0] for field in sort_by.split(u',')] extra_vars[u'sort_by_fields'] = sort_by_fields pager_url = partial(_pager_url, params_nopage, package_type) search_url_params = urlencode(_encode_params(params_nopage)) extra_vars[u'search_url_params'] = search_url_params details = _get_search_details() extra_vars[u'fields'] = details[u'fields'] extra_vars[u'fields_grouped'] = details[u'fields_grouped'] fq = details[u'fq'] search_extras = details[u'search_extras'] context = { u'model': model, u'session': model.Session, u'user': g.user, u'for_view': True, u'auth_user_obj': g.userobj } # Unless changed via config options, don't show other dataset # types any search page. Potential alternatives are do show them # on the default search page (dataset) or on one other search page search_all_type = config.get(u'ckan.search.show_all_types', u'dataset') search_all = False try: # If the "type" is set to True or False, convert to bool # and we know that no type was specified, so use traditional # behaviour of applying this only to dataset type search_all = asbool(search_all_type) search_all_type = u'dataset' # Otherwise we treat as a string representing a type except ValueError: search_all = True if not search_all or package_type != search_all_type: # Only show datasets of this particular type fq += u' +dataset_type:{type}'.format(type=package_type) facets = OrderedDict() org_label = h.humanize_entity_type( u'organization', h.default_group_type(u'organization'), u'facet label') or _(u'Organizations') group_label = h.humanize_entity_type( u'group', h.default_group_type(u'group'), u'facet label') or _(u'Groups') default_facet_titles = { u'organization': org_label, u'groups': group_label, u'tags': _(u'Tags'), u'res_format': _(u'Formats'), u'license_id': _(u'Licenses'), } for facet in h.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): facets = plugin.dataset_facets(facets, package_type) extra_vars[u'facet_titles'] = facets data_dict = { u'q': q, u'fq': fq.strip(), u'facet.field': list(facets.keys()), u'rows': limit, u'start': (page - 1) * limit, u'sort': sort_by, u'extras': search_extras, u'include_private': asbool( config.get(u'ckan.search.default_include_private', True) ), } try: query = get_action(u'package_search')(context, data_dict) extra_vars[u'sort_by_selected'] = query[u'sort'] extra_vars[u'page'] = h.Page( collection=query[u'results'], page=page, url=pager_url, item_count=query[u'count'], items_per_page=limit ) extra_vars[u'search_facets'] = query[u'search_facets'] extra_vars[u'page'].items = query[u'results'] except SearchQueryError as se: # User's search parameters are invalid, in such a way that is not # achievable with the web interface, so return a proper error to # discourage spiders which are the main cause of this. log.info(u'Dataset search query rejected: %r', se.args) base.abort( 400, _(u'Invalid search query: {error_message}') .format(error_message=str(se)) ) except SearchError as se: # May be bad input from the user, but may also be more serious like # bad code causing a SOLR syntax error, or a problem connecting to # SOLR log.error(u'Dataset search error: %r', se.args) extra_vars[u'query_error'] = True extra_vars[u'search_facets'] = {} extra_vars[u'page'] = h.Page(collection=[]) # FIXME: try to avoid using global variables g.search_facets_limits = {} for facet in extra_vars[u'search_facets'].keys(): try: limit = int( request.args.get( u'_%s_limit' % facet, int(config.get(u'search.facets.default', 10)) ) ) except ValueError: base.abort( 400, _(u'Parameter u"{parameter_name}" is not ' u'an integer').format(parameter_name=u'_%s_limit' % facet) ) g.search_facets_limits[facet] = limit _setup_template_variables(context, {}, package_type=package_type) extra_vars[u'dataset_type'] = package_type # TODO: remove for key, value in six.iteritems(extra_vars): setattr(g, key, value) return base.render( _get_pkg_template(u'search_template', package_type), extra_vars )
def post(self, id, group_type, is_organization, data=None): set_org(is_organization) context = self._prepare(group_type) data_dict = {u'id': id, u'type': group_type} try: check_access(u'bulk_update_public', context, {u'org_id': id}) # 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 group_dict = _action(u'group_show')(context, data_dict) group = context['group'] except NotFound: group_label = h.humanize_entity_type( u'organization' if is_organization else u'group', group_type, u'default label') or _( u'Organization' if is_organization else u'Group') base.abort(404, _(u'{} not found'.format(group_label))) except NotAuthorized: base.abort(403, _(u'User %r not authorized to edit %s') % (g.user, id)) if not group_dict['is_organization']: # FIXME: better error raise Exception(u'Must be an organization') # 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.group = group # use different form names so that ie7 can be detected form_names = set([ u"bulk_action.public", u"bulk_action.delete", u"bulk_action.private" ]) actions_in_form = set(request.form.keys()) actions = form_names.intersection(actions_in_form) # ie7 puts all buttons in form params but puts submitted one twice for key, value in six.iteritems(request.form.to_dict()): if value in [u'private', u'public']: action = key.split(u'.')[-1] break else: # normal good browser form submission action = actions.pop().split(u'.')[-1] # process the action first find the datasets to perform the action on. # they are prefixed by dataset_ in the form data datasets = [] for param in request.form: if param.startswith(u'dataset_'): datasets.append(param[8:]) action_functions = { u'private': u'bulk_update_private', u'public': u'bulk_update_public', u'delete': u'bulk_update_delete', } data_dict = {u'datasets': datasets, u'org_id': group_dict['id']} try: get_action(action_functions[action])(context, data_dict) except NotAuthorized: base.abort(403, _(u'Not authorized to perform bulk update')) return h.redirect_to(u'{}.bulk_process'.format(group_type), id=id)
def _read(id, limit, group_type): u''' This is common code used by both read and bulk_process''' extra_vars = {} context = { u'model': model, u'session': model.Session, u'user': g.user, u'schema': _db_to_form_schema(group_type=group_type), u'for_view': True, u'extras_as_string': True } q = request.params.get(u'q', u'') # TODO: Remove # ckan 2.9: Adding variables that were removed from c object for # compatibility with templates in existing extensions g.q = q # Search within group if g.group_dict.get(u'is_organization'): fq = u' owner_org:"%s"' % g.group_dict.get(u'id') else: fq = u' groups:"%s"' % g.group_dict.get(u'name') extra_vars["q"] = q g.description_formatted = \ h.render_markdown(g.group_dict.get(u'description')) context['return_query'] = True page = h.get_page_number(request.params) # most search operations should reset the page counter: params_nopage = [(k, v) for k, v in request.params.items(multi=True) if k != u'page'] sort_by = request.params.get(u'sort', None) def search_url(params): controller = lookup_group_controller(group_type) action = u'bulk_process' if getattr( g, u'action', u'') == u'bulk_process' else u'read' url = h.url_for(u'.'.join([controller, action]), id=id) params = [ (k, v.encode(u'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_type, action=u'read', extras=dict(id=g.group_dict.get(u'name')), new_params=by) extra_vars["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=u'read', extras=dict(id=g.group_dict.get(u'name'))) extra_vars["remove_field"] = remove_field def pager_url(q=None, page=None): params = list(params_nopage) params.append((u'page', page)) return search_url(params) details = _get_search_details() extra_vars[u'fields'] = details[u'fields'] extra_vars[u'fields_grouped'] = details[u'fields_grouped'] fq += details[u'fq'] search_extras = details[u'search_extras'] # TODO: Remove # ckan 2.9: Adding variables that were removed from c object for # compatibility with templates in existing extensions g.fields = extra_vars[u'fields'] g.fields_grouped = extra_vars[u'fields_grouped'] facets = OrderedDict() org_label = h.humanize_entity_type(u'organization', h.default_group_type(u'organization'), u'facet label') or _(u'Organizations') group_label = h.humanize_entity_type(u'group', h.default_group_type(u'group'), u'facet label') or _(u'Groups') default_facet_titles = { u'organization': org_label, u'groups': group_label, u'tags': _(u'Tags'), u'res_format': _(u'Formats'), u'license_id': _(u'Licenses') } for facet in h.facets(): if facet in default_facet_titles: facets[facet] = default_facet_titles[facet] else: facets[facet] = facet # Facet titles facets = _update_facet_titles(facets, group_type) extra_vars["facet_titles"] = facets data_dict = { u'q': q, u'fq': fq, u'include_private': True, u'facet.field': list(facets.keys()), u'rows': limit, u'sort': sort_by, u'start': (page - 1) * limit, u'extras': search_extras } context_ = dict((k, v) for (k, v) in context.items() if k != u'schema') try: query = get_action(u'package_search')(context_, data_dict) except search.SearchError as se: log.error(u'Group search error: %r', se.args) extra_vars["query_error"] = True extra_vars["page"] = h.Page(collection=[]) else: extra_vars["page"] = h.Page(collection=query['results'], page=page, url=pager_url, item_count=query['count'], items_per_page=limit) # TODO: Remove # ckan 2.9: Adding variables that were removed from c object for # compatibility with templates in existing extensions g.group_dict['package_count'] = query['count'] extra_vars["search_facets"] = g.search_facets = query['search_facets'] extra_vars["search_facets_limits"] = g.search_facets_limits = {} for facet in g.search_facets.keys(): limit = int( request.params.get(u'_%s_limit' % facet, config.get(u'search.facets.default', 10))) g.search_facets_limits[facet] = limit extra_vars["page"].items = query['results'] extra_vars["sort_by_selected"] = sort_by # TODO: Remove # ckan 2.9: Adding variables that were removed from c object for # compatibility with templates in existing extensions g.facet_titles = facets g.page = extra_vars["page"] extra_vars["group_type"] = group_type _setup_template_variables(context, {u'id': id}, group_type=group_type) return extra_vars