Skip to content

Commit

Permalink
[#2750] Use ITemplateHelpers not setup_template_variables()
Browse files Browse the repository at this point in the history
In ExampleIDatasetForm use ITemplateHelpers not
setup_template_variables() to pass the list of available country codes
to the template. This greatly reduces the number of templates that need
to be overridden!
  • Loading branch information
Sean Hammond committed Mar 15, 2013
1 parent 0221455 commit 4d45568
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 51 deletions.
33 changes: 18 additions & 15 deletions ckanext/example_idatasetform/plugin.py
Expand Up @@ -4,6 +4,16 @@
import ckan.plugins.toolkit as tk


def country_codes():
'''Return the list of country codes from the country codes vocabulary.'''
try:
country_codes = tk.get_action('tag_list')(
data_dict={'vocabulary_id': 'country_codes'})
return country_codes
except tk.ObjectNotFound:
return None


class ExampleIDatasetFormPlugin(plugins.SingletonPlugin,
tk.DefaultDatasetForm):
'''An example IDatasetForm CKAN plugin.
Expand All @@ -13,6 +23,7 @@ class ExampleIDatasetFormPlugin(plugins.SingletonPlugin,
'''
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.
Expand Down Expand Up @@ -54,6 +65,9 @@ def update_config(self, config):
# that CKAN will use this plugin's custom templates.
tk.add_template_directory(config, 'templates')

def get_helpers(self):
return {'country_codes': country_codes}

def is_fallback(self):
# Return True to register this plugin as the default handler for
# package types not handled by any other IDatasetForm plugin.
Expand All @@ -65,6 +79,10 @@ def package_types(self):
return []

def form_to_db_schema(self):

# Create the country_codes vocab and tags, if they don't already exist.
self.create_country_codes()

schema = super(ExampleIDatasetFormPlugin, self).form_to_db_schema()

# Add our custom country_code metadata field to the schema.
Expand Down Expand Up @@ -104,21 +122,6 @@ def db_to_form_schema(self):

return schema

def setup_template_variables(self, context, data_dict=None):
super(ExampleIDatasetFormPlugin, self).setup_template_variables(
context, data_dict)

# Create the country_codes vocab and tags, if they don't already exist.
self.create_country_codes()

# Add the list of available country codes, from the country_codes
# vocab, to the template context.
try:
tk.c.country_codes = tk.get_action('tag_list')(
context, {'vocabulary_id': 'country_codes'})
except tk.ObjectNotFound:
tk.c.country_codes = None

# 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
Expand Down

This file was deleted.

This file was deleted.

Expand Up @@ -27,7 +27,7 @@
<label class="control-label" for="field-country_code">{{ _("Country Code") }}</label>
<div class="controls">
<select id="field-country_code" name="country_code" data-module="autocomplete">
{% for country_code in country_codes %}
{% for country_code in h.country_codes() %}
<option value="{{ country_code }}" {% if country_code in data.get('country_code', []) %}selected="selected"{% endif %}>{{ country_code }}</option>
{% endfor %}
</select>
Expand Down

This file was deleted.

0 comments on commit 4d45568

Please sign in to comment.