2014-07-11 13:55:23 +02:00
|
|
|
import constants
|
2014-07-10 12:26:31 +02:00
|
|
|
import db
|
|
|
|
|
2014-07-10 13:24:41 +02:00
|
|
|
from ckan.plugins import toolkit
|
2014-07-10 12:26:31 +02:00
|
|
|
from ckan.common import _
|
|
|
|
from itertools import count
|
|
|
|
|
|
|
|
|
|
|
|
def private_datasets_metadata_checker(key, data, errors, context):
|
|
|
|
|
2014-07-10 13:24:41 +02:00
|
|
|
dataset_id = data.get(('id',))
|
2014-07-10 12:26:31 +02:00
|
|
|
private_val = data.get(('private',))
|
2014-07-10 13:24:41 +02:00
|
|
|
|
2014-07-17 13:25:03 +02:00
|
|
|
# Avoid missing value
|
2014-07-16 15:59:45 +02:00
|
|
|
if not isinstance(private_val, basestring) and not isinstance(private_val, bool):
|
|
|
|
private_val = None
|
|
|
|
|
2014-07-10 13:24:41 +02:00
|
|
|
# If the private field is not included in the data dict, we must check the current value
|
2014-07-14 17:05:46 +02:00
|
|
|
if private_val is None and dataset_id:
|
2014-07-10 13:24:41 +02:00
|
|
|
dataset_dict = toolkit.get_action('package_show')({'ignore_auth': True}, {'id': dataset_id})
|
|
|
|
private_val = dataset_dict.get('private')
|
|
|
|
|
2014-07-10 12:26:31 +02:00
|
|
|
private = private_val is True if isinstance(private_val, bool) else private_val == "True"
|
|
|
|
metadata_value = data[key]
|
|
|
|
|
|
|
|
# If allowed users are included and the dataset is not private outside and organization, an error will be raised.
|
2014-07-16 15:59:45 +02:00
|
|
|
if metadata_value and not private:
|
2014-07-10 13:24:41 +02:00
|
|
|
errors[key].append(_('This field is only valid when you create a private dataset'))
|
2014-07-10 12:26:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def allowed_users_convert(key, data, errors, context):
|
2014-07-16 13:29:53 +02:00
|
|
|
|
|
|
|
# By default, all the fileds are in the data dictionary even if they contains nothing. In this case,
|
|
|
|
# the value is 'ckan.lib.navl.dictization_functions.Missing' and for this reason the type is checked
|
|
|
|
|
|
|
|
# Get the allowed user list
|
|
|
|
if (constants.ALLOWED_USERS,) in data and isinstance(data[(constants.ALLOWED_USERS,)], list):
|
|
|
|
allowed_users = data[(constants.ALLOWED_USERS,)]
|
|
|
|
elif (constants.ALLOWED_USERS_STR,) in data and isinstance(data[(constants.ALLOWED_USERS_STR,)], basestring):
|
|
|
|
allowed_users_str = data[(constants.ALLOWED_USERS_STR,)].strip()
|
|
|
|
allowed_users = [allowed_user for allowed_user in allowed_users_str.split(',') if allowed_user.strip() != '']
|
2014-07-10 12:26:31 +02:00
|
|
|
else:
|
2014-07-16 13:29:53 +02:00
|
|
|
allowed_users = None
|
2014-07-10 12:26:31 +02:00
|
|
|
|
2014-07-16 13:29:53 +02:00
|
|
|
if allowed_users is not None:
|
|
|
|
current_index = max([int(k[1]) for k in data.keys() if len(k) == 2 and k[0] == key[0]] + [-1])
|
2014-07-10 12:26:31 +02:00
|
|
|
|
2014-07-16 13:29:53 +02:00
|
|
|
if len(allowed_users) == 0:
|
|
|
|
data[(constants.ALLOWED_USERS,)] = []
|
|
|
|
else:
|
|
|
|
for num, allowed_user in zip(count(current_index + 1), allowed_users):
|
|
|
|
allowed_user = allowed_user.strip()
|
|
|
|
toolkit.get_validator('name_validator')(allowed_user, context) # User name should be validated
|
|
|
|
data[(key[0], num)] = allowed_user
|
2014-07-10 12:26:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_allowed_users(key, data, errors, context):
|
|
|
|
pkg_id = data[('id',)]
|
|
|
|
|
2014-07-15 11:52:09 +02:00
|
|
|
db.init_db(context['model'])
|
2014-07-10 12:26:31 +02:00
|
|
|
|
|
|
|
users = db.AllowedUser.get(package_id=pkg_id)
|
|
|
|
counter = 0
|
|
|
|
|
|
|
|
for user in users:
|
|
|
|
data[(key[0], counter)] = user.user_name
|
|
|
|
counter += 1
|