Skip to content

Commit

Permalink
Merge branch 'master' of github.com:okfn/ckan into 430-packages-missi…
Browse files Browse the repository at this point in the history
…ng-vocabulary-show
  • Loading branch information
Sean Hammond committed Mar 25, 2013
2 parents 9698b35 + 818e605 commit 45aae19
Show file tree
Hide file tree
Showing 72 changed files with 922 additions and 2,627 deletions.
124 changes: 63 additions & 61 deletions ckan/controllers/group.py
Expand Up @@ -4,32 +4,38 @@
import datetime
from urllib import urlencode

from ckan.lib.base import BaseController, c, model, request, render, g
from ckan.lib.base import ValidationException, abort, gettext
from pylons.i18n import get_lang

import ckan.lib.base as base
from pylons.i18n import get_lang, _
import ckan.lib.helpers as h
import ckan.lib.maintain as maintain
from ckan.lib.navl.dictization_functions import DataError, unflatten, validate
from ckan.logic import NotFound, NotAuthorized, ValidationError
from ckan.logic import check_access, get_action
from ckan.logic import tuplize_dict, clean_dict, parse_params
import ckan.logic.action.get
import ckan.lib.navl.dictization_functions as dict_fns
import ckan.logic as logic
import ckan.lib.search as search
import ckan.new_authz

from ckan.lib.plugins import lookup_group_plugin
import ckan.model as model
import ckan.new_authz as new_authz
import ckan.lib.plugins
import ckan.plugins as plugins

try:
from collections import OrderedDict # 2.7
except ImportError:
from sqlalchemy.util import OrderedDict
from ckan.common import OrderedDict, c, g, request, _

log = logging.getLogger(__name__)

render = base.render
abort = base.abort

NotFound = logic.NotFound
NotAuthorized = logic.NotAuthorized
ValidationError = logic.ValidationError
check_access = logic.check_access
get_action = logic.get_action
tuplize_dict = logic.tuplize_dict
clean_dict = logic.clean_dict
parse_params = logic.parse_params

lookup_group_plugin = ckan.lib.plugins.lookup_group_plugin


class GroupController(BaseController):
class GroupController(base.BaseController):

group_type = 'group'

Expand All @@ -48,7 +54,7 @@ def _db_to_form_schema(self, group_type=None):

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

def _new_template(self, group_type):
return lookup_group_plugin(group_type).new_template()
Expand Down Expand Up @@ -77,7 +83,6 @@ def _admins_template(self, group_type):
def _bulk_process_template(self, group_type):
return lookup_group_plugin(group_type).bulk_process_template()


## end hooks
def _replace_group_org(self, string):
''' substitute organization for group if this is an org'''
Expand Down Expand Up @@ -134,8 +139,7 @@ def index(self):
'with_private': False}

q = c.q = request.params.get('q', '')
data_dict = {'all_fields': True,
'q': q,}
data_dict = {'all_fields': True, 'q': q}
sort_by = c.sort_by_selected = request.params.get('sort')
if sort_by:
data_dict['sort'] = sort_by
Expand Down Expand Up @@ -199,7 +203,7 @@ def _read(self, id, limit):

try:
description_formatted = ckan.misc.MarkdownFormat().to_html(
c.group_dict.get('description', ''))
c.group_dict.get('description', ''))
c.description_formatted = genshi.HTML(description_formatted)
except Exception, e:
error_msg = "<span class='inline-warning'>%s</span>" %\
Expand All @@ -210,7 +214,7 @@ def _read(self, id, limit):

# c.group_admins is used by CKAN's legacy (Genshi) templates only,
# if we drop support for those then we can delete this line.
c.group_admins = ckan.new_authz.get_group_or_org_admin_ids(c.group.id)
c.group_admins = new_authz.get_group_or_org_admin_ids(c.group.id)

try:
page = int(request.params.get('page', 1))
Expand All @@ -234,8 +238,7 @@ def search_url(params):
action='read',
id=id)
else:
url = self._url_for(controller='group', action='read',
id=id)
url = self._url_for(controller='group', action='read', id=id)
params = [(k, v.encode('utf-8') if isinstance(v, basestring)
else str(v)) for k, v in params]
return url + u'?' + urlencode(params)
Expand All @@ -250,8 +253,8 @@ def drill_down_url(**by):

