-
Notifications
You must be signed in to change notification settings - Fork 2k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
74 additions
and
68 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,70 @@ | ||
from pylons import request, tmpl_context as c | ||
from genshi.input import HTML | ||
from genshi.filters import Transformer | ||
from ckan.logic import get_action | ||
from ckan.logic.converters import convert_to_tags, convert_from_tags, free_tags_only | ||
from ckan.logic.schema import package_form_schema, default_package_schema | ||
from ckan.lib.navl.validators import ignore_missing, keep_extras | ||
from ckan import plugins | ||
|
||
TEST_VOCAB_NAME = 'test-vocab' | ||
|
||
class MockVocabTagsPlugin(plugins.SingletonPlugin): | ||
plugins.implements(plugins.IDatasetForm, inherit=True) | ||
plugins.implements(plugins.IGenshiStreamFilter) | ||
|
||
active = True | ||
|
||
def is_fallback(self): | ||
return False | ||
|
||
def package_types(self): | ||
return ["mock_vocab_tags_plugin"] | ||
|
||
def package_form(self): | ||
return 'package/new_package_form.html' | ||
|
||
def setup_template_variables(self, context, data_dict=None): | ||
c.vocab_tags = get_action('tag_list')(context, {'vocabulary_id': TEST_VOCAB_NAME}) | ||
|
||
def form_to_db_schema(self): | ||
schema = default_package_schema() | ||
schema.update({ | ||
'vocab_tags': [ignore_missing, convert_to_tags(TEST_VOCAB_NAME)], | ||
}) | ||
return schema | ||
|
||
def db_to_form_schema(self): | ||
schema = package_form_schema() | ||
schema.update({ | ||
'tags': { | ||
'__extras': [keep_extras, free_tags_only] | ||
}, | ||
'vocab_tags_selected': [convert_from_tags(TEST_VOCAB_NAME), ignore_missing], | ||
}) | ||
return schema | ||
|
||
def filter(self, stream): | ||
if self.active: | ||
routes = request.environ.get('pylons.routes_dict') | ||
if routes.get('controller') == 'package' \ | ||
and routes.get('action') == 'read': | ||
# add vocab tags to the bottom of the page | ||
tags = c.pkg_dict.get('tags', []) | ||
for tag in tags: | ||
if tag.get('vocabulary_id'): | ||
stream = stream | Transformer('body')\ | ||
.append(HTML('<p>%s</p>' % tag.get('name'))) | ||
if routes.get('controller') == 'package' \ | ||
and routes.get('action') == 'edit': | ||
# add vocabs tag select box to edit page | ||
html = '<select id="vocab_tags" name="vocab_tags" size="60" multiple="multiple">' | ||
selected_tags = c.pkg_dict.get('vocab_tags_selected', []) | ||
for tag in c.vocab_tags: | ||
if tag in selected_tags: | ||
html += '<option selected="selected" value="%s">%s</option>' % (tag, tag) | ||
else: | ||
html += '<option value="%s">%s</option>' % (tag, tag) | ||
html += '</select>' | ||
stream = stream | Transformer('fieldset[@id="groups"]').append(HTML(html)) | ||
return stream |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters