Bug fixing and improvements
This commit is contained in:
parent
edc1928795
commit
4bdd121dd9
|
@ -0,0 +1,5 @@
|
|||
ALLOWED_USERS = 'allowed_users'
|
||||
ALLOWED_USERS_STR = 'allowed_users_str'
|
||||
SEARCHABLE = 'searchable'
|
||||
ADQUIRE_URL = 'adquire_url'
|
||||
CONTEXT_CALLBACK = 'updating_via_cb'
|
|
@ -2,6 +2,7 @@ import ckan.lib.base as base
|
|||
import ckan.lib.helpers as helpers
|
||||
import ckan.model as model
|
||||
import ckan.plugins as plugins
|
||||
import ckanext.privatedatasets.constants as constants
|
||||
import ckanext.privatedatasets.db as db
|
||||
import importlib
|
||||
import logging
|
||||
|
@ -61,15 +62,16 @@ class AdquiredDatasetsControllerAPI(base.BaseController):
|
|||
for dataset_id in user_info['datasets']:
|
||||
try:
|
||||
|
||||
dataset = plugins.toolkit.get_action('package_show')({'ignore_auth': True}, {'id': dataset_id})
|
||||
allowed_user = db.AllowedUser.get(package_id=dataset['id'], user_name=user_info['user'])
|
||||
dataset = plugins.toolkit.get_action('package_show')({'ignore_auth': True, constants.CONTEXT_CALLBACK: True}, {'id': dataset_id})
|
||||
|
||||
if not allowed_user:
|
||||
allowed_user = db.AllowedUser()
|
||||
allowed_user.package_id = dataset['id']
|
||||
allowed_user.user_name = user_info['user']
|
||||
allowed_user.save()
|
||||
model.Session.add(allowed_user)
|
||||
# Create the array
|
||||
if constants.ALLOWED_USERS not in dataset:
|
||||
dataset[constants.ALLOWED_USERS] = []
|
||||
|
||||
# Add the user only if he/she is not in the list
|
||||
if user_info['user'] not in dataset[constants.ALLOWED_USERS]:
|
||||
dataset[constants.ALLOWED_USERS].append(user_info['user'])
|
||||
plugins.toolkit.get_action('package_update')({'ignore_auth': True}, dataset)
|
||||
else:
|
||||
log.warn('The user %s is already allowed to access the %s dataset' % (user_info['user'], dataset_id))
|
||||
|
||||
|
@ -82,7 +84,7 @@ class AdquiredDatasetsControllerAPI(base.BaseController):
|
|||
# Some datasets does not allow to introduce the list of allowed users since this property is
|
||||
# only valid for private datasets outside an organization. In this case, a wanr will return
|
||||
# but the process will continue
|
||||
warns.append('Dataset %s: %s' % (dataset_id, e.error_dict['allowed_users'][0]))
|
||||
warns.append('Dataset %s: %s' % (dataset_id, e.error_dict[constants.ALLOWED_USERS][0]))
|
||||
|
||||
# Commit the changes
|
||||
model.Session.commit()
|
||||
|
|
|
@ -1,3 +1,4 @@
|
|||
import constants
|
||||
import db
|
||||
|
||||
from ckan.plugins import toolkit
|
||||
|
@ -29,10 +30,10 @@ def allowed_users_convert(key, data, errors, context):
|
|||
else:
|
||||
allowed_users = data[key]
|
||||
|
||||
current_index = max([int(k[1]) for k in data.keys() if len(k) == 2 and k[0] == 'allowed_users'] + [-1])
|
||||
current_index = max([int(k[1]) for k in data.keys() if len(k) == 2 and k[0] == constants.ALLOWED_USERS] + [-1])
|
||||
|
||||
for num, allowed_user in zip(count(current_index + 1), allowed_users):
|
||||
data[('allowed_users', num)] = allowed_user
|
||||
data[(constants.ALLOWED_USERS, num)] = allowed_user
|
||||
|
||||
|
||||
def get_allowed_users(key, data, errors, context):
|
||||
|
|
|
@ -12,8 +12,12 @@ def is_adquired(pkg_dict):
|
|||
|
||||
|
||||
def is_owner(pkg_dict):
|
||||
return tk.c.userobj.id == pkg_dict['creator_user_id']
|
||||
if tk.c.userobj:
|
||||
return tk.c.userobj.id == pkg_dict['creator_user_id']
|
||||
else:
|
||||
return False
|
||||
|
||||
|
||||
def get_allowed_users_str(users):
|
||||
return ','.join([user for user in users])
|
||||
if users:
|
||||
return ','.join([user for user in users])
|
||||
|
|
|
@ -1,6 +1,7 @@
|
|||
import ckan.plugins as p
|
||||
import ckan.plugins.toolkit as tk
|
||||
import auth
|
||||
import constants
|
||||
import converters_validators as conv_val
|
||||
import db
|
||||
import helpers as helpers
|
||||
|
@ -24,18 +25,18 @@ class PrivateDatasets(p.SingletonPlugin, tk.DefaultDatasetForm):
|
|||
# remove datasets_with_no_organization_cannot_be_private validator
|
||||
'private': [tk.get_validator('ignore_missing'),
|
||||
tk.get_validator('boolean_validator')],
|
||||
'allowed_users_str': [tk.get_validator('ignore_missing'),
|
||||
conv_val.allowed_users_convert,
|
||||
conv_val.private_datasets_metadata_checker],
|
||||
'allowed_users': [tk.get_validator('ignore_missing'),
|
||||
conv_val.private_datasets_metadata_checker],
|
||||
'adquire_url': [tk.get_validator('ignore_missing'),
|
||||
conv_val.private_datasets_metadata_checker,
|
||||
tk.get_converter('convert_to_extras')],
|
||||
'searchable': [tk.get_validator('ignore_missing'),
|
||||
conv_val.private_datasets_metadata_checker,
|
||||
tk.get_converter('convert_to_extras'),
|
||||
tk.get_validator('boolean_validator')]
|
||||
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')]
|
||||
}
|
||||
|
||||
def create_package_schema(self):
|
||||
|
@ -53,12 +54,12 @@ class PrivateDatasets(p.SingletonPlugin, tk.DefaultDatasetForm):
|
|||
def show_package_schema(self):
|
||||
schema = super(PrivateDatasets, self).show_package_schema()
|
||||
schema.update({
|
||||
'allowed_users': [conv_val.get_allowed_users,
|
||||
tk.get_validator('ignore_missing')],
|
||||
'adquire_url': [tk.get_converter('convert_from_extras'),
|
||||
tk.get_validator('ignore_missing')],
|
||||
'searchable': [tk.get_converter('convert_from_extras'),
|
||||
tk.get_validator('ignore_missing')]
|
||||
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')]
|
||||
})
|
||||
return schema
|
||||
|
||||
|
@ -114,7 +115,7 @@ class PrivateDatasets(p.SingletonPlugin, tk.DefaultDatasetForm):
|
|||
|
||||
def before_index(self, pkg_dict):
|
||||
|
||||
if 'extras_searchable' in pkg_dict:
|
||||
if 'extras_' + constants.SEARCHABLE in pkg_dict:
|
||||
if pkg_dict['extras_searchable'] == 'False':
|
||||
pkg_dict['capacity'] = 'private'
|
||||
else:
|
||||
|
@ -141,24 +142,20 @@ class PrivateDatasets(p.SingletonPlugin, tk.DefaultDatasetForm):
|
|||
return pkg_dict
|
||||
|
||||
def after_create(self, context, pkg_dict):
|
||||
return pkg_dict
|
||||
|
||||
def after_update(self, context, pkg_dict):
|
||||
|
||||
session = context['session']
|
||||
|
||||
if db.package_allowed_users_table is None:
|
||||
db.init_db(context['model'])
|
||||
|
||||
# Get the users and the package ID
|
||||
if 'allowed_users' in pkg_dict:
|
||||
if constants.ALLOWED_USERS in pkg_dict:
|
||||
|
||||
# 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
|
||||
if len(pkg_dict['allowed_users']) == 1 and pkg_dict['allowed_users'][0] == '':
|
||||
pkg_dict['allowed_users'] = []
|
||||
if len(pkg_dict[constants.ALLOWED_USERS]) == 1 and pkg_dict[constants.ALLOWED_USERS][0] == '':
|
||||
pkg_dict[constants.ALLOWED_USERS] = []
|
||||
|
||||
received_users = [allowed_user for allowed_user in pkg_dict['allowed_users']]
|
||||
received_users = [allowed_user for allowed_user in pkg_dict[constants.ALLOWED_USERS]]
|
||||
package_id = pkg_dict['id']
|
||||
|
||||
# Get current users
|
||||
|
@ -182,12 +179,17 @@ class PrivateDatasets(p.SingletonPlugin, tk.DefaultDatasetForm):
|
|||
|
||||
return pkg_dict
|
||||
|
||||
def after_update(self, context, pkg_dict):
|
||||
return self.after_create(context, pkg_dict)
|
||||
|
||||
def after_show(self, context, pkg_dict):
|
||||
|
||||
user_obj = context.get('auth_user_obj')
|
||||
updating_via_api = context.get(constants.CONTEXT_CALLBACK, False)
|
||||
|
||||
# Only the package creator can update it
|
||||
if not user_obj or (pkg_dict['creator_user_id'] != user_obj.id and not user_obj.sysadmin):
|
||||
attrs = ['allowed_users', 'searchable', 'adquire_url']
|
||||
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]
|
||||
for attr in attrs:
|
||||
if attr in pkg_dict:
|
||||
del pkg_dict[attr]
|
||||
|
|
Loading…
Reference in New Issue