|
import logging |
|
|
|
import ckan.plugins as plugins |
|
import ckan.plugins.toolkit as tk |
|
|
|
|
|
# get_action will be deprecated soon, this is the replacement |
|
def get_user(user): |
|
user_name = unicode(user) |
|
if user_name: |
|
user_obj = model.User.get(id) |
|
context['user_obj'] = user_obj |
|
if user_obj is None: |
|
raise NotFound |
|
else: |
|
raise NotFound |
|
|
|
return model_dictize.user_dictize(user_obj,context) |
|
|
|
|
|
class ExampleIDatasetFormPlugin(plugins.SingletonPlugin, |
|
tk.DefaultDatasetForm): |
|
'''An example IDatasetForm CKAN plugin. |
|
|
|
Uses a tag vocabulary to add a custom metadata field to datasets. |
|
|
|
''' |
|
plugins.implements(plugins.IConfigurer, inherit=False) |
|
plugins.implements(plugins.IDatasetForm, inherit=False) |
|
plugins.implements(plugins.ITemplateHelpers, inherit=False) |
|
|
|
# These record how many times methods that this plugin's methods are |
|
# called, for testing purposes. |
|
num_times_new_template_called = 0 |
|
num_times_read_template_called = 0 |
|
num_times_edit_template_called = 0 |
|
num_times_search_template_called = 0 |
|
num_times_history_template_called = 0 |
|
num_times_package_form_called = 0 |
|
num_times_check_data_dict_called = 0 |
|
num_times_setup_template_variables_called = 0 |
|
|
|
|
|
def update_config(self, config): |
|
# Add this plugin's templates dir to CKAN's extra_template_paths, so |
|
# that CKAN will use this plugin's custom templates. |
|
# here = os.path.dirname(__file__) |
|
# rootdir = os.path.dirname(os.path.dirname(here)) |
|
|
|
tk.add_template_directory(config, 'templates') |
|
tk.add_public_directory(config, 'theme/public') |
|
tk.add_resource('theme/public', 'ckanext-datagovau') |
|
# config['licenses_group_url'] = 'http://%(ckan.site_url)/licenses.json' |
|
|
|
def get_helpers(self): |
|
return {'getuser': get_user} |
|
|
|
def is_fallback(self): |
|
# Return True to register this plugin as the default handler for |
|
# package types not handled by any other IDatasetForm plugin. |
|
return True |
|
|
|
def package_types(self): |
|
# This plugin doesn't handle any special package types, it just |
|
# registers itself as the default (above). |
|
return [] |
|
|
|
|
|
def create_package_schema(self): |
|
schema = super(ExampleIDatasetFormPlugin, self).create_package_schema() |
|
schema = self._modify_package_schema(schema) |
|
return schema |
|
|
|
def update_package_schema(self): |
|
schema = super(ExampleIDatasetFormPlugin, self).update_package_schema() |
|
schema = self._modify_package_schema(schema) |
|
return schema |
|
|
|
def show_package_schema(self): |
|
schema = super(ExampleIDatasetFormPlugin, self).show_package_schema() |
|
|
|
# Don't show vocab tags mixed in with normal 'free' tags |
|
# (e.g. on dataset pages, or on the search page) |
|
schema['tags']['__extras'].append(tk.get_converter('free_tags_only')) |
|
|
|
# Add our custom_text field to the dataset schema. |
|
# ignore_missing == optional |
|
# ignore_empty == mandatory but not for viewing |
|
# !!! always convert_from_extras first |
|
schema.update({ |
|
'agency_program': [tk.get_converter('convert_from_extras'), |
|
tk.get_validator('ignore_missing')], |
|
'contact_point': [tk.get_converter('convert_from_extras'), |
|
tk.get_validator('ignore_empty')], |
|
'spatial_coverage': [tk.get_converter('convert_from_extras'), |
|
tk.get_validator('ignore_empty')], |
|
'granularity': [tk.get_converter('convert_from_extras'), |
|
tk.get_validator('ignore_empty')], |
|
'jurisdiction': [tk.get_converter('convert_from_extras'), |
|
tk.get_validator('ignore_empty')], |
|
'temporal_coverage': [tk.get_converter('convert_from_extras'), |
|
tk.get_validator('ignore_empty')], |
|
'data_state': [tk.get_converter('convert_from_extras'), |
|
tk.get_validator('ignore_empty')], |
|
'update_freq': [tk.get_converter('convert_from_extras'), |
|
tk.get_validator('ignore_empty')] |
|
}) |
|
return schema |
|
|
|
def _modify_package_schema(self, schema): |
|
# Add our custom_test metadata field to the schema, this one will use |
|
# convert_to_extras instead of convert_to_tags. |
|
# ignore_missing == optional |
|
# not_empty == mandatory, enforced here while modifying |
|
|
|
schema.update({ |
|
'agency_program': [tk.get_validator('ignore_missing'), |
|
tk.get_converter('convert_to_extras')], |
|
'contact_point': [tk.get_converter('convert_to_extras'), |
|
tk.get_validator('not_empty')], |
|
'spatial_coverage': [tk.get_converter('convert_to_extras'), |
|
tk.get_validator('not_empty')], |
|
'granularity': [tk.get_converter('convert_to_extras'), |
|
tk.get_validator('not_empty')], |
|
'jurisdiction': [tk.get_converter('convert_to_extras'), |
|
tk.get_validator('not_empty')], |
|
'temporal_coverage': [tk.get_converter('convert_to_extras'), |
|
tk.get_validator('not_empty')], |
|
'data_state': [tk.get_converter('convert_to_extras'), |
|
tk.get_validator('not_empty')], |
|
'update_freq': [tk.get_converter('convert_to_extras'), |
|
tk.get_validator('not_empty')] |
|
}) |
|
return schema |
|
|
|
# These methods just record how many times they're called, for testing |
|
# purposes. |
|
# TODO: It might be better to test that custom templates returned by |
|
# these methods are actually used, not just that the methods get |
|
# called. |
|
|
|
def setup_template_variables(self, context, data_dict): |
|
return super(ExampleIDatasetFormPlugin, self).setup_template_variables( |
|
context, data_dict) |
|
|
|
def new_template(self): |
|
return super(ExampleIDatasetFormPlugin, self).new_template() |
|
|
|
def read_template(self): |
|
return super(ExampleIDatasetFormPlugin, self).read_template() |
|
|
|
def edit_template(self): |
|
return super(ExampleIDatasetFormPlugin, self).edit_template() |
|
|
|
def search_template(self): |
|
return super(ExampleIDatasetFormPlugin, self).search_template() |
|
|
|
def history_template(self): |
|
return super(ExampleIDatasetFormPlugin, self).history_template() |
|
|
|
def package_form(self): |
|
return super(ExampleIDatasetFormPlugin, self).package_form() |
|
|
|
|