Skip to content

Commit

Permalink
make plugin for tag_vocab_tests
Browse files Browse the repository at this point in the history
  • Loading branch information
tobes committed Feb 27, 2012
1 parent 54d656c commit 9265444
Show file tree
Hide file tree
Showing 4 changed files with 74 additions and 68 deletions.
69 changes: 2 additions & 67 deletions ckan/tests/functional/test_tag_vocab.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,74 +6,15 @@
from ckan import model
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
from ckan.logic.schema import package_form_schema, default_package_schema
from ckan.lib.navl.validators import ignore_missing, keep_extras
from ckan.lib.create_test_data import CreateTestData
import ckan.lib.helpers as h
from ckan import plugins
from ckan.tests import WsgiAppCase, url_for
from ckan.tests import WsgiAppCase

TEST_VOCAB_NAME = 'test-vocab'

class MockVocabTagsPlugin(plugins.SingletonPlugin):
plugins.implements(plugins.IDatasetForm, inherit=True)
plugins.implements(plugins.IGenshiStreamFilter)

active = False

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 = package_form_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


# paste.fixture.Field.Select does not handle multiple selects currently,
Expand Down Expand Up @@ -143,10 +84,6 @@ def setup_class(cls):
cls.tag1_name = 'vocab-tag-1'
cls.tag2_name = 'vocab-tag-2'

cls.plugin = MockVocabTagsPlugin()
plugins.load(cls.plugin)
cls.plugin.active = True

# use our custom select class for this test suite
cls.old_select = paste.fixture.Field.classes['select']
paste.fixture.Field.classes['select'] = Select
Expand All @@ -172,8 +109,6 @@ def setup_class(cls):

@classmethod
def teardown_class(cls):
plugins.unload(cls.plugin)
cls.plugin.active = False
paste.fixture.Field.classes['select'] = cls.old_select
model.repo.rebuild_db()

Expand Down
70 changes: 70 additions & 0 deletions ckanext/test_tag_vocab_plugin.py
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
1 change: 1 addition & 0 deletions setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@
stats=ckanext.stats.plugin:StatsPlugin
publisher_form=ckanext.publisher_form.forms:PublisherForm
publisher_dataset_form=ckanext.publisher_form.forms:PublisherDatasetForm
test_tag_vocab=ckanext.test_tag_vocab_plugin:MockVocabTagsPlugin
[ckan.system_plugins]
domain_object_mods = ckan.model.modification:DomainObjectModificationExtension
Expand Down
2 changes: 1 addition & 1 deletion test-core.ini
Original file line number Diff line number Diff line change
Expand Up @@ -45,7 +45,7 @@ search_backend = sql
# Change API key HTTP header to something non-standard.
apikey_header_name = X-Non-Standard-CKAN-API-Key

ckan.plugins = stats
ckan.plugins = stats test_tag_vocab

# use <strong> so we can check that html is *not* escaped
ckan.template_head_end = <link rel="stylesheet" href="TEST_TEMPLATE_HEAD_END.css" type="text/css">
Expand Down

0 comments on commit 9265444

Please sign in to comment.