2014-06-23 17:25:37 +02:00
|
|
|
import ckan.plugins as p
|
|
|
|
import ckan.plugins.toolkit as tk
|
2014-07-10 12:26:31 +02:00
|
|
|
import auth
|
2014-07-14 11:05:56 +02:00
|
|
|
import actions
|
2014-07-11 13:55:23 +02:00
|
|
|
import constants
|
2014-07-10 12:26:31 +02:00
|
|
|
import converters_validators as conv_val
|
|
|
|
import db
|
|
|
|
import helpers as helpers
|
2014-07-04 14:18:10 +02:00
|
|
|
|
|
|
|
|
2014-06-23 17:25:37 +02:00
|
|
|
class PrivateDatasets(p.SingletonPlugin, tk.DefaultDatasetForm):
|
2014-06-24 17:21:06 +02:00
|
|
|
|
2014-06-23 17:25:37 +02:00
|
|
|
p.implements(p.IDatasetForm)
|
|
|
|
p.implements(p.IAuthFunctions)
|
|
|
|
p.implements(p.IConfigurer)
|
2014-06-24 17:21:06 +02:00
|
|
|
p.implements(p.IRoutes, inherit=True)
|
2014-07-14 11:05:56 +02:00
|
|
|
p.implements(p.IActions)
|
2014-07-04 11:49:36 +02:00
|
|
|
p.implements(p.IPackageController)
|
2014-07-04 14:18:10 +02:00
|
|
|
p.implements(p.ITemplateHelpers)
|
2014-06-23 17:25:37 +02:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
############################ DATASET FORM ############################
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def _modify_package_schema(self):
|
|
|
|
return {
|
2014-06-26 16:37:27 +02:00
|
|
|
# remove datasets_with_no_organization_cannot_be_private validator
|
|
|
|
'private': [tk.get_validator('ignore_missing'),
|
|
|
|
tk.get_validator('boolean_validator')],
|
2014-07-11 13:55:23 +02:00
|
|
|
constants.ALLOWED_USERS_STR: [tk.get_validator('ignore_missing'),
|
|
|
|
conv_val.allowed_users_convert,
|
|
|
|
conv_val.private_datasets_metadata_checker],
|
|
|
|
constants.ALLOWED_USERS: [tk.get_validator('ignore_missing'),
|
|
|
|
conv_val.private_datasets_metadata_checker],
|
|
|
|
constants.ADQUIRE_URL: [tk.get_validator('ignore_missing'),
|
|
|
|
conv_val.private_datasets_metadata_checker,
|
|
|
|
tk.get_converter('convert_to_extras')],
|
|
|
|
constants.SEARCHABLE: [tk.get_validator('ignore_missing'),
|
|
|
|
conv_val.private_datasets_metadata_checker,
|
|
|
|
tk.get_converter('convert_to_extras'),
|
|
|
|
tk.get_validator('boolean_validator')]
|
2014-06-23 17:25:37 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
def create_package_schema(self):
|
2014-06-24 11:04:14 +02:00
|
|
|
# grab the default schema in our plugin
|
2014-06-23 17:25:37 +02:00
|
|
|
schema = super(PrivateDatasets, self).create_package_schema()
|
|
|
|
schema.update(self._modify_package_schema())
|
|
|
|
return schema
|
|
|
|
|
|
|
|
def update_package_schema(self):
|
2014-06-24 11:04:14 +02:00
|
|
|
# grab the default schema in our plugin
|
2014-06-23 17:25:37 +02:00
|
|
|
schema = super(PrivateDatasets, self).update_package_schema()
|
|
|
|
schema.update(self._modify_package_schema())
|
|
|
|
return schema
|
|
|
|
|
|
|
|
def show_package_schema(self):
|
|
|
|
schema = super(PrivateDatasets, self).show_package_schema()
|
|
|
|
schema.update({
|
2014-07-11 13:55:23 +02:00
|
|
|
constants.ALLOWED_USERS: [conv_val.get_allowed_users,
|
|
|
|
tk.get_validator('ignore_missing')],
|
|
|
|
constants.ADQUIRE_URL: [tk.get_converter('convert_from_extras'),
|
|
|
|
tk.get_validator('ignore_missing')],
|
|
|
|
constants.SEARCHABLE: [tk.get_converter('convert_from_extras'),
|
|
|
|
tk.get_validator('ignore_missing')]
|
2014-06-23 17:25:37 +02:00
|
|
|
})
|
|
|
|
return schema
|
|
|
|
|
|
|
|
def is_fallback(self):
|
|
|
|
# Return True to register this plugin as the default handler for
|
|
|
|
# package types not handled by any other IDatasetForm plugin.
|
|
|
|
return True
|
|
|
|
|
|
|
|
def package_types(self):
|
|
|
|
# This plugin doesn't handle any special package types, it just
|
|
|
|
# registers itself as the default (above).
|
|
|
|
return []
|
|
|
|
|
|
|
|
######################################################################
|
|
|
|
########################### AUTH FUNCTIONS ###########################
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def get_auth_functions(self):
|
2014-07-10 12:26:31 +02:00
|
|
|
return {'package_show': auth.package_show,
|
|
|
|
'package_update': auth.package_update,
|
2014-07-14 11:50:59 +02:00
|
|
|
'resource_show': auth.resource_show,
|
|
|
|
constants.PACKAGE_ADQUIRED: auth.package_adquired}
|
2014-06-23 17:25:37 +02:00
|
|
|
|
|
|
|
######################################################################
|
|
|
|
############################ ICONFIGURER #############################
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
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.
|
|
|
|
tk.add_template_directory(config, 'templates')
|
|
|
|
|
|
|
|
# Register this plugin's fanstatic directory with CKAN.
|
|
|
|
tk.add_resource('fanstatic', 'privatedatasets')
|
2014-06-24 17:21:06 +02:00
|
|
|
|
|
|
|
######################################################################
|
2014-07-14 11:05:56 +02:00
|
|
|
############################## IROUTES ###############################
|
2014-06-24 17:21:06 +02:00
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def after_map(self, m):
|
|
|
|
# DataSet adquired notification
|
2014-07-07 17:38:28 +02:00
|
|
|
m.connect('user_adquired_datasets', '/dashboad/adquired', ckan_icon='shopping-cart',
|
2014-07-08 12:30:28 +02:00
|
|
|
controller='ckanext.privatedatasets.controllers.ui_controller:AdquiredDatasetsControllerUI',
|
2014-07-07 17:38:28 +02:00
|
|
|
action='user_adquired_datasets', conditions=dict(method=['GET']))
|
2014-06-24 17:21:06 +02:00
|
|
|
|
|
|
|
return m
|
2014-06-26 10:22:10 +02:00
|
|
|
|
2014-07-14 11:05:56 +02:00
|
|
|
######################################################################
|
|
|
|
############################## IACTIONS ##############################
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def get_actions(self):
|
2014-07-14 11:50:59 +02:00
|
|
|
return {constants.PACKAGE_ADQUIRED: actions.package_adquired}
|
2014-07-14 11:05:56 +02:00
|
|
|
|
2014-07-04 11:49:36 +02:00
|
|
|
######################################################################
|
|
|
|
######################### IPACKAGECONTROLLER #########################
|
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def before_index(self, pkg_dict):
|
2014-07-07 09:30:51 +02:00
|
|
|
|
2014-07-11 13:55:23 +02:00
|
|
|
if 'extras_' + constants.SEARCHABLE in pkg_dict:
|
2014-07-07 09:30:51 +02:00
|
|
|
if pkg_dict['extras_searchable'] == 'False':
|
|
|
|
pkg_dict['capacity'] = 'private'
|
|
|
|
else:
|
|
|
|
pkg_dict['capacity'] = 'public'
|
|
|
|
|
2014-07-04 11:49:36 +02:00
|
|
|
return pkg_dict
|
|
|
|
|
|
|
|
def before_view(self, pkg_dict):
|
|
|
|
return pkg_dict
|
|
|
|
|
|
|
|
def before_search(self, search_params):
|
|
|
|
return search_params
|
|
|
|
|
|
|
|
def create(self, pkg_dict):
|
|
|
|
return pkg_dict
|
|
|
|
|
|
|
|
def edit(self, pkg_dict):
|
|
|
|
return pkg_dict
|
|
|
|
|
|
|
|
def read(self, pkg_dict):
|
|
|
|
return pkg_dict
|
|
|
|
|
|
|
|
def delete(self, pkg_dict):
|
|
|
|
return pkg_dict
|
|
|
|
|
|
|
|
def after_create(self, context, pkg_dict):
|
2014-07-10 12:26:31 +02:00
|
|
|
session = context['session']
|
|
|
|
|
|
|
|
if db.package_allowed_users_table is None:
|
|
|
|
db.init_db(context['model'])
|
|
|
|
|
|
|
|
# Get the users and the package ID
|
2014-07-11 13:55:23 +02:00
|
|
|
if constants.ALLOWED_USERS in pkg_dict:
|
2014-07-10 15:48:53 +02:00
|
|
|
|
|
|
|
# When the user removes all the users using the UI, we recieve an array with one
|
|
|
|
# element that is an empty string, so set the value properly
|
2014-07-11 13:55:23 +02:00
|
|
|
if len(pkg_dict[constants.ALLOWED_USERS]) == 1 and pkg_dict[constants.ALLOWED_USERS][0] == '':
|
|
|
|
pkg_dict[constants.ALLOWED_USERS] = []
|
2014-07-10 15:48:53 +02:00
|
|
|
|
2014-07-11 13:55:23 +02:00
|
|
|
received_users = [allowed_user for allowed_user in pkg_dict[constants.ALLOWED_USERS]]
|
2014-07-10 13:21:28 +02:00
|
|
|
package_id = pkg_dict['id']
|
|
|
|
|
|
|
|
# Get current users
|
|
|
|
users = db.AllowedUser.get(package_id=package_id)
|
|
|
|
|
|
|
|
# Delete users and save the list of current users
|
|
|
|
current_users = []
|
|
|
|
for user in users:
|
|
|
|
current_users.append(user.user_name)
|
|
|
|
if user.user_name not in received_users:
|
|
|
|
session.delete(user)
|
|
|
|
|
|
|
|
# Add non existing users
|
|
|
|
for user_name in received_users:
|
|
|
|
if user_name not in current_users:
|
|
|
|
out = db.AllowedUser()
|
|
|
|
out.package_id = package_id
|
|
|
|
out.user_name = user_name
|
|
|
|
out.save()
|
|
|
|
session.add(out)
|
2014-07-10 12:26:31 +02:00
|
|
|
|
2014-07-04 11:49:36 +02:00
|
|
|
return pkg_dict
|
|
|
|
|
2014-07-11 13:55:23 +02:00
|
|
|
def after_update(self, context, pkg_dict):
|
|
|
|
return self.after_create(context, pkg_dict)
|
|
|
|
|
2014-07-04 11:49:36 +02:00
|
|
|
def after_show(self, context, pkg_dict):
|
2014-07-11 13:55:23 +02:00
|
|
|
|
2014-07-10 12:26:31 +02:00
|
|
|
user_obj = context.get('auth_user_obj')
|
2014-07-11 13:55:23 +02:00
|
|
|
updating_via_api = context.get(constants.CONTEXT_CALLBACK, False)
|
2014-07-10 12:26:31 +02:00
|
|
|
|
|
|
|
# Only the package creator can update it
|
2014-07-11 13:55:23 +02:00
|
|
|
if not updating_via_api and (not user_obj or (pkg_dict['creator_user_id'] != user_obj.id and not user_obj.sysadmin)):
|
|
|
|
attrs = [constants.ALLOWED_USERS, constants.SEARCHABLE, constants.ADQUIRE_URL]
|
2014-07-10 12:26:31 +02:00
|
|
|
for attr in attrs:
|
|
|
|
if attr in pkg_dict:
|
|
|
|
del pkg_dict[attr]
|
|
|
|
|
2014-07-04 11:49:36 +02:00
|
|
|
return pkg_dict
|
|
|
|
|
|
|
|
def after_search(self, search_results, search_params):
|
|
|
|
return search_results
|
|
|
|
|
|
|
|
def after_delete(self, context, pkg_dict):
|
|
|
|
return pkg_dict
|
2014-07-04 14:18:10 +02:00
|
|
|
|
|
|
|
######################################################################
|
2014-07-14 17:05:46 +02:00
|
|
|
######################### ITEMPLATESHELPER ###########################
|
2014-07-04 14:18:10 +02:00
|
|
|
######################################################################
|
|
|
|
|
|
|
|
def get_helpers(self):
|
2014-07-10 12:26:31 +02:00
|
|
|
return {'privatedatasets_adquired': helpers.is_adquired,
|
|
|
|
'get_allowed_users_str': helpers.get_allowed_users_str,
|
|
|
|
'is_owner': helpers.is_owner}
|