def remove_field(key, value=None, replace=None):
return h.remove_url_param(key, value=value, replace=replace,
controller='group', action='read',
extras=dict(id=c.group_dict.get('name')))
controller='group', action='read',
extras=dict(id=c.group_dict.get('name')))

c.remove_field = remove_field

Expand Down Expand Up @@ -283,9 +286,9 @@ def pager_url(q=None, page=None):
facets = OrderedDict()

default_facet_titles = {'groups': _('Groups'),
'tags': _('Tags'),
'res_format': _('Formats'),
'license': _('Licence'), }
'tags': _('Tags'),
'res_format': _('Formats'),
'license': _('Licence')}

for facet in g.facets:
if facet in default_facet_titles:
Expand All @@ -302,7 +305,8 @@ def pager_url(q=None, page=None):
facets = plugin.group_facets(
facets, self.group_type, None)

if 'capacity' in facets and (self.group_type != 'organization' or not user_member_of_orgs):
if 'capacity' in facets and (self.group_type != 'organization' or
not user_member_of_orgs):
del facets['capacity']

c.facet_titles = facets
Expand All @@ -328,9 +332,8 @@ def pager_url(q=None, page=None):
)

c.facets = query['facets']
maintain.deprecate_context_item(
'facets',
'Use `c.search_facets` instead.')
maintain.deprecate_context_item('facets',
'Use `c.search_facets` instead.')

c.search_facets = query['search_facets']
c.search_facets_limits = {}
Expand All @@ -347,15 +350,13 @@ def pager_url(q=None, page=None):
c.facets = {}
c.page = h.Page(collection=[])



def bulk_process(self, id):
''' Allow bulk processing of datasets for an organization. Make
private/public or delete. For organization admins.'''

group_type = self._get_group_type(id.split('@')[0])

if group_type != 'organization':
if group_type != 'organization':
# FIXME: better error
raise Exception('Must be an organization')

Expand Down Expand Up @@ -385,7 +386,8 @@ def bulk_process(self, id):
c.packages = c.page.items
return render(self._bulk_process_template(group_type))

# process the action first find the datasets to perform the action on. they are prefixed by dataset_ in the form data
# process the action first find the datasets to perform the action on.
# they are prefixed by dataset_ in the form data
datasets = []
for param in request.params:
if param.startswith('dataset_'):
Expand All @@ -403,8 +405,9 @@ def bulk_process(self, id):
get_action(action_functions[action])(context, data_dict)
except NotAuthorized:
abort(401, _('Not authorized to perform bulk update'))
base.redirect(h.url_for(controller='organization', action='bulk_process',
id=id))
base.redirect(h.url_for(controller='organization',
action='bulk_process',
id=id))

def new(self, data=None, errors=None, error_summary=None):
group_type = self._guess_group_type(True)
Expand Down Expand Up @@ -486,7 +489,7 @@ def _get_group_type(self, id):

