Skip to content

Commit

Permalink
[#2570] Add ckanext/example_igroupform
Browse files Browse the repository at this point in the history
  • Loading branch information
Sean Hammond committed Jul 28, 2012
1 parent eac57b2 commit 243478b
Show file tree
Hide file tree
Showing 5 changed files with 276 additions and 0 deletions.
Empty file.
75 changes: 75 additions & 0 deletions ckanext/example_igroupform/plugin.py
@@ -0,0 +1,75 @@
import ckan.plugins as plugins
import ckan.plugins.toolkit as toolkit
import ckan.lib.plugins as lib_plugins
import ckan.lib.navl.validators as validators
import ckan.logic.schema
import ckan.logic.converters as converters

class ExampleIGroupFormPlugin(plugins.SingletonPlugin,
lib_plugins.DefaultGroupForm):
'''An example CKAN plugin that adds some custom metadata fields to groups.
'''
plugins.implements(plugins.IConfigurer, inherit=True)
plugins.implements(plugins.IGroupForm, inherit=True)

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.
toolkit.add_template_directory(config, 'templates')

def is_fallback(self):
# Register this plugin as the default IGroupForm plugin that is used
# when no IGroupForm matches the type of the group being edited.
return True

def group_types(self):
# This plugin doesn't handle any special group types (it just acts
# as the default plugin for group's with type None).
return []

def form_to_db_schema(self):
schema = ckan.logic.schema.group_form_schema()

# Add our custom country_code metadata field to the schema.
schema.update({
'country_code': [validators.ignore_missing,
converters.convert_to_extras]
})

# Add our custom website_url metadata field to the schema.
schema.update({'website_url': [validators.ignore_missing,
converters.convert_to_extras]})

return schema

def db_to_form_schema(self):
schema = ckan.logic.schema.group_form_schema()

# Add our custom country_code metadata field to the schema.
schema.update({
'country_code': [
converters.convert_from_extras, validators.ignore_missing]
})

# Add our custom website_url metadata field to the schema.
schema.update({'website_url': [converters.convert_from_extras,
validators.ignore_missing]})

return schema

def check_data_dict(self, data_dict):
# If the user submits a website URL that doesn't start with http://,
# prepend it for them.
website_url = data_dict.get('website_url', None)
if website_url:
if not website_url.startswith('http://'):
website_url = 'http://' + website_url
data_dict['website_url'] = website_url

def setup_template_variables(self, context, data_dict=None):
lib_plugins.DefaultGroupForm.setup_template_variables(self, context,
data_dict)

# Add this list of available country codes to the template context.
toolkit.c.country_codes = ('de', 'en', 'fr', 'nl')
118 changes: 118 additions & 0 deletions ckanext/example_igroupform/templates/group/new_group_form.html
@@ -0,0 +1,118 @@
<form
class="form-horizontal ${'has-errors' if errors else ''}"
id="group-edit"
action=""
method="post"
xmlns:i18n="http://genshi.edgewall.org/i18n"
xmlns:py="http://genshi.edgewall.org/"
xmlns:xi="http://www.w3.org/2001/XInclude">

<xi:include href="../_util.html" />

<div class="error-explanation" py:if="error_summary">
<h2>Errors in form</h2>
<p>The form contains invalid entries:</p>
<ul>
<li py:for="key, error in error_summary.items()">${"%s: %s" % (key if not key=='Name' else 'URL', error)}</li>
</ul>
</div>

<fieldset id="basic-information">
<div class="control-group">
<label for="name" class="control-label">Title</label>
<div class="controls">
<input class="js-title" id="title" name="title" type="text" value="${data.get('title', '')}"/>
</div>
</div>
<div class="control-group">
<label for="title" class="control-label">Url</label>
<div class="controls">
<div class="input-prepend">
<span class="add-on">${h.url(controller='group', action='index')+'/'}</span>
<input maxlength="100" name="name" type="text" class="js-url-input" value="${data.get('name', '')}" />
</div>
<p class="js-url-is-valid">&nbsp;</p>
<p class="url-is-long">Warning: URL is very long. Consider changing it to something shorter.</p>
<p>2+ characters, lowercase, using only 'a-z0-9' and '-_'</p>
<p class="field_error" py:if="errors.get('name', '')">${errors.get('name', '')}</p>
</div>
</div>
<div class="control-group">
<label for="" class="control-label">Description</label>
<div class="controls">
${markdown_editor('description', data.get('description'), 'notes', _('Start with a summary sentence ...'))}
</div>
</div>
<div class="control-group">
<label for="name" class="control-label">Image URL:</label>
<div class="controls">
<input id="image_url" name="image_url" type="text" value="${data.get('image_url', '')}"/>
<p>The URL for the image that is associated with this group.</p>
</div>
</div>

<!-- Our custom Country Code metadata field. -->
<div class="control-group">
<label class="control-label">Country:</label>
<div class="controls">
<select id="country_code" name="country_code">
<py:for each="country_code in c.country_codes">
<option value="${country_code}" py:attrs="{'selected': 'selected' if country_code == data.get('country_code', '') else None}" >${country_code}</option>
</py:for>
</select>
</div>
</div>

<!-- Our custom Website URL metadata field. -->
<div class="control-group">
<label class="control-label">Website URL:</label>
<div class="controls">
<input id="website_url" name="website_url" type="text"
value="${data.get('website_url', '')}"/>
<p>The URL of this group's website.</p>
</div>
</div>

<div class="state-field control-group" py:if="c.is_sysadmin or c.auth_for_change_state">
<label for="" class="control-label">State</label>
<div class="controls">
<select id="state" name="state" >
<option py:attrs="{'selected': 'selected' if data.get('state') == 'active' else None}" value="active">active</option>
<option py:attrs="{'selected': 'selected' if data.get('state') == 'deleted' else None}" value="deleted">deleted</option>
</select>
</div>
</div>
</fieldset>

<fieldset id="datasets">
<h3>Datasets</h3>
<div py:if="data.get('packages')">
<py:for each="num, package in enumerate(data.get('packages'))">
<div class="control-group">
<div class="controls">
<div class="checkbox">
<input value="${package['name']}" checked="checked" id="datasets__${num}__name" name="packages__${num}__name" type="checkbox" />
<label for="packages__${num}__name">${package['name']}</label>
</div>
</div>
</div>
</py:for>
</div>
<p py:if="not data.get('packages')">There are no datasets currently in this group.</p>

<h3>Add datasets</h3>
<div class="control-group">
<label class="control-label" for="packages__${len(data.get('packages', []))}__name">Dataset</label>
<div class="controls">
<input class="autocomplete-dataset" id="datasets__${len(data.get('packages', []))}__name" name="packages__${len(data.get('packages', []))}__name" type="text" />
</div>
</div>
</fieldset>

<div class="form-actions">
<input id="save" class="btn btn-primary" name="save" type="submit" value="${_('Save Changes')}" />
<py:if test="c.group">
<input id="cancel" class="btn href-action" name="cancel" type="reset" value="${_('Cancel')}" action="${h.url_for(controller='group', action='read', id=c.group.name)}" />
</py:if>
</div>
</form>
82 changes: 82 additions & 0 deletions ckanext/example_igroupform/templates/group/read.html
@@ -0,0 +1,82 @@
<html xmlns:py="http://genshi.edgewall.org/"
xmlns:i18n="http://genshi.edgewall.org/i18n"
xmlns:xi="http://www.w3.org/2001/XInclude"
py:strip="">

<xi:include href="../facets.html" />

<py:def function="page_title">${c.group_dict.display_name}</py:def>
<py:def function="page_heading">${c.group_dict.display_name}</py:def>
<py:if test="c.group.image_url">
<py:def function="page_logo">${c.group.image_url}</py:def>
</py:if>

<py:match path="primarysidebar">

<li class="widget-container widget_text">
<ul class="property-list">
<py:if test="c.group_admins">
<li>
<h3>Administrators</h3>
<ul class="no-break">
<li py:for="admin in c.group_admins">${h.linked_user(admin)}</li>
</ul>
</li>
</py:if>

<!-- Display our custom country_code metadata field. -->
<py:if test="c.group_dict.get('country_code', '')">
<li>
<h3>Group Country Code</h3>
${c.group_dict.country_code}
</li>
</py:if>

<!-- Display our custom website_url metadata field. -->
<py:if test="c.group_dict.get('website_url', '')">
<li>
<h3>Group Website</h3>
<a href="${c.group_dict.website_url}">${c.group_dict.website_url}</a>
</li>
</py:if>
</ul>
</li>
${facet_div('tags', _('Tags'))}
${facet_div('res_format', _('Resource Formats'))}
</py:match>

<py:match path="content">
<h3 py:if="c.group['state'] != 'active'">State: ${c.group['state']}</h3>
<div class="notes" py:if="str(c.description_formatted).strip()">
<div id="notes-extract">
${c.description_formatted}
</div>
<div id="notes-remainder"></div>
<div id="notes-toggle" style="display: none;">
<button class="more btn"><img src="/images/chevron-down.png"/></button>
<button class="less btn" style="display: none;"><img src="/images/chevron-up.png"/></button>
</div>
</div>
<div class="group-dataset-list">
<h3>Datasets</h3>
<xi:include href="../package/search_form.html" />
${field_list()}

<p i18n:msg="query, number_of_results"><span py:if="c.q">You searched for "${c.q}". </span>${c.page.item_count} datasets found.</p>
${c.page.pager()}
${package_list_from_dict(c.page.items)}
${c.page.pager()}
</div>
</py:match>

<py:def function="optional_feed">
<link rel="alternate" type="application/atom+xml" title="${g.site_title} - Datasets in group '${c.group['title']}'"
href="${h.url(controller='feed', action='group', id=c.group['name'])}" />
<link rel="alternate" type="application/atom+xml" title="${g.site_title} - Recent Revision History"
href="${h.url_for(controller='revision', action='list', format='atom', days=1)}" />
</py:def>

<xi:include href="layout.html" />
</html>


1 change: 1 addition & 0 deletions setup.py
Expand Up @@ -108,6 +108,7 @@
organizations=ckanext.organizations.forms:OrganizationForm
organizations_dataset=ckanext.organizations.forms:OrganizationDatasetForm
test_tag_vocab_plugin=ckanext.test_tag_vocab_plugin:MockVocabTagsPlugin
example_igroupform=ckanext.example_igroupform.plugin:ExampleIGroupFormPlugin
[ckan.system_plugins]
domain_object_mods = ckan.model.modification:DomainObjectModificationExtension
Expand Down

0 comments on commit 243478b

Please sign in to comment.