Skip to content

Commit

Permalink
[#3196] Remove usages of g outside templates
Browse files Browse the repository at this point in the history
  • Loading branch information
amercader committed Aug 11, 2016
1 parent 4d24254 commit c087a72
Show file tree
Hide file tree
Showing 7 changed files with 38 additions and 26 deletions.
24 changes: 14 additions & 10 deletions ckan/controllers/feed.py
Expand Up @@ -25,14 +25,13 @@
import urlparse

import webhelpers.feedgenerator
from ckan.common import config

import ckan.model as model
import ckan.lib.base as base
import ckan.lib.helpers as h
import ckan.logic as logic

from ckan.common import _, g, c, request, response, json
from ckan.common import _, config, c, request, response, json

# TODO make the item list configurable
ITEMS_LIMIT = 20
Expand Down Expand Up @@ -190,25 +189,26 @@ def _group_or_organization(self, obj_dict, is_org):
action=group_type,
id=obj_dict['name'])

site_title = config.get('ckan.site_title', 'CKAN')
if is_org:
guid = _create_atom_id(u'/feeds/organization/%s.atom' %
obj_dict['name'])
alternate_url = self._alternate_url(params,
organization=obj_dict['name'])
desc = u'Recently created or updated datasets on %s '\
'by organization: "%s"' % (g.site_title, obj_dict['title'])
title = u'%s - Organization: "%s"' %\
(g.site_title, obj_dict['title'])
'by organization: "%s"' % (site_title, obj_dict['title'])
title = u'%s - Organization: "%s"' % (site_title,
obj_dict['title'])

else: # is group
guid = _create_atom_id(u'/feeds/group/%s.atom' %
obj_dict['name'])
alternate_url = self._alternate_url(params,
groups=obj_dict['name'])
desc = u'Recently created or updated datasets on %s '\
'by group: "%s"' % (g.site_title, obj_dict['title'])
'by group: "%s"' % (site_title, obj_dict['title'])
title = u'%s - Group: "%s"' %\
(g.site_title, obj_dict['title'])
(site_title, obj_dict['title'])

return self.output_feed(results,
feed_title=title,
Expand Down Expand Up @@ -289,10 +289,12 @@ def general(self):

alternate_url = self._alternate_url(params)

site_title = config.get('ckan.site_title', 'CKAN')

return self.output_feed(results,
feed_title=g.site_title,
feed_title=site_title,
feed_description=u'Recently created or '
'updated datasets on %s' % g.site_title,
'updated datasets on %s' % site_title,
feed_link=alternate_url,
feed_guid=_create_atom_id
(u'/feeds/dataset.atom'),
Expand Down Expand Up @@ -338,8 +340,10 @@ def custom(self):

alternate_url = self._alternate_url(request.params)

site_title = config.get('ckan.site_title', 'CKAN')

return self.output_feed(results,
feed_title=u'%s - Custom query' % g.site_title,
feed_title=u'%s - Custom query' % site_title,
feed_description=u'Recently created or updated'
' datasets on %s. Custom query: \'%s\'' %
(g.site_title, q),
Expand Down
6 changes: 3 additions & 3 deletions ckan/controllers/group.py
Expand Up @@ -16,7 +16,7 @@
import ckan.authz as authz
import ckan.lib.plugins
import ckan.plugins as plugins
from ckan.common import OrderedDict, c, g, request, _
from ckan.common import OrderedDict, c, config, request, _

log = logging.getLogger(__name__)

Expand Down Expand Up @@ -310,7 +310,7 @@ def pager_url(q=None, page=None):
'res_format': _('Formats'),
'license_id': _('Licenses')}

for facet in g.facets:
for facet in h.facets():
if facet in default_facet_titles:
facets[facet] = default_facet_titles[facet]
else:
Expand Down Expand Up @@ -356,7 +356,7 @@ def pager_url(q=None, page=None):
c.search_facets_limits = {}
for facet in c.facets.keys():
limit = int(request.params.get('_%s_limit' % facet,
g.facets_default_number))
config.get('search.facets.default', 10)))
c.search_facets_limits[facet] = limit
c.page.items = query['results']

Expand Down
6 changes: 3 additions & 3 deletions ckan/controllers/home.py
Expand Up @@ -10,7 +10,7 @@
import ckan.model as model
import ckan.lib.helpers as h

from ckan.common import _, g, c
from ckan.common import _, config, c

CACHE_PARAMETERS = ['__cache', '__no_cache__']

