Skip to content

Commit

Permalink
Merge pull request #727 from okfn/727-import-cleans
Browse files Browse the repository at this point in the history
import cleanups for 2.1
  • Loading branch information
domoritz committed Apr 16, 2013
2 parents f75d21b + cf1c548 commit ad87251
Show file tree
Hide file tree
Showing 28 changed files with 188 additions and 161 deletions.
27 changes: 14 additions & 13 deletions ckan/config/environment.py
Expand Up @@ -9,7 +9,6 @@
from paste.deploy.converters import asbool
import sqlalchemy
from pylons import config
from pylons.i18n import _, ungettext
from genshi.template import TemplateLoader
from genshi.filters.i18n import Translator

Expand All @@ -18,10 +17,12 @@
import ckan.plugins as p
import ckan.lib.helpers as h
import ckan.lib.app_globals as app_globals
import ckan.lib.jinja_extensions as jinja_extensions

from ckan.common import _, ungettext

log = logging.getLogger(__name__)

import lib.jinja_extensions

# Suppress benign warning 'Unbuilt egg for setuptools'
warnings.simplefilter('ignore', UserWarning)
Expand Down Expand Up @@ -297,22 +298,22 @@ def genshi_lookup_attr(cls, obj, key):


# Create Jinja2 environment
env = lib.jinja_extensions.Environment(
loader=lib.jinja_extensions.CkanFileSystemLoader(template_paths),
env = jinja_extensions.Environment(
loader=jinja_extensions.CkanFileSystemLoader(template_paths),
autoescape=True,
extensions=['jinja2.ext.do', 'jinja2.ext.with_',
lib.jinja_extensions.SnippetExtension,
lib.jinja_extensions.CkanExtend,
lib.jinja_extensions.CkanInternationalizationExtension,
lib.jinja_extensions.LinkForExtension,
lib.jinja_extensions.ResourceExtension,
lib.jinja_extensions.UrlForStaticExtension,
lib.jinja_extensions.UrlForExtension]
jinja_extensions.SnippetExtension,
jinja_extensions.CkanExtend,
jinja_extensions.CkanInternationalizationExtension,
jinja_extensions.LinkForExtension,
jinja_extensions.ResourceExtension,
jinja_extensions.UrlForStaticExtension,
jinja_extensions.UrlForExtension]
)
env.install_gettext_callables(_, ungettext, newstyle=True)
# custom filters
env.filters['empty_and_escape'] = lib.jinja_extensions.empty_and_escape
env.filters['truncate'] = lib.jinja_extensions.truncate
env.filters['empty_and_escape'] = jinja_extensions.empty_and_escape
env.filters['truncate'] = jinja_extensions.truncate
config['pylons.app_globals'].jinja_env = env

# CONFIGURATION OPTIONS HERE (note: all config options will override
Expand Down
41 changes: 20 additions & 21 deletions ckan/controllers/api.py
Expand Up @@ -5,12 +5,9 @@
import glob
import urllib

from pylons import c, request, response
from pylons.i18n import _, gettext
from paste.util.multidict import MultiDict
from webob.multidict import UnicodeMultiDict
from paste.util.multidict import MultiDict

import ckan.rating
import ckan.model as model
import ckan.logic as logic
import ckan.lib.base as base
Expand All @@ -20,6 +17,8 @@
import ckan.lib.jsonp as jsonp
import ckan.lib.munge as munge

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


log = logging.getLogger(__name__)

Expand Down Expand Up @@ -159,7 +158,7 @@ def action(self, logic_function, ver=None):
except KeyError:
log.error('Can\'t find logic function: %s' % logic_function)
return self._finish_bad_request(
gettext('Action name not known: %s') % str(logic_function))
_('Action name not known: %s') % str(logic_function))

context = {'model': model, 'session': model.Session, 'user': c.user,
'api_version': ver}
Expand All @@ -172,12 +171,12 @@ def action(self, logic_function, ver=None):
except ValueError, inst:
log.error('Bad request data: %s' % str(inst))
return self._finish_bad_request(
gettext('JSON Error: %s') % str(inst))
_('JSON Error: %s') % str(inst))
if not isinstance(request_data, dict):
# this occurs if request_data is blank
log.error('Bad request data - not dict: %r' % request_data)
return self._finish_bad_request(
gettext('Bad request data: %s') %
_('Bad request data: %s') %
'Request data JSON decoded to %r but '
'it needs to be a dictionary.' % request_data)
try:
Expand Down Expand Up @@ -265,7 +264,7 @@ def list(self, ver=None, register=None, subregister=None, id=None):
action = self._get_action_from_map(action_map, register, subregister)
if not action:
return self._finish_bad_request(
gettext('Cannot list entity of this type: %s') % register)
_('Cannot list entity of this type: %s') % register)
try:
return self._finish_ok(action(context, {'id': id}))
except NotFound, e:
Expand Down Expand Up @@ -296,7 +295,7 @@ def show(self, ver=None, register=None, subregister=None,
action = self._get_action_from_map(action_map, register, subregister)
if not action:
return self._finish_bad_request(
gettext('Cannot read entity of this type: %s') % register)
_('Cannot read entity of this type: %s') % register)
try:
return self._finish_ok(action(context, data_dict))
except NotFound, e:
Expand Down Expand Up @@ -331,12 +330,12 @@ def create(self, ver=None, register=None, subregister=None,
data_dict.update(request_data)
except ValueError, inst:
return self._finish_bad_request(
gettext('JSON Error: %s') % str(inst))
_('JSON Error: %s') % str(inst))

action = self._get_action_from_map(action_map, register, subregister)
if not action:
return self._finish_bad_request(
gettext('Cannot create new entity of this type: %s %s') %
_('Cannot create new entity of this type: %s %s') %
(register, subregister))

try:
Expand Down Expand Up @@ -390,12 +389,12 @@ def update(self, ver=None, register=None, subregister=None,
data_dict.update(request_data)
except ValueError, inst:
return self._finish_bad_request(
gettext('JSON Error: %s') % str(inst))
_('JSON Error: %s') % str(inst))

action = self._get_action_from_map(action_map, register, subregister)
if not action:
return self._finish_bad_request(
gettext('Cannot update entity of this type: %s') %
_('Cannot update entity of this type: %s') %
register.encode('utf-8'))
try:
response_data = action(context, data_dict)
Expand Down Expand Up @@ -439,7 +438,7 @@ def delete(self, ver=None, register=None, subregister=None,
action = self._get_action_from_map(action_map, register, subregister)
if not action:
return self._finish_bad_request(
gettext('Cannot delete entity of this type: %s %s') %
_('Cannot delete entity of this type: %s %s') %
(register, subregister or ''))
try:
response_data = action(context, data_dict)
Expand All @@ -462,11 +461,11 @@ def search(self, ver=None, register=None):
id = request.params['since_id']
if not id:
return self._finish_bad_request(
gettext(u'No revision specified'))
_(u'No revision specified'))
rev = model.Session.query(model.Revision).get(id)
if rev is None:
return self._finish_not_found(
gettext(u'There is no revision with id: %s') % id)
_(u'There is no revision with id: %s') % id)
since_time = rev.timestamp
elif 'since_time' in request.params:
since_time_str = request.params['since_time']
Expand All @@ -476,7 +475,7 @@ def search(self, ver=None, register=None):
return self._finish_bad_request('ValueError: %s' % inst)
else:
return self._finish_bad_request(
gettext("Missing search term ('since_id=UUID' or " +
_("Missing search term ('since_id=UUID' or " +
" 'since_time=TIMESTAMP')"))
revs = model.Session.query(model.Revision).\
filter(model.Revision.timestamp > since_time)
Expand All @@ -486,7 +485,7 @@ def search(self, ver=None, register=None):
params = MultiDict(self._get_search_params(request.params))
except ValueError, e:
return self._finish_bad_request(
gettext('Could not read parameters: %r' % e))
_('Could not read parameters: %r' % e))

# if using API v2, default to returning the package ID if
# no field list is specified
Expand Down Expand Up @@ -543,10 +542,10 @@ def search(self, ver=None, register=None):
except search.SearchError, e:
log.exception(e)
return self._finish_bad_request(
gettext('Bad search option: %s') % e)
_('Bad search option: %s') % e)
else:
return self._finish_not_found(
gettext('Unknown register: %s') % register)
_('Unknown register: %s') % register)

@classmethod
def _get_search_params(cls, request_params):
Expand All @@ -555,7 +554,7 @@ def _get_search_params(cls, request_params):
qjson_param = request_params['qjson'].replace('\\\\u', '\\u')
params = h.json.loads(qjson_param, encoding='utf8')
except ValueError, e:
raise ValueError(gettext('Malformed qjson value') + ': %r'
raise ValueError(_('Malformed qjson value: %r')
% e)
elif len(request_params) == 1 and \
len(request_params.values()[0]) < 2 and \
Expand Down
29 changes: 15 additions & 14 deletions ckan/controllers/feed.py
Expand Up @@ -21,16 +21,17 @@
# TODO fix imports
import logging
import urlparse
from urllib import urlencode

import webhelpers.feedgenerator
from pylons import config
from pylons.i18n import _
from urllib import urlencode

from ckan import model
from ckan.lib.base import BaseController, c, request, response, json, abort, g
import ckan.model as model
import ckan.lib.base as base
import ckan.lib.helpers as h
from ckan.logic import get_action, NotFound
import ckan.logic as logic

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

# TODO make the item list configurable
ITEMS_LIMIT = 20
Expand All @@ -55,7 +56,7 @@ def _package_search(data_dict):
data_dict['rows'] = ITEMS_LIMIT

# package_search action modifies the data_dict, so keep our copy intact.
query = get_action('package_search')(context, data_dict.copy())
query = logic.get_action('package_search')(context, data_dict.copy())

return query['count'], query['results']

Expand Down Expand Up @@ -151,7 +152,7 @@ def _create_atom_id(resource_path, authority_name=None, date_string=None):
return ':'.join(['tag', tagging_entity, resource_path])


class FeedController(BaseController):
class FeedController(base.BaseController):
base_url = config.get('ckan.site_url')

def _alternate_url(self, params, **kwargs):
Expand All @@ -170,9 +171,9 @@ def group(self, id):
try:
context = {'model': model, 'session': model.Session,
'user': c.user or c.author}
group_dict = get_action('group_show')(context, {'id': id})
except NotFound:
abort(404, _('Group not found'))
group_dict = logic.get_action('group_show')(context, {'id': id})
except logic.NotFound:
base.abort(404, _('Group not found'))

data_dict, params = self._parse_url_params()
data_dict['fq'] = 'groups:"%s"' % id
Expand Down Expand Up @@ -281,9 +282,9 @@ def custom(self):
try:
page = int(request.params.get('page', 1))
except ValueError:
abort(400, _('"page" parameter must be a positive integer'))
base.abort(400, _('"page" parameter must be a positive integer'))
if page < 0:
abort(400, _('"page" parameter must be a positive integer'))
base.abort(400, _('"page" parameter must be a positive integer'))

limit = ITEMS_LIMIT
data_dict = {
Expand Down Expand Up @@ -433,9 +434,9 @@ def _parse_url_params(self):
try:
page = int(request.params.get('page', 1)) or 1
except ValueError:
abort(400, _('"page" parameter must be a positive integer'))
base.abort(400, _('"page" parameter must be a positive integer'))
if page < 0:
abort(400, _('"page" parameter must be a positive integer'))
base.abort(400, _('"page" parameter must be a positive integer'))

limit = ITEMS_LIMIT
data_dict = {
Expand Down
5 changes: 3 additions & 2 deletions ckan/controllers/home.py
@@ -1,5 +1,4 @@
from pylons.i18n import _
from pylons import g, c, config, cache
from pylons import config, cache
import sqlalchemy.exc

import ckan.logic as logic
Expand All @@ -9,6 +8,8 @@
import ckan.model as model
import ckan.lib.helpers as h

from ckan.common import _, g, c

CACHE_PARAMETERS = ['__cache', '__no_cache__']

# horrible hack
Expand Down
7 changes: 3 additions & 4 deletions ckan/controllers/related.py
@@ -1,15 +1,14 @@
import urllib

import ckan.model as model
import ckan.logic as logic
import ckan.lib.base as base
import ckan.lib.helpers as h
import ckan.lib.navl.dictization_functions as df

import pylons.i18n as i18n
from ckan.common import _, c

_ = i18n._
import urllib

c = base.c
abort = base.abort
_get_action = logic.get_action

Expand Down
16 changes: 8 additions & 8 deletions ckan/controllers/revision.py
@@ -1,14 +1,14 @@
from datetime import datetime, timedelta

from pylons.i18n import get_lang, _
from pylons import c, request

from ckan.logic import NotAuthorized, check_access
from pylons.i18n import get_lang

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

from ckan.common import _, c, request


class RevisionController(base.BaseController):

Expand All @@ -18,15 +18,15 @@ def __before__(self, action, **env):
context = {'model': model, 'user': c.user or c.author}
if c.user:
try:
check_access('revision_change_state', context)
logic.check_access('revision_change_state', context)
c.revision_change_state_allowed = True
except NotAuthorized:
except logic.NotAuthorized:
c.revision_change_state_allowed = False
else:
c.revision_change_state_allowed = False
try:
check_access('site_read', context)
except NotAuthorized:
logic.check_access('site_read', context)
except logic.NotAuthorized:
base.abort(401, _('Not authorized to see this page'))

def index(self):
Expand Down
5 changes: 3 additions & 2 deletions ckan/controllers/tag.py
@@ -1,11 +1,12 @@
from pylons.i18n import _
from pylons import request, c, config
from pylons import config

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

from ckan.common import _, request, c


LIMIT = 25

Expand Down
5 changes: 3 additions & 2 deletions ckan/controllers/user.py
@@ -1,8 +1,7 @@
import logging
from urllib import quote

from pylons import session, c, g, request, config
from pylons.i18n import _
from pylons import config
import genshi

import ckan.lib.i18n as i18n
Expand All @@ -17,6 +16,8 @@
import ckan.lib.mailer as mailer
import ckan.lib.navl.dictization_functions as dictization_functions

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

log = logging.getLogger(__name__)


Expand Down

0 comments on commit ad87251

Please sign in to comment.