2014-09-02 17:52:55 +02:00
|
|
|
# -*- coding: utf-8 -*-
|
|
|
|
|
|
|
|
# Copyright (c) 2014 CoNWeT Lab., Universidad Politécnica de Madrid
|
|
|
|
|
|
|
|
# This file is part of CKAN Private Dataset Extension.
|
|
|
|
|
|
|
|
# CKAN Private Dataset Extension is free software: you can redistribute it and/or
|
|
|
|
# modify it under the terms of the GNU Affero General Public License as published by
|
|
|
|
# the Free Software Foundation, either version 3 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
|
|
|
|
# CKAN Private Dataset Extension is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU Affero General Public License for more details.
|
|
|
|
|
|
|
|
# You should have received a copy of the GNU Affero General Public License
|
|
|
|
# along with CKAN Private Dataset Extension. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
|
2014-07-10 12:26:31 +02:00
|
|
|
import ckan.model as model
|
|
|
|
import ckan.plugins.toolkit as tk
|
|
|
|
import db
|
|
|
|
|
2014-10-09 14:44:47 +02:00
|
|
|
from pylons import config
|
|
|
|
|
2014-12-12 13:00:57 +01:00
|
|
|
from ckan.common import request
|
|
|
|
|
|
|
|
import logging
|
|
|
|
log = logging.getLogger(__name__)
|
2014-07-10 12:26:31 +02:00
|
|
|
|
2014-08-28 15:19:34 +02:00
|
|
|
def is_dataset_acquired(pkg_dict):
|
2014-07-10 12:26:31 +02:00
|
|
|
|
2014-07-15 11:52:09 +02:00
|
|
|
db.init_db(model)
|
2014-07-10 12:26:31 +02:00
|
|
|
|
2014-07-14 17:05:46 +02:00
|
|
|
if tk.c.user:
|
|
|
|
return len(db.AllowedUser.get(package_id=pkg_dict['id'], user_name=tk.c.user)) > 0
|
|
|
|
else:
|
|
|
|
return False
|
2014-07-10 12:26:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def is_owner(pkg_dict):
|
2014-07-15 11:52:09 +02:00
|
|
|
if tk.c.userobj is not None:
|
2014-07-11 13:55:23 +02:00
|
|
|
return tk.c.userobj.id == pkg_dict['creator_user_id']
|
|
|
|
else:
|
|
|
|
return False
|
2014-07-10 12:26:31 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_allowed_users_str(users):
|
2014-07-11 13:55:23 +02:00
|
|
|
if users:
|
|
|
|
return ','.join([user for user in users])
|
2014-07-14 17:05:46 +02:00
|
|
|
else:
|
|
|
|
return ''
|
2014-07-24 15:38:44 +02:00
|
|
|
|
|
|
|
|
|
|
|
def can_read(pkg_dict):
|
2014-07-24 16:46:02 +02:00
|
|
|
try:
|
|
|
|
context = {'user': tk.c.user, 'userobj': tk.c.userobj, 'model': model}
|
|
|
|
return tk.check_access('package_show', context, pkg_dict)
|
|
|
|
except tk.NotAuthorized:
|
|
|
|
return False
|
2014-10-09 14:44:47 +02:00
|
|
|
|
|
|
|
|
|
|
|
def get_config_bool_value(config_name, default_value=False):
|
|
|
|
value = config.get(config_name, default_value)
|
|
|
|
value = value if type(value) == bool else value != 'False'
|
|
|
|
return value
|
|
|
|
|
|
|
|
|
|
|
|
def show_acquire_url_on_create():
|
|
|
|
return get_config_bool_value('ckan.privatedatasets.show_acquire_url_on_create')
|
2014-10-09 15:25:32 +02:00
|
|
|
|
|
|
|
|
|
|
|
def show_acquire_url_on_edit():
|
|
|
|
return get_config_bool_value('ckan.privatedatasets.show_acquire_url_on_edit')
|
2014-12-12 13:00:57 +01:00
|
|
|
|
|
|
|
|
|
|
|
def snippet(template_name, data=None):
|
|
|
|
''' This function is used to load html snippets into pages. keywords
|
|
|
|
can be used to pass parameters into the snippet rendering '''
|
|
|
|
return tk.render_snippet(template_name, data)
|
|
|
|
|
|
|
|
|
|
|
|
def getaccess_button(package):
|
|
|
|
'''Return a Get Access button for the given package id.
|
|
|
|
|
|
|
|
If the user is not logged in return an empty string instead.
|
|
|
|
|
|
|
|
:param package: the the package to request access when the get access
|
|
|
|
button is clicked
|
|
|
|
:type package: Package
|
|
|
|
|
|
|
|
:returns: a get access button as an HTML snippet
|
|
|
|
:rtype: string
|
|
|
|
|
|
|
|
'''
|
|
|
|
|
|
|
|
if 'acquire_url' in package and request.path.startswith('/dataset')\
|
|
|
|
and package['acquire_url'] != '':
|
|
|
|
url_dest = package['acquire_url']
|
|
|
|
data = {'url_dest': url_dest}
|
|
|
|
return snippet('snippets/fiware_getaccess_button.html', data)
|
|
|
|
else:
|
|
|
|
return ''
|