Expand Down Expand Up @@ -46,7 +46,7 @@ def index(self):
'user': c.user, 'auth_user_obj': c.userobj}
data_dict = {
'q': '*:*',
'facet.field': g.facets,
'facet.field': h.facets(),
'rows': 4,
'start': 0,
'sort': 'views_recent desc',
Expand Down Expand Up @@ -82,7 +82,7 @@ def index(self):
' and add your email address. ') % url + \
_('%s uses your email address'
' if you need to reset your password.') \
% g.site_title
% config.get('ckan.site_title')
h.flash_notice(msg, allow_html=True)

return base.render('home/index.html', cache_force=True)
Expand Down
11 changes: 6 additions & 5 deletions ckan/controllers/package.py
Expand Up @@ -23,7 +23,7 @@
import ckan.plugins as p
import ckan.lib.render

from ckan.common import OrderedDict, _, json, request, c, g, response
from ckan.common import OrderedDict, _, json, request, c, response
from home import CACHE_PARAMETERS

log = logging.getLogger(__name__)
Expand Down Expand Up @@ -148,7 +148,7 @@ def search(self):
c.query_error = False
page = h.get_page_number(request.params)

limit = g.datasets_per_page
limit = int(config.get('ckan.datasets_per_page', 20))

# most search operations should reset the page counter:
params_nopage = [(k, v) for k, v in request.params.items()
Expand Down Expand Up @@ -245,7 +245,7 @@ def pager_url(q=None, page=None):
'license_id': _('Licenses'),
}

for facet in g.facets:
for facet in h.facets():
if facet in default_facet_titles:
facets[facet] = default_facet_titles[facet]
else:
Expand Down Expand Up @@ -300,7 +300,7 @@ def pager_url(q=None, page=None):
for facet in c.search_facets.keys():
try:
limit = int(request.params.get('_%s_limit' % facet,
g.facets_default_number))
int(config.get('search.facets.default', 10))))
except ValueError:
abort(400, _('Parameter "{parameter_name}" is not '
'an integer').format(
Expand Down Expand Up @@ -657,7 +657,8 @@ def new_resource(self, id, data=None, errors=None, error_summary=None):
# no data so keep on page
msg = _('You must add at least one data resource')
# On new templates do not use flash message
if g.legacy_templates:

if asbool(config.get('ckan.legacy_templates')):
h.flash_error(msg)
redirect(h.url_for(controller='package',
action='new_resource', id=id))
Expand Down
3 changes: 1 addition & 2 deletions ckan/controllers/user.py
@@ -1,7 +1,6 @@
# encoding: utf-8

import logging
from urllib import quote

from ckan.common import config
from paste.deploy.converters import asbool
Expand All @@ -18,7 +17,7 @@
import ckan.lib.authenticator as authenticator
import ckan.plugins as p

from ckan.common import _, c, g, request, response
from ckan.common import _, c, request, response

log = logging.getLogger(__name__)

Expand Down
2 changes: 1 addition & 1 deletion ckan/lib/base.py
Expand Up @@ -30,7 +30,7 @@
# These imports are for legacy usages and will be removed soon these should
# be imported directly from ckan.common for internal ckan code and via the
# plugins.toolkit for extensions.
from ckan.common import json, _, ungettext, c, g, request, response, config
from ckan.common import json, _, ungettext, c, request, response, config

log = logging.getLogger(__name__)

Expand Down
12 changes: 10 additions & 2 deletions ckan/lib/helpers.py
Expand Up @@ -43,10 +43,12 @@
import ckan.plugins as p
import ckan

from ckan.common import _, ungettext, g, c, request, session, json
from ckan.common import _, ungettext, c, request, session, json

log = logging.getLogger(__name__)

DEFAULT_FACET_NAMES = u'organization groups tags res_format license_id'


class HelperAttributeDict(dict):
def __init__(self, *args, **kwargs):
Expand Down Expand Up @@ -845,7 +847,7 @@ def sorted_extras(package_extras, auto_clean=False, subs=None, exclude=None):

# If exclude is not supplied use values defined in the config
if not exclude:
exclude = g.package_hide_extras
exclude = config.get('package_hide_extras', [])
output = []
for extra in sorted(package_extras, key=lambda x: x['key']):
if extra.get('state') == 'deleted':
Expand Down Expand Up @@ -2268,6 +2270,12 @@ def get_translated(data_dict, field):
return data_dict.get(field, '')


@core_helper
def facets():
u'''Returns a list of the current facet names'''
return config.get(u'search.facets', DEFAULT_FACET_NAMES).split()


core_helper(flash, name='flash')
core_helper(localised_number)
core_helper(localised_SI_number)
Expand Down

0 comments on commit c087a72

Please sign in to comment.