From 97b390f3c160368aad42f7753128e4b2705a36eb Mon Sep 17 00:00:00 2001 From: amercader Date: Tue, 6 Mar 2012 16:01:43 +0000 Subject: [PATCH] [auth,logic,ui] Handle publishers on the UI Add fields for publishers in the form when using the publihser auth profile. Some changes related to the source schema. --- ckanext/harvest/controllers/view.py | 49 ++++++++++++++++--- ckanext/harvest/logic/action/create.py | 2 +- ckanext/harvest/logic/action/update.py | 4 +- ckanext/harvest/logic/schema.py | 10 +++- .../templates/source/new_source_form.html | 12 +++++ ckanext/harvest/templates/source/read.html | 4 +- 6 files changed, 68 insertions(+), 13 deletions(-) diff --git a/ckanext/harvest/controllers/view.py b/ckanext/harvest/controllers/view.py index 899358a..dfe64b1 100644 --- a/ckanext/harvest/controllers/view.py +++ b/ckanext/harvest/controllers/view.py @@ -2,7 +2,9 @@ from lxml import etree from lxml.etree import XMLSyntaxError from pylons.i18n import _ +from ckan.authz import Authorizer from ckan import model +from ckan.model.group import Group import ckan.lib.helpers as h, json from ckan.lib.base import BaseController, c, g, request, \ @@ -19,6 +21,33 @@ class ViewController(BaseController): not_auth_message = _('Not authorized to see this page') + def __before__(self, action, **params): + + super(ViewController,self).__before__(action, **params) + + c.publisher_auth = (config.get('ckan.harvest.auth.profile',None) == 'publisher') + + def _get_publishers(self): + groups = None + + if c.publisher_auth: + if Authorizer().is_sysadmin(c.user): + groups = Group.all(group_type='publisher') + elif c.userobj: + groups = c.userobj.get_groups('publisher') + else: # anonymous user shouldn't have access to this page anyway. + groups = [] + + # Be explicit about which fields we make available in the template + groups = [ { + 'name': g.name, + 'id': g.id, + 'title': g.title, + } for g in groups ] + + return groups + + def index(self): context = {'model':model, 'user':c.user,'session':model.Session} try: @@ -46,6 +75,7 @@ class ViewController(BaseController): vars = {'data': data, 'errors': errors, 'error_summary': error_summary, 'harvesters': harvesters_info} + c.groups = self._get_publishers() c.form = render('source/new_source_form.html', extra_vars=vars) return render('source/new.html') @@ -53,7 +83,9 @@ class ViewController(BaseController): try: data_dict = dict(request.params) self._check_data_dict(data_dict) - context = {'model':model, 'user':c.user, 'session':model.Session} + context = {'model':model, 'user':c.user, 'session':model.Session, + 'schema':harvest_source_form_schema()} + source = get_action('harvest_source_create')(context,data_dict) # Create a harvest job for the new source @@ -61,7 +93,7 @@ class ViewController(BaseController): h.flash_success(_('New harvest source added successfully.' 'A new harvest job for the source has also been created.')) - redirect(h.url_for('harvest')) + redirect('/harvest/%s' % source['id']) except NotAuthorized,e: abort(401,self.not_auth_message) except DataError,e: @@ -98,6 +130,7 @@ class ViewController(BaseController): vars = {'data': data, 'errors': errors, 'error_summary': error_summary, 'harvesters': harvesters_info} + c.groups = self._get_publishers() c.form = render('source/new_source_form.html', extra_vars=vars) return render('source/edit.html') @@ -106,12 +139,13 @@ class ViewController(BaseController): data_dict = dict(request.params) data_dict['id'] = id self._check_data_dict(data_dict) - context = {'model':model, 'user':c.user, 'session':model.Session} + context = {'model':model, 'user':c.user, 'session':model.Session, + 'schema':harvest_source_form_schema()} source = get_action('harvest_source_update')(context,data_dict) h.flash_success(_('Harvest source edited successfully.')) - redirect(h.url_for('harvest')) + redirect('/harvest/%s' %id) except NotAuthorized,e: abort(401,self.not_auth_message) except DataError,e: @@ -125,11 +159,14 @@ class ViewController(BaseController): def _check_data_dict(self, data_dict): '''Check if the return data is correct''' - surplus_keys_schema = ['id','publisher_id','user_id','active','save','config'] - + surplus_keys_schema = ['id','publisher_id','user_id','config','save'] schema_keys = harvest_source_form_schema().keys() keys_in_schema = set(schema_keys) - set(surplus_keys_schema) + # user_id is not yet used, we'll set the logged user one for the time being + if not data_dict.get('user_id',None): + if c.userobj: + data_dict['user_id'] = c.userobj.id if keys_in_schema - set(data_dict.keys()): log.info(_('Incorrect form fields posted')) raise DataError(data_dict) diff --git a/ckanext/harvest/logic/action/create.py b/ckanext/harvest/logic/action/create.py index 6b0907b..63580a3 100644 --- a/ckanext/harvest/logic/action/create.py +++ b/ckanext/harvest/logic/action/create.py @@ -15,8 +15,8 @@ def harvest_source_create(context,data_dict): model = context['model'] session = context['session'] + schema = context.get('schema') or default_harvest_source_schema() - schema = harvest_source_form_schema() data, errors = validate(data_dict, schema) if errors: diff --git a/ckanext/harvest/logic/action/update.py b/ckanext/harvest/logic/action/update.py index 0aacf39..8b84155 100644 --- a/ckanext/harvest/logic/action/update.py +++ b/ckanext/harvest/logic/action/update.py @@ -28,14 +28,14 @@ def harvest_source_update(context,data_dict): session = context['session'] source_id = data_dict.get('id') - - schema = harvest_source_form_schema() + schema = context.get('schema') or default_harvest_source_schema() source = HarvestSource.get(source_id) if not source: raise NotFound('Harvest source %s does not exist' % source_id) data, errors = validate(data_dict, schema) + if errors: session.rollback() raise ValidationError(errors,_error_summary(errors)) diff --git a/ckanext/harvest/logic/schema.py b/ckanext/harvest/logic/schema.py index 231a530..c95c7c8 100644 --- a/ckanext/harvest/logic/schema.py +++ b/ckanext/harvest/logic/schema.py @@ -1,3 +1,5 @@ +from ckan.lib.base import config + from ckan.lib.navl.validators import (ignore_missing, not_empty, empty, @@ -20,11 +22,15 @@ def default_harvest_source_schema(): 'title': [ignore_missing,unicode], 'description': [ignore_missing,unicode], 'active': [ignore_missing,harvest_source_active_validator], - 'user_id': [ignore_missing], - 'publisher_id': [ignore_missing], + 'user_id': [ignore_missing,unicode], 'config': [ignore_missing,harvest_source_config_validator] } + if config.get('ckan.harvest.auth.profile',None) == 'publisher': + schema['publisher_id'] = [not_empty,unicode] + else: + schema['publisher_id'] = [ignore_missing,unicode] + return schema diff --git a/ckanext/harvest/templates/source/new_source_form.html b/ckanext/harvest/templates/source/new_source_form.html index d3c5adb..b98b2a7 100644 --- a/ckanext/harvest/templates/source/new_source_form.html +++ b/ckanext/harvest/templates/source/new_source_form.html @@ -44,6 +44,18 @@
You can add your own notes here about what the URL above represents to remind you later.
+ +
+
+ +
+
Cannot add any publishers.
+ +
diff --git a/ckanext/harvest/templates/source/read.html b/ckanext/harvest/templates/source/read.html index 3ca8348..a5fc697 100644 --- a/ckanext/harvest/templates/source/read.html +++ b/ckanext/harvest/templates/source/read.html @@ -51,11 +51,11 @@ - - + User ${c.source.user_id} - + Publisher ${c.source.publisher_id}