Skip to content

Commit

Permalink
PEP8 cleanup
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Jun 18, 2012
1 parent 4a7f480 commit ea92af3
Showing 1 changed file with 63 additions and 51 deletions.
114 changes: 63 additions & 51 deletions ckan/controllers/group.py
Expand Up @@ -13,11 +13,15 @@
from ckan.logic import tuplize_dict, clean_dict, parse_params
import ckan.forms
import ckan.logic.action.get
import ckan.lib.search as search

from ckan.lib.plugins import lookup_group_plugin

log = logging.getLogger(__name__)

group_activity_list_html = ckan.action.logic.get.group_activity_list_html


class GroupController(BaseController):

## hooks for subclasses
Expand All @@ -34,12 +38,13 @@ def _db_to_form_schema(self, group_type=None):
return lookup_group_plugin(group_type).form_to_db_schema()

def _setup_template_variables(self, context, data_dict, group_type=None):
return lookup_group_plugin(group_type).setup_template_variables(context,data_dict)
return lookup_group_plugin(group_type).\
setup_template_variables(context, data_dict)

def _new_template(self,group_type):
def _new_template(self, group_type):
return lookup_group_plugin(group_type).new_template()

def _index_template(self,group_type):
def _index_template(self, group_type):
return lookup_group_plugin(group_type).index_template()

def _read_template(self, group_type):
Expand Down Expand Up @@ -67,7 +72,6 @@ def _guess_group_type(self, expecting_name=False):

return gt


def index(self):
group_type = self._guess_group_type()

Expand All @@ -90,8 +94,7 @@ def index(self):
url=h.pager_url,
items_per_page=20
)
return render( self._index_template(group_type) )

return render(self._index_template(group_type))

def read(self, id):
from ckan.lib.search import SearchError
Expand All @@ -101,7 +104,8 @@ def read(self, id):
'schema': self._form_to_db_schema(group_type=group_type),
'for_view': True}
data_dict = {'id': id}
q = c.q = request.params.get('q', '') # unicode format (decoded from utf8)
# unicode format (decoded from utf8)
q = c.q = request.params.get('q', '')

try:
c.group_dict = get_action('group_show')(context, data_dict)
Expand All @@ -115,10 +119,12 @@ def read(self, id):
q += ' groups: "%s"' % c.group_dict.get('name')

try:
description_formatted = ckan.misc.MarkdownFormat().to_html(c.group_dict.get('description',''))
description_formatted = ckan.misc.MarkdownFormat().to_html(
c.group_dict.get('description', ''))
c.description_formatted = genshi.HTML(description_formatted)
except Exception, e:
error_msg = "<span class='inline-warning'>%s</span>" % _("Cannot render description")
error_msg = "<span class='inline-warning'>%s</span>" %\
_("Cannot render description")
c.description_formatted = genshi.HTML(error_msg)

c.group_admins = self.authorizer.get_admins(c.group)
Expand All @@ -132,12 +138,14 @@ def read(self, id):
abort(400, ('"page" parameter must be an integer'))

# most search operations should reset the page counter:
params_nopage = [(k, v) for k,v in request.params.items() if k != 'page']
params_nopage = [(k, v) for k, v in request.params.items()
if k != 'page']

def search_url(params):
url = h.url_for(controller='group', action='read', id=c.group_dict.get('name'))
params = [(k, v.encode('utf-8') if isinstance(v, basestring) else str(v)) \
for k, v in params]
url = h.url_for(controller='group', action='read',
id=c.group_dict.get('name'))
params = [(k, v.encode('utf-8') if isinstance(v, basestring)
else str(v)) for k, v in params]
return url + u'?' + urlencode(params)

def drill_down_url(**by):
Expand Down Expand Up @@ -171,22 +179,21 @@ def pager_url(q=None, page=None):
else:
search_extras[param] = value


fq = 'capacity:"public"'
if (c.userobj and c.group and c.userobj.is_in_group(c.group)):
fq = ''
context['ignore_capacity_check'] = True

data_dict = {
'q':q,
'fq':fq,
'facet.field':g.facets,
'rows':limit,
'start':(page-1)*limit,
'extras':search_extras
'q': q,
'fq': fq,
'facet.field': g.facets,
'rows': limit,
'start': (page - 1) * limit,
'extras': search_extras
}

query = get_action('package_search')(context,data_dict)
query = get_action('package_search')(context, data_dict)

c.page = h.Page(
collection=query['results'],
Expand All @@ -207,10 +214,9 @@ def pager_url(q=None, page=None):
# Add the group's activity stream (already rendered to HTML) to the
# template context for the group/read.html template to retrieve later.
c.group_activity_stream = \
ckan.logic.action.get.group_activity_list_html(context,
{'id': c.group_dict['id']})
group_activity_list_html(context, {'id': c.group_dict['id']})

return render( self._read_template(c.group_dict['type']) )
return render(self._read_template(c.group_dict['type']))

def new(self, data=None, errors=None, error_summary=None):
group_type = self._guess_group_type(True)
Expand All @@ -222,7 +228,7 @@ def new(self, data=None, errors=None, error_summary=None):
'save': 'save' in request.params,
'parent': request.params.get('parent', None)}
try:
check_access('group_create',context)
check_access('group_create', context)
except NotAuthorized:
abort(401, _('Unauthorized to create a group'))

Expand All @@ -234,8 +240,9 @@ def new(self, data=None, errors=None, error_summary=None):
error_summary = error_summary or {}
vars = {'data': data, 'errors': errors, 'error_summary': error_summary}

self._setup_template_variables(context,data)
c.form = render(self._group_form(group_type=group_type), extra_vars=vars)
self._setup_template_variables(context, data)
c.form = render(self._group_form(group_type=group_type),
extra_vars=vars)
return render(self._new_template(group_type))

def edit(self, id, data=None, errors=None, error_summary=None):
Expand Down Expand Up @@ -265,7 +272,7 @@ def edit(self, id, data=None, errors=None, error_summary=None):
c.group = group

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

Expand All @@ -281,10 +288,9 @@ def _get_group_type(self, id):
Given the id of a group it determines the type of a group given
a valid id/name for the group.
"""
group = model.Group.get( id )
group = model.Group.get(id)
if not group:
return None

return group.type

def _save_new(self, context, group_type=None):
Expand All @@ -297,7 +303,7 @@ def _save_new(self, context, group_type=None):
group = get_action('group_create')(context, data_dict)

# Redirect to the appropriate _read route for the type of group
h.redirect_to( group['type'] + '_read', id=group['name'])
h.redirect_to(group['type'] + '_read', id=group['name'])
except NotAuthorized:
abort(401, _('Unauthorized to read group %s') % '')
except NotFound, e:
Expand All @@ -309,16 +315,14 @@ def _save_new(self, context, group_type=None):
error_summary = e.error_summary
return self.new(data_dict, errors, error_summary)

def _force_reindex(self,grp):
def _force_reindex(self, grp):
""" When the group name has changed, we need to force a reindex
of the datasets within the group, otherwise they will stop
appearing on the read page for the group (as they're connected via
the group name)"""
from ckan.lib.search import rebuild

group = model.Group.get(grp['name'])
for dataset in group.active_packages().all():
rebuild( dataset.name )
search.rebuild(dataset.name)

def _save_edit(self, id, context):
try:
Expand Down Expand Up @@ -351,31 +355,34 @@ def authz(self, id):
c.grouptitle = group.display_name

try:
context = {'model':model,'user':c.user or c.author, 'group':group}
check_access('group_edit_permissions',context)
context = \
{'model': model, 'user': c.user or c.author, 'group': group}
check_access('group_edit_permissions', context)
c.authz_editable = True
c.group = context['group']
except NotAuthorized:
c.authz_editable = False
if not c.authz_editable:
abort(401, gettext('User %r not authorized to edit %s authorizations') % (c.user, id))
abort(401,
gettext('User %r not authorized to edit %s authorizations') %
(c.user, id))

roles = self._handle_update_of_authz(group)
self._prepare_authz_info_for_render(roles)
return render('group/authz.html')


def history(self, id):
if 'diff' in request.params or 'selected1' in request.params:
try:
params = {'id':request.params.getone('group_name'),
'diff':request.params.getone('selected1'),
'oldid':request.params.getone('selected2'),
params = {'id': request.params.getone('group_name'),
'diff': request.params.getone('selected1'),
'oldid': request.params.getone('selected2'),
}
except KeyError, e:
if dict(request.params).has_key('group_name'):
if 'group_name' in dict(request.params):
id = request.params.getone('group_name')
c.error = _('Select two revisions before doing the comparison.')
c.error = \
_('Select two revisions before doing the comparison.')
else:
params['diff_entity'] = 'group'
h.redirect_to(controller='revision', action='diff', **params)
Expand All @@ -386,7 +393,8 @@ def history(self, id):
data_dict = {'id': id}
try:
c.group_dict = get_action('group_show')(context, data_dict)
c.group_revisions = get_action('group_revision_list')(context, data_dict)
c.group_revisions = get_action('group_revision_list')(context,
data_dict)
#TODO: remove
# Still necessary for the authz check in group/layout.html
c.group = context['group']
Expand All @@ -401,13 +409,15 @@ def history(self, id):
from webhelpers.feedgenerator import Atom1Feed
feed = Atom1Feed(
title=_(u'CKAN Group Revision History'),
link=h.url_for(controller='group', action='read', id=c.group_dict['name']),
link=h.url_for(controller='group', action='read',
id=c.group_dict['name']),
description=_(u'Recent changes to CKAN Group: ') +
c.group_dict['display_name'],
c.group_dict['display_name'],
language=unicode(get_lang()),
)
for revision_dict in c.group_revisions:
revision_date = h.date_str_to_datetime(revision_dict['timestamp'])
revision_date = h.date_str_to_datetime(
revision_dict['timestamp'])
try:
dayHorizon = int(request.params.get('days'))
except:
Expand All @@ -416,10 +426,12 @@ def history(self, id):
if dayAge >= dayHorizon:
break
if revision_dict['message']:
item_title = u'%s' % revision_dict['message'].split('\n')[0]
item_title = u'%s' % revision_dict['message'].\
split('\n')[0]
else:
item_title = u'%s' % revision_dict['id']
item_link = h.url_for(controller='revision', action='read', id=revision_dict['id'])
item_link = h.url_for(controller='revision', action='read',
id=revision_dict['id'])
item_description = _('Log message: ')
item_description += '%s' % (revision_dict['message'] or '')
item_author_name = revision_dict['author']
Expand All @@ -433,7 +445,7 @@ def history(self, id):
)
feed.content_type = 'application/atom+xml'
return feed.writeString('utf-8')
return render( self._history_template(c.group_dict['type']) )
return render(self._history_template(c.group_dict['type']))

def _render_edit_form(self, fs):
# errors arrive in c.error and fs.errors
Expand Down

0 comments on commit ea92af3

Please sign in to comment.