From: John Glover Date: Mon, 06 Feb 2012 10:24:27 +0000 Subject: [1720][forms] Update for change in location of vocabulary get function X-Git-Url: https://maxious.lambdacomplex.org/git/?p=ckanext-datagovau.git&a=commitdiff&h=1ca2982d0b35198fdabb197eea8fbb6eda1a704b --- [1720][forms] Update for change in location of vocabulary get function --- --- a/ckanext/example/forms.py +++ b/ckanext/example/forms.py @@ -4,12 +4,13 @@ import ckan.logic.action.update as update import ckan.logic.action.get as get from ckan.logic.converters import date_to_db, date_to_form, convert_to_extras,\ - convert_from_extras, convert_to_tags + convert_from_extras, convert_to_tags, convert_from_tags from ckan.logic import NotFound, NotAuthorized, ValidationError from ckan.logic import tuplize_dict, clean_dict, parse_params import ckan.logic.schema as default_schema from ckan.logic.schema import package_form_schema, group_form_schema import ckan.logic.validators as val +from ckan.model import vocabulary from ckan.lib.base import BaseController, render, c, model, abort, request from ckan.lib.base import redirect, _, config, h from ckan.lib.package_saver import PackageSaver @@ -17,7 +18,7 @@ from ckan.lib.navl.dictization_functions import Invalid from ckan.lib.navl.dictization_functions import validate, missing from ckan.lib.navl.dictization_functions import DataError, flatten_dict, unflatten -from ckan.plugins import IDatasetForm, IGroupForm, IConfigurer +from ckan.plugins import IDatasetForm, IGroupForm, IConfigurer, IConfigurable from ckan.plugins import implements, SingletonPlugin from ckan.lib.navl.validators import (ignore_missing, @@ -134,6 +135,7 @@ """ implements(IDatasetForm, inherit=True) implements(IConfigurer, inherit=True) + implements(IConfigurable) def update_config(self, config): """ @@ -146,6 +148,19 @@ 'example', 'theme', 'templates') config['extra_template_paths'] = ','.join([template_dir, config.get('extra_template_paths', '')]) + + def configure(self, config): + """ + Adds our new vocabulary to the database if it doesn't + already exist. + """ + self.vocab_name = u'example_vocab' + v = vocabulary.get(self.vocab_name) + if not v: + log.info("Adding vocab %s" % self.vocab_name) + vocab = model.Vocabulary(self.vocab_name) + model.Session.add(vocab) + model.Session.commit() def package_form(self): """ @@ -201,11 +216,11 @@ schema = package_form_schema() schema.update({ 'published_by': [not_empty, unicode, convert_to_extras], - 'vocab_tag_string': [ignore_missing, convert_to_tags('example_vocab')], + 'vocab_tag_string': [ignore_missing, convert_to_tags(self.vocab_name)], }) return schema - def db_to_form_schema(data): + def db_to_form_schema(self): """ Returns the schema for mapping package data from the database into a format suitable for the form (optional) @@ -215,6 +230,7 @@ 'tags': { '__extras': [keep_extras] }, + 'vocab_tag_string': [convert_from_tags(self.vocab_name), ignore_missing], 'published_by': [convert_from_extras, ignore_missing], }) return schema