def remove_url_param(key, value=None, replace=None, controller=None, action=None, extras=None, alternative_url=None): if isinstance(key, basestring): keys = [key] else: keys = key params_nopage = [(k, v) for k, v in request.params.items() if k != 'page'] params = list(params_nopage) if value: params.remove((keys[0], value)) else: for key in keys: [params.remove((k, v)) for (k, v) in params[:] if k == key] if replace is not None: params.append((keys[0], replace)) if alternative_url: return ckan_helpers._url_with_params(alternative_url, params) return ckan_helpers._create_url_with_params(params=params, controller=controller, action=action, extras=extras)
def _feed_url(self, query, controller, action, **kwargs): """ Constructs the url for the given action. Encoding the query parameters. """ path = h.url_for(controller=controller, action=action, **kwargs) return h._url_with_params(self.base_url + path, query.items())
def hdx_add_url_param(alternative_url=None, controller=None, action=None, extras=None, new_params=None, unwanted_keys=[]): ''' MODIFIED CKAN HELPER THAT ALLOWS REMOVING SOME PARAMS Adds extra parameters to existing ones controller action & extras (dict) are used to create the base url via url_for(controller=controller, action=action, **extras) controller & action default to the current ones This can be overriden providing an alternative_url, which will be used instead. ''' params_nopage = [(k, v) for k, v in request.params.items() if k != 'page' and k not in unwanted_keys] params = set(params_nopage) if new_params: params |= set(new_params.items()) if alternative_url: return h._url_with_params(alternative_url, params) return h._create_url_with_params(params=params, controller=controller, action=action, extras=extras)
def replace_url_param(new_params, alternative_url=None, controller=None, action=None, extras=None): ''' replace existing parameters with new ones controller action & extras (dict) are used to create the base url via :py:func:`~ckan.lib.helpers.url_for` controller & action default to the current ones This can be overriden providing an alternative_url, which will be used instead. ''' params_cleaned = [(k, v) for k, v in toolkit.request.params.items() if k not in new_params.keys()] params = set(params_cleaned) if new_params: params |= set(new_params.items()) if alternative_url: return helpers._url_with_params(alternative_url, params) return helpers._create_url_with_params(params=params, controller=controller, action=action, extras=extras)
def toggle_archives_url(): key = 'include_archives' params = request.params if key in request.params: params = [p for p in request.params.items() if p[0] != key] else: params = [p for p in request.params.items()] params.append((key, True)) return _url_with_params(request.path, params)
def replace_url_param(new_params, alternative_url=None, controller=None, action=None, extras=None): ''' replace existing parameters with new ones controller action & extras (dict) are used to create the base url via :py:func:`~ckan.lib.helpers.url_for` controller & action default to the current ones This can be overriden providing an alternative_url, which will be used instead. ''' params_cleaned = [(k, v) for k, v in toolkit.request.params.items() if k not in new_params.keys()] params = set(params_cleaned) if new_params: params |= set(new_params.items()) if alternative_url: return helpers._url_with_params(alternative_url, params) return helpers._url_with_params(toolkit.request.path, params)
def custom(self): q = request.params.get('q', u'') fq = '' search_params = {} for (param, value) in request.params.items(): if param not in ['q', 'page', 'sort'] \ and len(value) and not param.startswith('_'): search_params[param] = value fq += ' %s:"%s"' % (param, value) try: page = int(request.params.get('page', 1)) except ValueError: base.abort(400, _('"page" parameter must be a positive integer')) if page < 0: base.abort(400, _('"page" parameter must be a positive integer')) limit = ITEMS_LIMIT data_dict = { 'q': q, 'fq': fq, 'start': (page - 1) * limit, 'rows': limit, 'sort': request.params.get('sort', None), } item_count, results = _package_search(data_dict) navigation_urls = self._navigation_urls(request.params, item_count=item_count, limit=data_dict['rows'], controller='feed', action='custom') feed_url = self._feed_url(request.params, controller='feed', action='custom') atom_url = h._url_with_params('/feeds/custom.atom', search_params.items()) alternate_url = self._alternate_url(request.params) return self.output_feed(results, feed_title=u'%s - Custom query' % g.site_title, feed_description=u'Recently created or updated' ' datasets on %s. Custom query: \'%s\'' % (g.site_title, q), feed_link=alternate_url, feed_guid=_create_atom_id(atom_url), feed_url=feed_url, navigation_urls=navigation_urls)
def custom(): """ Custom atom feed """ q = request.params.get(u'q', u'') fq = u'' search_params = {} for (param, value) in request.params.items(): if param not in [u'q', u'page', u'sort'] \ and len(value) and not param.startswith(u'_'): search_params[param] = value fq += u'%s:"%s"' % (param, value) page = h.get_page_number(request.params) limit = ITEMS_LIMIT data_dict = { u'q': q, u'fq': fq, u'start': (page - 1) * limit, u'rows': limit, u'sort': request.params.get(u'sort', None) } item_count, results = _package_search(data_dict) navigation_urls = _navigation_urls( request.params, item_count=item_count, limit=data_dict['rows'], controller=u'feeds', action=u'custom') feed_url = _feed_url(request.params, controller=u'feeds', action=u'custom') atom_url = h._url_with_params(u'/feeds/custom.atom', search_params.items()) alternate_url = _alternate_url(request.params) return output_feed( results, feed_title=u'%s - Custom query' % SITE_TITLE, feed_description=u'Recently created or updated' ' datasets on %s. Custom query: \'%s\'' % (SITE_TITLE, q), feed_link=alternate_url, feed_guid=_create_atom_id(atom_url), feed_url=feed_url, navigation_urls=navigation_urls)
def h_add_url_param_unique(self, alternative_url=None, controller=None, action=None, extras=None, new_params=None): from ckan.common import request from ckan.lib import helpers as h params_nopage = [(k, v) for k, v in request.params.items() if ((k != 'page') and (k not in new_params))] params = set(params_nopage) if new_params: params |= set(new_params.items()) if alternative_url: return h._url_with_params(alternative_url, params) return h._create_url_with_params(params=params, controller=controller, action=action, extras=extras)
def custom(self): q = request.params.get("q", u"") fq = "" search_params = {} for (param, value) in request.params.items(): if param not in ["q", "page", "sort"] and len(value) and not param.startswith("_"): search_params[param] = value fq += ' %s:"%s"' % (param, value) page = self._get_page_number(request.params) limit = ITEMS_LIMIT data_dict = { "q": q, "fq": fq, "start": (page - 1) * limit, "rows": limit, "sort": request.params.get("sort", None), } item_count, results = _package_search(data_dict) navigation_urls = self._navigation_urls( request.params, item_count=item_count, limit=data_dict["rows"], controller="feed", action="custom" ) feed_url = self._feed_url(request.params, controller="feed", action="custom") atom_url = h._url_with_params("/feeds/custom.atom", search_params.items()) alternate_url = self._alternate_url(request.params) return self.output_feed( results, feed_title=u"%s - Custom query" % g.site_title, feed_description=u"Recently created or updated" " datasets on %s. Custom query: '%s'" % (g.site_title, q), feed_link=alternate_url, feed_guid=_create_atom_id(atom_url), feed_url=feed_url, navigation_urls=navigation_urls, )
def custom(self): q = request.params.get('q', u'') fq = '' search_params = {} params_dict = {} for plugin in p.PluginImplementations(p.IFacets): facets = plugin.dataset_facets(None, None) for (param, value) in request.params.items(): if param not in ['q', 'plage', 'sort'] \ and len(value) and not param.startswith('_'): search_params[param] = value params_dict[param] = params_dict.get(param, []) + [value] param = "groups" if param == "eurovoc_domains" else param fq += ' %s:"%s"' % (param, value) params_dict.pop('ext_boolean', None) try: page = int(request.params.get('page', 1)) or 1 except ValueError: base.abort(400, _('"page" parameter must be a positive integer')) if page < 0: base.abort(400, _('"page" parameter must be a positive integer')) limit = ITEMS_LIMIT data_dict = { 'q': q, 'fq': fq, 'start': (page - 1) * limit, 'rows': limit, 'sort': request.params.get('sort', None), } item_count, results = _package_search(data_dict) navigation_urls = self._navigation_urls(request.params, item_count=item_count, limit=data_dict['rows'], controller='feed', action='custom') feed_url = self._feed_url(request.params, controller='feed', action='custom') atom_url = h._url_with_params('/feeds/custom.atom', search_params.items()) alternate_url = self._alternate_url(request.params) if q: q = "'%s'" % q if params_dict: q += ";" elif not q and params_dict: q = '' else: q = '[everything]' facets_title = '%s' % q locale = tk.request.environ['CKAN_LANG'] or config.get( 'ckan.locale_default', 'en') for (param, value) in params_dict.iteritems(): facets_title += u' %s filters:' % _(facets.get(param, 'Not set')) for term_trans in value: query = '' if param == 'vocab_theme': trans_res_list = nal_util.retrieve_all_themes( locale) #.get(term_trans) trans_res = next((item.get('label') for item in trans_res_list if item.get('uri') == term_trans), '') query_result = [tuple([trans_res])] elif param == 'vocab_concepts_eurovoc': trans_res_list = nal_util.retrieve_all_controlled_keyword( locale) #.get(term_trans) trans_res = next( (item[1] for item in trans_res_list if item[0] == term_trans), '') query_result = [tuple([trans_res])] elif param == 'vocab_language': trans_res_list = nal_util.retrieve_all_labels_in_graphs_for_language( [nal_util.controlled_vocabulary['language'], locale]) #.get(term_trans) trans_res = next((item['label'] for item in trans_res_list if item['uri'] == term_trans), '') query_result = [tuple([trans_res])] elif param == 'vocab_geographical_coverage': trans_res_list = nal_util.retrieve_all_labels_in_graphs_for_language( [nal_util.controlled_vocabulary['country'], locale]) #.get(term_trans) trans_res = next((item['label'] for item in trans_res_list if item['uri'] == term_trans), '') query_result = [tuple([trans_res])] elif param == 'organization': query = "select tt.term_translation from \"group\" gr join term_translation tt on tt.term = gr.title where gr.name = '%s' and gr.type = '%s' and tt.lang_code = '%s'" % ( term_trans, param, unicode(i18n.get_lang())) query_result = model.Session.execute(query).fetchall() if not query_result: query = "select title from \"group\" where type = 'organization' and name = '%s'" % term_trans query_result = model.Session.execute(query).fetchall() else: query_result = [tuple([term_trans])] for result in query_result: facets_title += ' %s' % url.unquote( str(url.quote(result[0].encode('utf8'), safe=''))).decode('utf8') if term_trans != value[-1]: facets_title += "," if term_trans == value[-1] and param != params_dict.keys( )[-1]: facets_title += ';' try: result = self.output_feed( results, feed_title=u'%s - Custom query: %s' % (_(g.site_title), facets_title), feed_description=u'Recently created or updated' ' datasets on %s. Custom query: %s' % (_(g.site_title), facets_title), feed_link=alternate_url, feed_guid=_create_atom_id(atom_url), feed_url=feed_url, navigation_urls=navigation_urls) return result except BaseException as e: import traceback log.error("Output feed failed") log.error(traceback.print_exc(e))