Skip to content

Commit

Permalink
[2211] Merge of refactor for IDatasetForm in publisher_form
Browse files Browse the repository at this point in the history
  • Loading branch information
rossjones committed Mar 13, 2012
2 parents fac91e5 + 7ac2b67 commit 51cd3cd
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 98 deletions.
20 changes: 18 additions & 2 deletions ckan/lib/plugins.py
Expand Up @@ -173,7 +173,13 @@ def package_form(self):
return 'package/new_package_form.html'

def form_to_db_schema(self):
return logic.schema.package_form_schema()
schema = logic.schema.package_form_schema()
schema['groups'] = {
'name': [not_empty, val.group_id_or_name_exists, unicode],
'id': [ignore_missing, unicode],
}
return schema


def form_to_db_schema_options(self, options):
''' This allows us to select different schemas for different
Expand Down Expand Up @@ -212,13 +218,23 @@ def check_data_dict(self, data_dict, schema=None):
raise dictization_functions.DataError(data_dict)

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

authz_fn = logic.get_action('group_list_authz')
c.groups_authz = authz_fn(context, data_dict)
data_dict.update({'available_only':True})
c.groups_available = authz_fn(context, data_dict)


c.publisher_enabled = 'publisher_form' in config['ckan.plugins']
if c.publisher_enabled:
c.groups_available = c.userobj.get_groups('publisher') if c.userobj else []
else:
c.groups_available = authz_fn(context, data_dict)

c.licences = [('', '')] + base.model.Package.get_license_options()
c.is_sysadmin = authz.Authorizer().is_sysadmin(c.user)


## This is messy as auths take domain object not data_dict
context_pkg = context.get('package', None)
pkg = context_pkg or c.pkg
Expand Down
95 changes: 0 additions & 95 deletions ckanext/publisher_form/forms.py
Expand Up @@ -132,98 +132,3 @@ def setup_template_variables(self, context, data_dict):
if grps:
c.parent = grps[0]
c.users = group.members_of_type(model.User)

class PublisherDatasetForm(SingletonPlugin):
"""
This plugin implements a new publisher form for cases where we
want to enforce group (type=publisher) membership on a dataset.
"""
implements(IDatasetForm, inherit=True)
implements(IConfigurer, inherit=True)

def update_config(self, config):
"""
This IConfigurer implementation causes CKAN to look in the
```templates``` directory when looking for the package_form()
"""
here = os.path.dirname(__file__)
rootdir = os.path.dirname(os.path.dirname(here))
template_dir = os.path.join(rootdir, 'ckanext',
'publisher_form', 'templates')
config['extra_template_paths'] = ','.join([template_dir,
config.get('extra_template_paths', '')])

def package_form(self):
"""
Returns a string representing the location of the template to be
rendered. e.g. "package/new_package_form.html".
"""
return 'dataset_form.html'

def is_fallback(self):
"""
Returns true iff this provides the fallback behaviour, when no other
plugin instance matches a package's type.
As this is not the fallback controller we should return False. If
we were wanting to act as the fallback, we'd return True
"""
return True

def package_types(self):
"""
Returns an iterable of package type strings.
If a request involving a package of one of those types is made, then
this plugin instance will be delegated to.
There must only be one plugin registered to each package type. Any
attempts to register more than one plugin instance to a given package
type will raise an exception at startup.
"""
return ["dataset"]

def setup_template_variables(self, context, data_dict=None):
"""
Adds variables to c just prior to the template being rendered that can
then be used within the form
"""
c.licences = [('', '')] + model.Package.get_license_options()
c.is_sysadmin = Authorizer().is_sysadmin(c.user)
c.resource_columns = model.Resource.get_columns()
c.groups_available = c.userobj.get_groups('publisher') if c.userobj else []

## This is messy as auths take domain object not data_dict
pkg = context.get('package') or c.pkg
if pkg:
c.auth_for_change_state = Authorizer().am_authorized(
c, model.Action.CHANGE_STATE, pkg)
gps = pkg.get_groups('publisher')
c.parent = gps[0] if gps else None

def form_to_db_schema(self):
"""
Returns the schema for mapping package data from a form to a format
suitable for the database.
"""
schema = package_form_schema()
schema['groups'] = {
'name': [not_empty, val.group_id_or_name_exists, unicode],
'id': [ignore_missing, unicode],
}
return schema

def db_to_form_schema(data):
"""
Returns the schema for mapping package data from the database into a
format suitable for the form (optional)
"""
return {}

def check_data_dict(self, data_dict):
"""
Check if the return data is correct and raises a DataError if not.
"""
pass

1 change: 0 additions & 1 deletion setup.py
Expand Up @@ -91,7 +91,6 @@
synchronous_search = ckan.lib.search:SynchronousSearchPlugin
stats=ckanext.stats.plugin:StatsPlugin
publisher_form=ckanext.publisher_form.forms:PublisherForm
publisher_dataset_form=ckanext.publisher_form.forms:PublisherDatasetForm
test_tag_vocab_plugin=ckanext.test_tag_vocab_plugin:MockVocabTagsPlugin
[ckan.system_plugins]
Expand Down

0 comments on commit 51cd3cd

Please sign in to comment.