[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.
This commit is contained in:
amercader 2012-03-06 16:01:43 +00:00
parent aea785701f
commit 97b390f3c1
6 changed files with 68 additions and 13 deletions

View File

@ -2,7 +2,9 @@ from lxml import etree
from lxml.etree import XMLSyntaxError from lxml.etree import XMLSyntaxError
from pylons.i18n import _ from pylons.i18n import _
from ckan.authz import Authorizer
from ckan import model from ckan import model
from ckan.model.group import Group
import ckan.lib.helpers as h, json import ckan.lib.helpers as h, json
from ckan.lib.base import BaseController, c, g, request, \ 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') 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): def index(self):
context = {'model':model, 'user':c.user,'session':model.Session} context = {'model':model, 'user':c.user,'session':model.Session}
try: try:
@ -46,6 +75,7 @@ class ViewController(BaseController):
vars = {'data': data, 'errors': errors, 'error_summary': error_summary, 'harvesters': harvesters_info} 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) c.form = render('source/new_source_form.html', extra_vars=vars)
return render('source/new.html') return render('source/new.html')
@ -53,7 +83,9 @@ class ViewController(BaseController):
try: try:
data_dict = dict(request.params) data_dict = dict(request.params)
self._check_data_dict(data_dict) 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) source = get_action('harvest_source_create')(context,data_dict)
# Create a harvest job for the new source # Create a harvest job for the new source
@ -61,7 +93,7 @@ class ViewController(BaseController):
h.flash_success(_('New harvest source added successfully.' h.flash_success(_('New harvest source added successfully.'
'A new harvest job for the source has also been created.')) 'A new harvest job for the source has also been created.'))
redirect(h.url_for('harvest')) redirect('/harvest/%s' % source['id'])
except NotAuthorized,e: except NotAuthorized,e:
abort(401,self.not_auth_message) abort(401,self.not_auth_message)
except DataError,e: except DataError,e:
@ -98,6 +130,7 @@ class ViewController(BaseController):
vars = {'data': data, 'errors': errors, 'error_summary': error_summary, 'harvesters': harvesters_info} 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) c.form = render('source/new_source_form.html', extra_vars=vars)
return render('source/edit.html') return render('source/edit.html')
@ -106,12 +139,13 @@ class ViewController(BaseController):
data_dict = dict(request.params) data_dict = dict(request.params)
data_dict['id'] = id data_dict['id'] = id
self._check_data_dict(data_dict) 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) source = get_action('harvest_source_update')(context,data_dict)
h.flash_success(_('Harvest source edited successfully.')) h.flash_success(_('Harvest source edited successfully.'))
redirect(h.url_for('harvest')) redirect('/harvest/%s' %id)
except NotAuthorized,e: except NotAuthorized,e:
abort(401,self.not_auth_message) abort(401,self.not_auth_message)
except DataError,e: except DataError,e:
@ -125,11 +159,14 @@ class ViewController(BaseController):
def _check_data_dict(self, data_dict): def _check_data_dict(self, data_dict):
'''Check if the return data is correct''' '''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() schema_keys = harvest_source_form_schema().keys()
keys_in_schema = set(schema_keys) - set(surplus_keys_schema) 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()): if keys_in_schema - set(data_dict.keys()):
log.info(_('Incorrect form fields posted')) log.info(_('Incorrect form fields posted'))
raise DataError(data_dict) raise DataError(data_dict)

View File

@ -15,8 +15,8 @@ def harvest_source_create(context,data_dict):
model = context['model'] model = context['model']
session = context['session'] session = context['session']
schema = context.get('schema') or default_harvest_source_schema()
schema = harvest_source_form_schema()
data, errors = validate(data_dict, schema) data, errors = validate(data_dict, schema)
if errors: if errors:

View File

@ -28,14 +28,14 @@ def harvest_source_update(context,data_dict):
session = context['session'] session = context['session']
source_id = data_dict.get('id') source_id = data_dict.get('id')
schema = context.get('schema') or default_harvest_source_schema()
schema = harvest_source_form_schema()
source = HarvestSource.get(source_id) source = HarvestSource.get(source_id)
if not source: if not source:
raise NotFound('Harvest source %s does not exist' % source_id) raise NotFound('Harvest source %s does not exist' % source_id)
data, errors = validate(data_dict, schema) data, errors = validate(data_dict, schema)
if errors: if errors:
session.rollback() session.rollback()
raise ValidationError(errors,_error_summary(errors)) raise ValidationError(errors,_error_summary(errors))

View File

@ -1,3 +1,5 @@
from ckan.lib.base import config
from ckan.lib.navl.validators import (ignore_missing, from ckan.lib.navl.validators import (ignore_missing,
not_empty, not_empty,
empty, empty,
@ -20,11 +22,15 @@ def default_harvest_source_schema():
'title': [ignore_missing,unicode], 'title': [ignore_missing,unicode],
'description': [ignore_missing,unicode], 'description': [ignore_missing,unicode],
'active': [ignore_missing,harvest_source_active_validator], 'active': [ignore_missing,harvest_source_active_validator],
'user_id': [ignore_missing], 'user_id': [ignore_missing,unicode],
'publisher_id': [ignore_missing],
'config': [ignore_missing,harvest_source_config_validator] '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 return schema

View File

@ -44,6 +44,18 @@
<dt><label class="field_opt" for="description">Description</label></dt> <dt><label class="field_opt" for="description">Description</label></dt>
<dd><textarea id="description" name="description" cols="30" rows="2" style="height:75px">${data.get('description', '')}</textarea></dd> <dd><textarea id="description" name="description" cols="30" rows="2" style="height:75px">${data.get('description', '')}</textarea></dd>
<dd class="instructions basic">You can add your own notes here about what the URL above represents to remind you later.</dd> <dd class="instructions basic">You can add your own notes here about what the URL above represents to remind you later.</dd>
<dt py:if="c.publisher_auth"><label class="field_opt" for="groups__${len(data.get('groups', []))}__id">Publisher</label></dt>
<dd py:if="c.publisher_auth and c.groups">
<select id="publisher_id" name="publisher_id">
<py:for each="group in c.groups">
<option value="${group['id']}" py:attrs="{'selected': 'selected' if group['id'] == data.get('publisher_id',None) else None}">${group['title']}</option>
</py:for>
</select>
</dd>
<dd py:if="c.publisher_auth and not c.groups"><em>Cannot add any publishers.</em></dd>
<dt><label class="field_opt" for="config">Configuration</label></dt> <dt><label class="field_opt" for="config">Configuration</label></dt>
<dd><textarea id="config" name="config" cols="30" rows="2" style="height:75px">${data.get('config', '')}</textarea></dd> <dd><textarea id="config" name="config" cols="30" rows="2" style="height:75px">${data.get('config', '')}</textarea></dd>

View File

@ -51,11 +51,11 @@
<td>-</td> <td>-</td>
</py:if> </py:if>
</tr> </tr>
<tr> <tr py:if="c.publisher_auth">
<th>User</th> <th>User</th>
<td>${c.source.user_id}</td> <td>${c.source.user_id}</td>
</tr> </tr>
<tr> <tr py:if="c.publisher_auth">
<th>Publisher</th> <th>Publisher</th>
<td>${c.source.publisher_id}</td> <td>${c.source.publisher_id}</td>
</tr> </tr>