1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 | from ckan import model from ckan.lib.cli import CkanCommand from ckan.logic import get_action, NotFound import forms import logging log = logging.getLogger() class ExampleCommand(CkanCommand): ''' CKAN Example Extension Usage:: paster example create-example-vocabs -c <path to config file> paster example clean -c <path to config file> - Remove all data created by ckanext-example The commands should be run from the ckanext-example directory. ''' summary = __doc__.split('\n')[0] usage = __doc__ def command(self): ''' Parse command line arguments and call appropriate method. ''' if not self.args or self.args[0] in ['--help', '-h', 'help']: print ExampleCommand.__doc__ return cmd = self.args[0] self._load_config() if cmd == 'create-example-vocabs': self.create_example_vocabs() else: log.error('Command "%s" not recognized' % (cmd,)) def create_example_vocabs(self): ''' Adds example vocabularies to the database if they don't already exist. ''' user = get_action('get_site_user')({'model': model, 'ignore_auth': True}, {}) context = {'model': model, 'session': model.Session, 'user': user['name']} try: data = {'id': forms.GENRE_VOCAB} get_action('vocabulary_show')(context, data) log.info("Example genre vocabulary already exists, skipping.") except NotFound: log.info("Creating vocab %s" % forms.GENRE_VOCAB) data = {'name': forms.GENRE_VOCAB} vocab = get_action('vocabulary_create')(context, data) log.info("Adding tag %s to vocab %s" % ('jazz', forms.GENRE_VOCAB)) data = {'name': 'jazz', 'vocabulary_id': vocab['id']} get_action('tag_create')(context, data) log.info("Adding tag %s to vocab %s" % ('soul', forms.GENRE_VOCAB)) data = {'name': 'soul', 'vocabulary_id': vocab['id']} get_action('tag_create')(context, data) try: data = {'id': forms.COMPOSER_VOCAB} get_action('vocabulary_show')(context, data) log.info("Example composer vocabulary already exists, skipping.") except NotFound: log.info("Creating vocab %s" % forms.COMPOSER_VOCAB) data = {'name': forms.COMPOSER_VOCAB} vocab = get_action('vocabulary_create')(context, data) log.info("Adding tag %s to vocab %s" % ('Bob Mintzer', forms.COMPOSER_VOCAB)) data = {'name': 'Bob Mintzer', 'vocabulary_id': vocab['id']} get_action('tag_create')(context, data) log.info("Adding tag %s to vocab %s" % ('Steve Lewis', forms.COMPOSER_VOCAB)) data = {'name': 'Steve Lewis', 'vocabulary_id': vocab['id']} get_action('tag_create')(context, data) |