def _save_new(self, context, group_type=None):
try:
data_dict = clean_dict(unflatten(
data_dict = clean_dict(dict_fns.unflatten(
tuplize_dict(parse_params(request.params))))
data_dict['type'] = group_type or 'group'
context['message'] = data_dict.get('log_message', '')
Expand All @@ -499,7 +502,7 @@ def _save_new(self, context, group_type=None):
abort(401, _('Unauthorized to read group %s') % '')
except NotFound, e:
abort(404, _('Group not found'))
except DataError:
except dict_fns.DataError:
abort(400, _(u'Integrity Error'))
except ValidationError, e:
errors = e.error_dict
Expand All @@ -517,7 +520,7 @@ def _force_reindex(self, grp):

def _save_edit(self, id, context):
try:
data_dict = clean_dict(unflatten(
data_dict = clean_dict(dict_fns.unflatten(
tuplize_dict(parse_params(request.params))))
context['message'] = data_dict.get('log_message', '')
data_dict['id'] = id
Expand All @@ -532,7 +535,7 @@ def _save_edit(self, id, context):
abort(401, _('Unauthorized to read group %s') % id)
except NotFound, e:
abort(404, _('Group not found'))
except DataError:
except dict_fns.DataError:
abort(400, _(u'Integrity Error'))
except ValidationError, e:
errors = e.error_dict
Expand All @@ -556,8 +559,8 @@ def authz(self, id):
c.authz_editable = False
if not c.authz_editable:
abort(401,
gettext('User %r not authorized to edit %s authorizations') %
(c.user, id))
_('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)
Expand Down Expand Up @@ -595,8 +598,9 @@ def members(self, id):
'user': c.user or c.author}

try:
c.members = self._action('member_list')(context, {'id': id,
'object_type': 'user'})
c.members = self._action('member_list')(
context, {'id': id, 'object_type': 'user'}
)
c.group_dict = self._action('group_show')(context, {'id': id})
except NotAuthorized:
abort(401, _('Unauthorized to delete group %s') % '')
Expand All @@ -611,7 +615,7 @@ def member_new(self, id):
#self._check_access('group_delete', context, {'id': id})
try:
if request.method == 'POST':
data_dict = clean_dict(unflatten(
data_dict = clean_dict(dict_fns.unflatten(
tuplize_dict(parse_params(request.params))))
data_dict['id'] = id
c.group_dict = self._action('group_member_create')(context, data_dict)
Expand All @@ -620,7 +624,7 @@ def member_new(self, id):
user = request.params.get('user')
if user:
c.user_dict = get_action('user_show')(context, {'id': user})
c.user_role = ckan.new_authz.users_role_for_group_or_org(id, user) or 'member'
c.user_role = new_authz.users_role_for_group_or_org(id, user) or 'member'
else:
c.user_role = 'member'
c.group_dict = self._action('group_show')(context, {'id': id})
Expand Down Expand Up @@ -658,7 +662,6 @@ def member_delete(self, id):
abort(404, _('Group not found'))
return self._render_template('group/confirm_delete_member.html')


def history(self, id):
if 'diff' in request.params or 'selected1' in request.params:
try:
Expand All @@ -682,7 +685,7 @@ def history(self, id):
try:
c.group_dict = self._action('group_show')(context, data_dict)
c.group_revisions = self._action('group_revision_list')(context,
data_dict)
data_dict)
#TODO: remove
# Still necessary for the authz check in group/layout.html
c.group = context['group']
Expand All @@ -698,7 +701,7 @@ def history(self, id):
feed = Atom1Feed(
title=_(u'CKAN Group Revision History'),
link=self._url_for(controller='group', action='read',
id=c.group_dict['name']),
id=c.group_dict['name']),
description=_(u'Recent changes to CKAN Group: ') +
c.group_dict['display_name'],
language=unicode(get_lang()),
Expand Down Expand Up @@ -755,7 +758,7 @@ def activity(self, id, offset=0):
# 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 = get_action('group_activity_list_html')(
context, {'id': c.group_dict['id'], 'offset': offset})
context, {'id': c.group_dict['id'], 'offset': offset})

#return render('group/activity_stream.html')
return render(self._activity_template(c.group_dict['type']))
Expand All @@ -773,7 +776,7 @@ def follow(self, id):
group_dict['title']))
except ValidationError as e:
error_message = (e.extra_msg or e.message or e.error_summary
or e.error_dict)
or e.error_dict)
h.flash_error(error_message)
except NotAuthorized as e:
h.flash_error(e.extra_msg)
Expand All @@ -792,7 +795,7 @@ def unfollow(self, id):
group_dict['title']))
except ValidationError as e:
error_message = (e.extra_msg or e.message or e.error_summary
or e.error_dict)
or e.error_dict)
h.flash_error(error_message)
except (NotFound, NotAuthorized) as e:
error_message = e.extra_msg or e.message
Expand All @@ -804,15 +807,14 @@ 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')

def admins(self, id):
c.group_dict = self._get_group_dict(id)
c.admins = ckan.new_authz.get_group_or_org_admin_ids(id)
c.admins = new_authz.get_group_or_org_admin_ids(id)
return render(self._admins_template(c.group_dict['type']))

def about(self, id):
Expand Down Expand Up @@ -845,7 +847,7 @@ def _update(self, fs, group_name, group_id):
validation = fs.validate()
if not validation:
c.form = self._render_edit_form(fs)
raise ValidationException(fs)
raise base.ValidationException(fs)

try:
fs.sync()
Expand All @@ -859,7 +861,7 @@ def _update_authz(self, fs):
validation = fs.validate()
if not validation:
c.form = self._render_edit_form(fs)
raise ValidationException(fs)
raise base.ValidationException(fs)
try:
fs.sync()
except Exception, inst:
Expand Down

0 comments on commit 45aae19

Please sign in to comment.