Skip to content

Instantly share code, notes, and snippets.

@pkdash
Last active August 29, 2015 14:01
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save pkdash/4d95bdcb5450c86535f8 to your computer and use it in GitHub Desktop.
Save pkdash/4d95bdcb5450c86535f8 to your computer and use it in GitHub Desktop.
from logging import getLogger
import ckan.plugins as p
import formencode.validators as v
import ckan.new_authz as auth
import copy
from ckan.logic.action.create import user_create as core_user_create, core_package_create
from ckan.logic.action.update import package_update
from ckan.logic.action.get import user_show, package_show
import ckan.lib.helpers as h
import helpers as meta_helper
import os
log = getLogger(__name__)
class MetadataPlugin(p.SingletonPlugin, p.toolkit.DefaultDatasetForm):
'''This plugin adds fields for the metadata (known as the Common Core) defined at
https://github.com/project-open-data/project-open-data.github.io/blob/master/schema.md
'''
p.implements(p.ITemplateHelpers)
p.implements(p.IConfigurer)
p.implements(p.IDatasetForm)
p.implements(p.IActions)
p.implements(p.IPackageController, inherit=True)
p.toolkit.add_resource('public', 'metadata_resources')
#See ckan.plugins.interfaces.IDatasetForm
def is_fallback(self):
# Return False so that we use the CKAN's default for
# /dataset/new and /dataset/edit
return False
#See ckan.plugins.interfaces.IDatasetForm
def package_types(self):
# This plugin doesn't handle any special package types, it just
# registers itself as the default (above).
return ['dataset']
def package_form(self):
return super(MetadataPlugin, self).package_form()
#See ckan.plugins.interfaces.IDatasetForm
def update_config(self, config):
# Instruct CKAN to look in the ```templates``` directory for customized templates and snippets
p.toolkit.add_template_directory(config, 'templates')
# add the extension's public dir path so that
# ckan can find any resources used from this path
# get the current dir path (here) for this plugin
here = os.path.dirname(__file__)
rootdir = os.path.dirname(os.path.dirname(here))
our_public_dir = os.path.join(rootdir, 'ckanext', 'Metadata', 'public')
config['extra_public_paths'] = ','.join([our_public_dir, config.get('extra_public_paths', '')])
#See ckan.plugins.interfaces.IDatasetForm
def _modify_package_schema(self, schema):
#log.debug("_modify_package_schema called")
not_empty = p.toolkit.get_validator('not_empty')
tag_string_convert = p.toolkit.get_validator('tag_string_convert')
# update the ckan's tag_string element making it required - which would force the user to enter
# at least on keyword (tag item)
schema.update({'tag_string': [not_empty, tag_string_convert]})
schema['resources']['name'][0] = not_empty
return schema
#See ckan.plugins.interfaces.IDatasetForm
def create_package_schema(self):
log.debug('create_package_schema')
schema = super(MetadataPlugin, self).create_package_schema()
schema = self._modify_package_schema(schema)
return schema
#See ckan.plugins.interfaces.IDatasetForm
def update_package_schema(self):
#log.debug('update_package_schema')
schema = super(MetadataPlugin, self).update_package_schema()
schema = self._modify_package_schema(schema)
return schema
#See ckan.plugins.interfaces.IDatasetForm
def show_package_schema(self):
schema = super(MetadataPlugin, self).show_package_schema()
ignore_missing = p.toolkit.get_validator('ignore_missing')
# Don't show vocab tags mixed in with normal 'free' tags
# (e.g. on dataset pages, or on the search page)
schema['tags']['__extras'].append(p.toolkit.get_converter('free_tags_only'))
return schema
#See ckan.plugins.interfaces.IActions
def get_actions(self):
return {
'user_create': user_create_custom,
'package_create': package_create_custom
}
def user_create_custom(context, data_dict):
log.debug('my very own user_create() called')
user_obj = core_user_create(context, data_dict) #>>> this causes infinite loop
#user_obj = p.toolkit.get_action('user_create')(context, data_dict)
data_dict = {
'id': 'iutah',
'username': user_obj['name'],
'role': 'editor'
}
context['ignore_auth'] = True
# 'default' is CKAN' default sysadmin account username that can be used for adding a user to an organization
context['user'] = 'default'
p.toolkit.get_action('organization_member_create')(context, data_dict)
return user_obj
def package_create_custom(context, data_dict):
log.debug('my very own package_create() called')
#if organization is iutah
iutahorg = p.toolkit.get_action('organization_show')(context, {'id': 'iutah'})
if data_dict['owner_org'] == iutahorg['id']:
data_dict['private'] = True
p.toolkit.check_access('package_create',context, data_dict)
pkg = core_package_create(context, data_dict)
return pkg
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment