Skip to content

Commit

Permalink
Merge branch 'example_idatasetform_without_tests_second_try'
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Mar 19, 2013
2 parents 7f24242 + 307c89e commit 3efeda6
Show file tree
Hide file tree
Showing 17 changed files with 496 additions and 613 deletions.
11 changes: 10 additions & 1 deletion ckan/controllers/package.py
Expand Up @@ -75,7 +75,16 @@ def _db_to_form_schema(self, package_type=None):
def _check_data_dict(self, data_dict, package_type=None):
'''Check if the return data is correct, mostly for checking out if
spammers are submitting only part of the form'''
return lookup_package_plugin(package_type).check_data_dict(data_dict)

# check_data_dict() is deprecated. If the package_plugin has a
# check_data_dict() we'll call it, if it doesn't have the method we'll
# do nothing.
package_plugin = lookup_package_plugin(package_type)
check_data_dict = getattr(package_plugin, 'check_data_dict', None)
if check_data_dict:
return check_data_dict(data_dict)
else:
return data_dict

def _setup_template_variables(self, context, data_dict, package_type=None):
return lookup_package_plugin(package_type).\
Expand Down
143 changes: 58 additions & 85 deletions ckan/lib/plugins.py
Expand Up @@ -158,73 +158,43 @@ def register_group_plugins(map):


class DefaultDatasetForm(object):
"""
Provides a default implementation of the pluggable package
controller behaviour.
'''The default implementation of IDatasetForm.
This class has 2 purposes:
See ckan.plugins.interfaces.IDatasetForm.
- it provides a base class for IDatasetForm implementations to use
if only a subset of the 5 method hooks need to be customised.
This class has two purposes:
- it provides the fallback behaviour if no plugin is setup to
provide the fallback behaviour.
1. It provides a base class for IDatasetForm implementations to inherit
from.
2. It is used as the default fallback plugin, if no IDatasetForm plugin
registers itself as the fallback.
Note - this isn't a plugin implementation. This is deliberate, as we
don't want this being registered.
"""
def new_template(self):
"""
Returns a string representing the location of the template to be
rendered for the new page
"""
return 'package/new.html'
def edit_template(self):
"""
Returns a string representing the location of the template to be
rendered for the edit page
"""
return 'package/edit.html'
'''
def form_to_db_schema_options(self, options):
'''Return different form_to_db_schemas under different conditions.
def comments_template(self):
"""
Returns a string representing the location of the template to be
rendered for the comments page
"""
return 'package/comments.html'
For example:
def search_template(self):
"""
Returns a string representing the location of the template to be
rendered for the search page (if present)
"""
return 'package/search.html'

def read_template(self):
"""
Returns a string representing the location of the template to be
rendered for the read page
"""
return 'package/read.html'
- if a context is provided, and it contains a schema, return that
schema
- if a dataset is being created via the api then return
form_to_db_schema_api_create()
- if a dataset is being updated via the api then return
form_to_db_schema_api_update()
- if a dataset is being created via the web form then return
form_to_db_schema()
def history_template(self):
"""
Returns a string representing the location of the template to be
rendered for the history page
"""
return 'package/history.html'
The schemas are defined by the methods below.
def package_form(self):
return 'package/new_package_form.html'
Because of this method, if an IDatasetForm plugin inherits from this
class then its form_to_db_schema() method will only be called when a
dataset is being created or updated over the web interface, and not
when the api is being used.
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:
Expand All @@ -248,43 +218,25 @@ def form_to_db_schema_api_create(self):
def form_to_db_schema_api_update(self):
return logic.schema.default_update_package_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 logic.schema.db_to_form_package_schema()

def db_to_form_schema_options(self, options):
'''This allows the selectino of different schemas for different
purposes. It is optional and if not available, ``db_to_form_schema``
should be used.
If a context is provided, and it contains a schema, it will be
returned.
'''Return different db_to_form_schemas under different conditions.
For example:
- if a context is provided, and it contains a schema, return that
schema
- otherwise return db_to_form_schema()
The schemas are defined by the methods below.
'''
schema = options.get('context', {}).get('schema', None)
if schema:
return schema
return self.db_to_form_schema()

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'''

# Resources might not exist yet (eg. Add Dataset)
surplus_keys_schema = ['__extras', '__junk', 'state', 'groups',
'extras_validation', 'save', 'return_to',
'resources', 'type', 'owner_org', 'private',
'log_message', 'tag_string', 'tags',
'url', 'version', 'extras']

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)
def db_to_form_schema(self):
return logic.schema.db_to_form_package_schema()

def setup_template_variables(self, context, data_dict):
authz_fn = logic.get_action('group_list_authz')
Expand All @@ -311,6 +263,27 @@ def setup_template_variables(self, context, data_dict):
except logic.NotAuthorized:
c.auth_for_change_state = False

def new_template(self):
return 'package/new.html'

def read_template(self):
return 'package/read.html'

def edit_template(self):
return 'package/edit.html'

def comments_template(self):
return 'package/comments.html'

def search_template(self):
return 'package/search.html'

def history_template(self):
return 'package/history.html'

def package_form(self):
return 'package/new_package_form.html'


class DefaultGroupForm(object):
"""
Expand Down
17 changes: 11 additions & 6 deletions ckan/logic/action/create.py
Expand Up @@ -117,12 +117,17 @@ def package_create(context, data_dict):
_check_access('package_create', context, data_dict)

if 'api_version' not in context:
# old plugins do not support passing the schema so we need
# to ensure they still work
try:
package_plugin.check_data_dict(data_dict, schema)
except TypeError:
package_plugin.check_data_dict(data_dict)
# check_data_dict() is deprecated. If the package_plugin has a
# check_data_dict() we'll call it, if it doesn't have the method we'll
# do nothing.
check_data_dict = getattr(package_plugin, 'check_datadict', None)
if check_data_dict:
try:
check_data_dict(data_dict, schema)
except TypeError:
# Old plugins do not support passing the schema so we need
# to ensure they still work
package_plugin.check_data_dict(data_dict)

data, errors = _validate(data_dict, schema, context)
log.debug('package_create validate_errs=%r user=%s package=%s data=%r',
Expand Down
17 changes: 11 additions & 6 deletions ckan/logic/action/update.py
Expand Up @@ -243,12 +243,17 @@ def package_update(context, data_dict):
schema = package_plugin.form_to_db_schema()

if 'api_version' not in context:
# old plugins do not support passing the schema so we need
# to ensure they still work
try:
package_plugin.check_data_dict(data_dict, schema)
except TypeError:
package_plugin.check_data_dict(data_dict)
# check_data_dict() is deprecated. If the package_plugin has a
# check_data_dict() we'll call it, if it doesn't have the method we'll
# do nothing.
check_data_dict = getattr(package_plugin, 'check_data_dict', None)
if check_data_dict:
try:
package_plugin.check_data_dict(data_dict, schema)
except TypeError:
# Old plugins do not support passing the schema so we need
# to ensure they still work.
package_plugin.check_data_dict(data_dict)

data, errors = _validate(data_dict, schema, context)
log.debug('package_update validate_errs=%r user=%s package=%s data=%r',
Expand Down

0 comments on commit 3efeda6

Please sign in to comment.