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 | import logging import ckan.plugins as plugins import ckan.lib as lib import ckan.lib.dictization.model_dictize as model_dictize import ckan.plugins.toolkit as tk import ckan.model as model from pylons import config from sqlalchemy import orm import ckan.model # get user created datasets and those they have edited def get_user_datasets(user_dict): created_datasets_list = user_dict['datasets'] active_datasets_list = [x['data']['package'] for x in lib.helpers.get_action('user_activity_list',{'id':user_dict['id']}) if x['data'].get('package')] raw_list = created_datasets_list + active_datasets_list filtered_dict = {} for dataset in raw_list: if dataset['id'] not in filtered_dict.keys(): filtered_dict[dataset['id']] = dataset return filtered_dict.values() def get_related_dataset(related_id): result = model.Session.execute("select dataset_id from related_dataset where related_id =\'"+related_id+"\' limit 1;").first()[0] return lib.helpers.get_action('package_show',{'id':result}) def related_create(context, data_dict=None): return {'success': False, 'msg': 'No one is allowed to create related items'} class DataGovAuPlugin(plugins.SingletonPlugin, tk.DefaultDatasetForm): '''An example IDatasetForm CKAN plugin. Uses a tag vocabulary to add a custom metadata field to datasets. ''' plugins.implements(plugins.IConfigurer, inherit=False) plugins.implements(plugins.ITemplateHelpers, inherit=False) plugins.implements(plugins.IAuthFunctions) def get_auth_functions(self): return {'related_create': related_create} def update_config(self, config): # Add this plugin's templates dir to CKAN's extra_template_paths, so # that CKAN will use this plugin's custom templates. # here = os.path.dirname(__file__) # rootdir = os.path.dirname(os.path.dirname(here)) tk.add_template_directory(config, 'templates') tk.add_public_directory(config, 'theme/public') tk.add_resource('theme/public', 'ckanext-datagovau') # config['licenses_group_url'] = 'http://%(ckan.site_url)/licenses.json' def get_helpers(self): return {'get_user_datasets': get_user_datasets, 'get_related_dataset': get_related_dataset} |