Skip to content

Commit

Permalink
PEP8, lint and log string substitution refinement.
Browse files Browse the repository at this point in the history
  • Loading branch information
David Read committed Sep 4, 2015
1 parent 9026c30 commit 5c6ab31
Show file tree
Hide file tree
Showing 5 changed files with 180 additions and 138 deletions.
70 changes: 38 additions & 32 deletions ckan/controllers/api.py
Expand Up @@ -85,7 +85,7 @@ def _finish(self, status_int, response_data=None,
if content_type == 'json':
response_msg = h.json.dumps(
response_data,
for_json=True) # handle objects with for_json methods
for_json=True) # handle objects with for_json methods
else:
response_msg = response_data
# Support "JSONP" callback.
Expand Down Expand Up @@ -161,7 +161,7 @@ def action(self, logic_function, ver=None):
try:
function = get_action(logic_function)
except KeyError:
log.info('Can\'t find logic function: %s' % logic_function)
log.info('Can\'t find logic function: %s', logic_function)
return self._finish_bad_request(
_('Action name not known: %s') % logic_function)

Expand All @@ -183,12 +183,13 @@ def action(self, logic_function, ver=None):
request_data = self._get_request_data(try_url_params=
side_effect_free)
except ValueError, inst:
log.info('Bad Action API request data: %s' % inst)
log.info('Bad Action API request data: %s', inst)
return self._finish_bad_request(
_('JSON Error: %s') % inst)
if not isinstance(request_data, dict):
# this occurs if request_data is blank
log.info('Bad Action API request data - not dict: %r' % request_data)
log.info('Bad Action API request data - not dict: %r',
request_data)
return self._finish_bad_request(
_('Bad request data: %s') %
'Request data JSON decoded to %r but '
Expand All @@ -201,7 +202,8 @@ def action(self, logic_function, ver=None):
return_dict['success'] = True
return_dict['result'] = result
except DataError, e:
log.info('Format incorrect (Action API): %s - %s' % (e.error, request_data))
log.info('Format incorrect (Action API): %s - %s',
e.error, request_data)
return_dict['error'] = {'__type': 'Integrity Error',
'message': e.error,
'data': request_data}
Expand All @@ -211,7 +213,7 @@ def action(self, logic_function, ver=None):
return_dict['error'] = {'__type': 'Authorization Error',
'message': _('Access denied')}
return_dict['success'] = False

if unicode(e):
return_dict['error']['message'] += u': %s' % e

Expand All @@ -229,7 +231,7 @@ def action(self, logic_function, ver=None):
return_dict['error'] = error_dict
return_dict['success'] = False
# CS nasty_string ignore
log.info('Validation error (Action API): %r' % str(e.error_dict))
log.info('Validation error (Action API): %r', str(e.error_dict))
return self._finish(409, return_dict, content_type='json')
except search.SearchQueryError, e:
return_dict['error'] = {'__type': 'Search Query Error',
Expand All @@ -243,9 +245,10 @@ def action(self, logic_function, ver=None):
return_dict['success'] = False
return self._finish(409, return_dict, content_type='json')
except search.SearchIndexError, e:
return_dict['error'] = {'__type': 'Search Index Error',
'message': 'Unable to add package to search index: %s' %
str(e)}
return_dict['error'] = {
'__type': 'Search Index Error',
'message': 'Unable to add package to search index: %s' %
str(e)}
return_dict['success'] = False
return self._finish(500, return_dict, content_type='json')
return self._finish_ok(return_dict)
Expand All @@ -268,7 +271,7 @@ def list(self, ver=None, register=None, subregister=None, id=None):
context = {'model': model, 'session': model.Session,
'user': c.user, 'api_version': ver,
'auth_user_obj': c.userobj}
log.debug('listing: %s' % context)
log.debug('listing: %s', context)
action_map = {
'revision': 'revision_list',
'group': 'group_list',
Expand Down Expand Up @@ -313,7 +316,7 @@ def show(self, ver=None, register=None, subregister=None,
'api_version': ver, 'auth_user_obj': c.userobj}
data_dict = {'id': id, 'id2': id2, 'rel': subregister}

log.debug('show: %s' % context)
log.debug('show: %s', context)

action = self._get_action_from_map(action_map, register, subregister)
if not action:
Expand Down Expand Up @@ -345,7 +348,7 @@ def create(self, ver=None, register=None, subregister=None,

context = {'model': model, 'session': model.Session, 'user': c.user,
'api_version': ver, 'auth_user_obj': c.userobj}
log.debug('create: %s' % (context))
log.debug('create: %s', (context))
try:
request_data = self._get_request_data()
data_dict = {'id': id, 'id2': id2, 'rel': subregister}
Expand Down Expand Up @@ -375,18 +378,19 @@ def create(self, ver=None, register=None, subregister=None,
return self._finish_not_found(unicode(e))
except ValidationError, e:
# CS: nasty_string ignore
log.info('Validation error (REST create): %r' % str(e.error_dict))
log.info('Validation error (REST create): %r', str(e.error_dict))
return self._finish(409, e.error_dict, content_type='json')
except DataError, e:
log.info('Format incorrect (REST create): %s - %s' % (e.error, request_data))
log.info('Format incorrect (REST create): %s - %s',
e.error, request_data)
error_dict = {
'success': False,
'error': {'__type': 'Integrity Error',
'message': e.error,
'data': request_data}}
return self._finish(400, error_dict, content_type='json')
except search.SearchIndexError:
log.error('Unable to add package to search index: %s' %
log.error('Unable to add package to search index: %s',
request_data)
return self._finish(500,
_(u'Unable to add package to search index') %
Expand All @@ -407,7 +411,7 @@ def update(self, ver=None, register=None, subregister=None,

context = {'model': model, 'session': model.Session, 'user': c.user,
'api_version': ver, 'id': id, 'auth_user_obj': c.userobj}
log.debug('update: %s' % (context))
log.debug('update: %s', context)
try:
request_data = self._get_request_data()
data_dict = {'id': id, 'id2': id2, 'rel': subregister}
Expand All @@ -430,18 +434,19 @@ def update(self, ver=None, register=None, subregister=None,
return self._finish_not_found(unicode(e))
except ValidationError, e:
# CS: nasty_string ignore
log.info('Validation error (REST update): %r' % str(e.error_dict))
log.info('Validation error (REST update): %r', str(e.error_dict))
return self._finish(409, e.error_dict, content_type='json')
except DataError, e:
log.info('Format incorrect (REST update): %s - %s' % (e.error, request_data))
log.info('Format incorrect (REST update): %s - %s',
e.error, request_data)
error_dict = {
'success': False,
'error': {'__type': 'Integrity Error',
'message': e.error,
'data': request_data}}
return self._finish(400, error_dict, content_type='json')
except search.SearchIndexError:
log.error('Unable to update search index: %s' % request_data)
log.error('Unable to update search index: %s', request_data)
return self._finish(500, _(u'Unable to update search index') %
request_data)

Expand All @@ -461,7 +466,7 @@ def delete(self, ver=None, register=None, subregister=None,

data_dict = {'id': id, 'id2': id2, 'rel': subregister}

log.debug('delete %s/%s/%s/%s' % (register, id, subregister, id2))
log.debug('delete %s/%s/%s/%s', register, id, subregister, id2)

action = self._get_action_from_map(action_map, register, subregister)
if not action:
Expand All @@ -477,12 +482,12 @@ def delete(self, ver=None, register=None, subregister=None,
return self._finish_not_found(unicode(e))
except ValidationError, e:
# CS: nasty_string ignore
log.info('Validation error (REST delete): %r' % str(e.error_dict))
log.info('Validation error (REST delete): %r', str(e.error_dict))
return self._finish(409, e.error_dict, content_type='json')

def search(self, ver=None, register=None):

log.debug('search %s params: %r' % (register, request.params))
log.debug('search %s params: %r', register, request.params)
if register == 'revision':
since_time = None
if 'since_id' in request.params:
Expand All @@ -504,10 +509,10 @@ def search(self, ver=None, register=None):
else:
return self._finish_bad_request(
_("Missing search term ('since_id=UUID' or " +
" 'since_time=TIMESTAMP')"))
" 'since_time=TIMESTAMP')"))
revs = model.Session.query(model.Revision).\
filter(model.Revision.timestamp > since_time)
return self._finish_ok([rev.id for rev in revs])
return self._finish_ok([rev_.id for rev_ in revs])
elif register in ['dataset', 'package', 'resource']:
try:
params = MultiDict(self._get_search_params(request.params))
Expand Down Expand Up @@ -685,14 +690,15 @@ def organization_autocomplete(self):
if q:
context = {'user': c.user, 'model': model}
data_dict = {'q': q, 'limit': limit}
organization_list = get_action('organization_autocomplete')(context, data_dict)
organization_list = \
get_action('organization_autocomplete')(context, data_dict)
return organization_list

def is_slug_valid(self):

def package_exists(val):
if model.Session.query(model.Package) \
.autoflush(False).filter_by(name=val).count():
.autoflush(False).filter_by(name=val).count():
return True
return False

Expand Down Expand Up @@ -849,9 +855,9 @@ def make_unicode(entity):
else:
return entity

cls.log.debug('Retrieving request params: %r' % request.params)
cls.log.debug('Retrieving request POST: %r' % request.POST)
cls.log.debug('Retrieving request GET: %r' % request.GET)
cls.log.debug('Retrieving request params: %r', request.params)
cls.log.debug('Retrieving request POST: %r', request.POST)
cls.log.debug('Retrieving request GET: %r', request.GET)
request_data = None
if request.POST and request.content_type == 'multipart/form-data':
request_data = dict(request.POST)
Expand Down Expand Up @@ -882,7 +888,7 @@ def make_unicode(entity):
msg = "Could not extract request body data: %s" % \
(inst)
raise ValueError(msg)
cls.log.debug('Retrieved request body: %r' % request.body)
cls.log.debug('Retrieved request body: %r', request.body)
if not request_data:
if not try_url_params:
msg = "No request body data"
Expand All @@ -905,5 +911,5 @@ def make_unicode(entity):
# if val is str then assume it is ascii, since json converts
# utf8 encoded JSON to unicode
request_data[key] = make_unicode(val)
cls.log.debug('Request data extracted: %r' % request_data)
cls.log.debug('Request data extracted: %r', request_data)
return request_data
42 changes: 25 additions & 17 deletions ckan/controllers/group.py
Expand Up @@ -212,7 +212,7 @@ def read(self, id, limit=20):
data_dict = {'id': id}

# unicode format (decoded from utf8)
q = c.q = request.params.get('q', '')
c.q = request.params.get('q', '')

try:
# Do not query for the group datasets when dictizing, as they will
Expand Down Expand Up @@ -243,7 +243,8 @@ def _read(self, id, limit, group_type):
else:
q += ' groups:"%s"' % c.group_dict.get('name')

c.description_formatted = h.render_markdown(c.group_dict.get('description'))
c.description_formatted = \
h.render_markdown(c.group_dict.get('description'))

context['return_query'] = True

Expand Down Expand Up @@ -339,7 +340,8 @@ def pager_url(q=None, page=None):
'extras': search_extras
}

context_ = dict((k, v) for (k, v) in context.items() if k != 'schema')
context_ = dict((k, v) for (k, v) in context.items()
if k != 'schema')
query = get_action('package_search')(context_, data_dict)

c.page = h.Page(
Expand Down Expand Up @@ -371,8 +373,8 @@ def pager_url(q=None, page=None):
c.facets = {}
c.page = h.Page(collection=[])

self._setup_template_variables(context, {'id':id},
group_type=group_type)
self._setup_template_variables(context, {'id': id},
group_type=group_type)

def _update_facet_titles(self, facets, group_type):
for plugin in plugins.PluginImplementations(plugins.IFacets):
Expand Down Expand Up @@ -523,7 +525,7 @@ def edit(self, id, data=None, errors=None, error_summary=None):

try:
self._check_access('group_update', context)
except NotAuthorized, e:
except NotAuthorized:
abort(401, _('User %r not authorized to edit %s') % (c.user, id))

errors = errors or {}
Expand Down Expand Up @@ -708,19 +710,21 @@ def member_new(self, id):
'role': data_dict['role']
}
del data_dict['email']
user_dict = self._action('user_invite')(context,
user_data_dict)
user_dict = self._action('user_invite')(
context, user_data_dict)
data_dict['username'] = user_dict['name']

c.group_dict = self._action('group_member_create')(context, data_dict)

c.group_dict = self._action('group_member_create')(
context, data_dict)

self._redirect_to_this_controller(action='members', id=id)
else:
user = request.params.get('user')
if user:
c.user_dict = get_action('user_show')(context, {'id': user})
c.user_role = authz.users_role_for_group_or_org(id, user) or 'member'
c.user_dict = \
get_action('user_show')(context, {'id': user})
c.user_role = \
authz.users_role_for_group_or_org(id, user) or 'member'
else:
c.user_role = 'member'
except NotAuthorized:
Expand Down Expand Up @@ -748,7 +752,8 @@ def member_delete(self, id):
try:
user_id = request.params.get('user')
if request.method == 'POST':
self._action('group_member_delete')(context, {'id': id, 'user_id': user_id})
self._action('group_member_delete')(
context, {'id': id, 'user_id': user_id})
h.flash_notice(_('Group member has been deleted.'))
self._redirect_to_this_controller(action='members', id=id)
c.user_dict = self._action('user_show')(context, {'id': user_id})
Expand All @@ -758,7 +763,8 @@ def member_delete(self, id):
abort(401, _('Unauthorized to delete group %s') % '')
except NotFound:
abort(404, _('Group not found'))
return self._render_template('group/confirm_delete_member.html', group_type)
return self._render_template('group/confirm_delete_member.html',
group_type)

def history(self, id):
group_type = self._ensure_controller_matches_group_type(id)
Expand All @@ -768,7 +774,7 @@ def history(self, id):
'diff': request.params.getone('selected1'),
'oldid': request.params.getone('selected2'),
}
except KeyError, e:
except KeyError:
if 'group_name' in dict(request.params):
id = request.params.getone('group_name')
c.error = \
Expand Down Expand Up @@ -909,7 +915,8 @@ def followers(self, id):
'user': c.user or c.author}
c.group_dict = self._get_group_dict(id)
try:
c.followers = get_action('group_follower_list')(context, {'id': id})
c.followers = \
get_action('group_follower_list')(context, {'id': id})
except NotAuthorized:
abort(401, _('Unauthorized to view followers %s') % '')
return render('group/followers.html',
Expand Down Expand Up @@ -940,7 +947,8 @@ def _get_group_dict(self, id):
'user': c.user or c.author,
'for_view': True}
try:
return self._action('group_show')(context, {'id': id, 'include_datasets': False})
return self._action('group_show')(
context, {'id': id, 'include_datasets': False})
except NotFound:
abort(404, _('Group not found'))
except NotAuthorized:
Expand Down

0 comments on commit 5c6ab31

Please sign in to comment.