Skip to content

Commit

Permalink
[2255] Cleanup of the new IDatasetForm and make sure the process work…
Browse files Browse the repository at this point in the history
…s with the publisher auth
  • Loading branch information
rossjones committed Mar 23, 2012
1 parent 7200b10 commit 6a386ae
Show file tree
Hide file tree
Showing 5 changed files with 32 additions and 47 deletions.
1 change: 1 addition & 0 deletions ckan/lib/plugins.py
Original file line number Diff line number Diff line change
Expand Up @@ -231,6 +231,7 @@ def db_to_form_schema(self):
'''This is an interface to manipulate data from the database
into a format suitable for the form (optional)'''


def check_data_dict(self, data_dict, schema=None):
'''Check if the return data is correct, mostly for checking out
if spammers are submitting only part of the form'''
Expand Down
1 change: 1 addition & 0 deletions ckan/logic/schema.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,7 @@ def default_package_schema():
'id': [ignore_missing, unicode],
'name': [ignore_missing, unicode],
'title': [ignore_missing, unicode],
'type': [ignore_missing, unicode],
'__extras': [ignore],
}
}
Expand Down
44 changes: 15 additions & 29 deletions ckanext/organizations/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@

import ckan.authz as authz
from ckan.logic import NotAuthorized
from ckan.logic.schema import group_form_schema
from ckan.logic.schema import group_form_schema, default_package_schema
from ckan.lib import base
from ckan.lib.base import c, model, abort, request
from ckan.lib.base import redirect, _, config, h
Expand Down Expand Up @@ -116,7 +116,7 @@ def db_to_form_schema(self):
Returns the schema for mapping group data from the database into a
format suitable for the form (optional)
"""
return {}
return group_form_schema()

def check_data_dict(self, data_dict):
"""
Expand Down Expand Up @@ -200,29 +200,14 @@ def history_template(self):
def package_form(self):
return 'organization_package_form.html'

def form_to_db_schema_options(self, options):
''' This allows us to select different schemas for different
purpose eg via the web interface or via the api or creation vs
updating. It is optional and if not available form_to_db_schema
should be used.
If a context is provided, and it contains a schema, it will be
returned.
'''
schema = options.get('context',{}).get('schema',None)
if schema:
return schema

if options.get('api'):
if options.get('type') == 'create':
return logic.schema.default_create_package_schema()
else:
return logic.schema.default_update_package_schema()
else:
return logic.schema.package_form_schema()

def db_to_form_schema(self):
'''This is an interface to manipulate data from the database
into a format suitable for the form (optional)'''
return default_package_schema()

def form_to_db_schema(self):
return default_package_schema()

def check_data_dict(self, data_dict, schema=None):
'''Check if the return data is correct, mostly for checking out
Expand All @@ -233,22 +218,23 @@ def check_data_dict(self, data_dict, schema=None):
'extras_validation', 'save', 'return_to',
'resources', 'type']

if not schema:
schema = self.form_to_db_schema()
schema_keys = schema.keys()
keys_in_schema = set(schema_keys) - set(surplus_keys_schema)
# if not schema:
# schema = self.form_to_db_schema()
# schema_keys = schema.keys()
# keys_in_schema = set(schema_keys) - set(surplus_keys_schema)

missing_keys = keys_in_schema - set(data_dict.keys())
if missing_keys:
log.info('incorrect form fields posted, missing %s' % missing_keys)
raise dictization_functions.DataError(data_dict)
# missing_keys = keys_in_schema - set(data_dict.keys())
# if missing_keys:
# log.info('incorrect form fields posted, missing %s' % missing_keys)
# raise DataError(data_dict)

def setup_template_variables(self, context, data_dict):
from pylons import config

data_dict.update({'available_only':True})

c.groups_available = c.userobj.get_groups('organization')
print c.groups_available
c.licences = [('', '')] + base.model.Package.get_license_options()
c.is_sysadmin = authz.Authorizer().is_sysadmin(c.user)

Expand Down
4 changes: 2 additions & 2 deletions ckanext/organizations/templates/organization_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -104,8 +104,8 @@ <h3>Extras</h3>
<h3>Users</h3>
<dl py:if="data.get('users')">
<py:for each="num, user in enumerate(data.get('users'))">
<dt py:if="c.is_superuser_or_groupadmin"><input checked="checked" id="users__${num}__name" name="users__${num}__name" type="checkbox" value="${user['name']}"/>
</dt>
<dt py:if="c.is_superuser_or_groupadmin"><input checked="checked" id="users__${num}__name" name="users__${num}__name" type="checkbox" value="${user['name']}" onclick="$('input[name=users__${num}__capacity]').val('')"/>
</dt>
<dd>
<label for="users__${num}__name">${user['name']} ${user['capacity']}</label>
</dd>
Expand Down
29 changes: 13 additions & 16 deletions ckanext/organizations/templates/organization_package_form.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,25 +84,22 @@ <h2>Errors in form</h2>
</dt>
<dd class="groups-field">
<dl>
<py:for each="num, group in enumerate(data.get('groups', []))">
<?python
authorized_group = [group_authz for group_authz in c.groups_authz if group_authz['id'] == group['id']]
authorized_group = authorized_group[0] if authorized_group else None
?>

<dt py:if="'id' in group">
<input type="${'checkbox' if authorized_group else 'hidden'}" name="groups__${num}__id" checked="checked" value="${group['id']}" />
<input type="hidden" name="groups__${num}__name" value="${group.get('name', authorized_group['name'] if authorized_group else '')}" />
</dt>
<dd py:if="'id' in group"><label for="groups__${num}__checked">${group.get('name', authorized_group['name'] if authorized_group else '')}</label></dd>
</py:for>

<select id="groups__${len(data.get('groups', []))}__id" name="groups__${len(data.get('groups', []))}__id" class="chzn-select">
<option selected="selected" value="">(None)</option>
<?python
organization = data.get('groups', {})
if organization:
organization = organization[0]
?>
<py:if test="(not 'name' in organization) or c.is_sysadmin or c.auth_for_change_state">
<select id="groups__0__id" name="groups__0__id" class="chzn-select">
<option value="">(None)</option>
<py:for each="group in c.groups_available">
<option value="${group['id']}" >${group['name']}</option>
<option value="${group['id']}" py:attrs="{'selected':'selected'} if organization.get('id','') == group['id'] else {}">${group['title']}</option>
</py:for>
</select>
</py:if>
<py:if test="not (c.is_sysadmin or c.auth_for_change_state)">
<span>${organization.get('title', '')}</span>
</py:if>
<dd py:if="not c.groups_available"><em>Cannot add any organizations.</em></dd>
</dl>
</dd>
Expand Down

0 comments on commit 6a386ae

Please sign in to comment.