From 12f4e25fbda336b8b37d372d6f6b01f034edd279 Mon Sep 17 00:00:00 2001 From: m431m Date: Mon, 4 Sep 2017 16:21:20 +0200 Subject: [PATCH 1/3] ckan 2.7 compatibility --- ckanext/restricted/action.py | 14 +++++++++----- ckanext/restricted/controller.py | 25 +++++++++++++++---------- ckanext/restricted/logic.py | 30 +++++++++++++++--------------- ckanext/restricted/plugin.py | 2 +- 4 files changed, 40 insertions(+), 31 deletions(-) diff --git a/ckanext/restricted/action.py b/ckanext/restricted/action.py index 6bb5fc6..0343a71 100644 --- a/ckanext/restricted/action.py +++ b/ckanext/restricted/action.py @@ -8,7 +8,12 @@ from ckanext.restricted import logic from ckanext.restricted import auth -from pylons import config +try: + # CKAN 2.7 and later + from ckan.common import config +except ImportError: + # CKAN 2.6 and earlier + from pylons import config from logging import getLogger log = getLogger(__name__) @@ -30,7 +35,7 @@ def body_from_user_dict(user_dict): email = config.get('email_to') if not email: raise MailerException('Missing "email-to" in config') - + subject = u'New Registration: ' + user_dict.get('name', 'new user') + ' (' + user_dict.get('email') + ')' extra_vars = { @@ -87,7 +92,7 @@ def restricted_resource_search(context, data_dict): resource_search_result = resource_search(context, data_dict) restricted_resource_search_result = {} - + for key,value in resource_search_result.items(): if key == 'results': restricted_resource_search_result[key] = _restricted_resource_list_url(context, value) @@ -101,7 +106,7 @@ def restricted_package_search(context, data_dict): package_search_result = package_search(context, data_dict) restricted_package_search_result = {} - + for key,value in package_search_result.items(): if key == 'results': restricted_package_search_result_list = [] @@ -122,4 +127,3 @@ def _restricted_resource_list_url(context, resource_list): restricted_resource['url'] = 'Not Authorized' restricted_resources_list += [restricted_resource] return restricted_resources_list - diff --git a/ckanext/restricted/controller.py b/ckanext/restricted/controller.py index 72dad13..64130e5 100644 --- a/ckanext/restricted/controller.py +++ b/ckanext/restricted/controller.py @@ -10,7 +10,14 @@ from ckan.lib.base import render_jinja2 from logging import getLogger -from pylons import config + +try: + # CKAN 2.7 and later + from ckan.common import config +except ImportError: + # CKAN 2.6 and earlier + from pylons import config + from email.header import Header import simplejson as json @@ -38,10 +45,10 @@ def _send_request_mail(self, data): success = False try: - resource_link = toolkit.url_for(controller='package', action='resource_read', + resource_link = toolkit.url_for(controller='package', action='resource_read', id=data.get('package_name'), resource_id=data.get('resource_id')) - - resource_edit_link = toolkit.url_for(controller='package', action='resource_edit', + + resource_edit_link = toolkit.url_for(controller='package', action='resource_edit', id=data.get('package_name') , resource_id=data.get('resource_id')) extra_vars = { @@ -68,11 +75,11 @@ def _send_request_mail(self, data): ## CC doesn't work and mailer cannot send to multiple addresses for email, name in email_dict.iteritems(): mailer.mail_recipient(name, email, subject, body, headers) - + ## Special copy for the user (no links) email = data.get('user_email') name = data.get('user_name','User') - + extra_vars['resource_link'] = '[...]' extra_vars['resource_edit_link'] = '[...]' body = render_jinja2('restricted/emails/restricted_access_request.txt', extra_vars) @@ -129,10 +136,10 @@ def restricted_request_access_form(self, package_id, resource_id, data=None, err '''Redirects to form ''' user_id = toolkit.c.user - + if not user_id: toolkit.abort(401, _('Access request form is available to logged in users only.')) - + context = { 'model': model, 'session': model.Session, @@ -217,5 +224,3 @@ def _get_contact_details(self, pkg_dict): contact_email = config.get('email_to', 'email_to_undefined') contact_name = "CKAN Admin" return {'contact_email':contact_email, 'contact_name':contact_name} - - diff --git a/ckanext/restricted/logic.py b/ckanext/restricted/logic.py index 8aa97c4..88de918 100644 --- a/ckanext/restricted/logic.py +++ b/ckanext/restricted/logic.py @@ -8,7 +8,12 @@ from ckan.lib.base import render_jinja2 import ckan.plugins.toolkit as toolkit -from pylons import config +try: + # CKAN 2.7 and later + from ckan.common import config +except ImportError: + # CKAN 2.6 and earlier + from pylons import config from logging import getLogger log = getLogger(__name__) @@ -16,7 +21,7 @@ def restricted_check_user_resource_access(user, resource_dict, package_dict): restricted_level = 'public' allowed_users = [] - + # check in resource_dict if resource_dict: extras = resource_dict.get('extras',{}) @@ -31,7 +36,7 @@ def restricted_check_user_resource_access(user, resource_dict, package_dict): if restricted: restricted_level = restricted.get('level', 'public') allowed_users = restricted.get('allowed_users', '').split(',') - + # Public resources (DEFAULT) if not restricted_level or restricted_level == 'public': return {'success': True } @@ -89,22 +94,22 @@ def restricted_mail_allowed_user(user_id, resource): resource_name = resource.get('name', resource['id']) # maybe check user[activity_streams_email_notifications]==True - + mail_body = restricted_allowed_user_mail_body(user, resource) mail_subject = 'Access granted to resource {0}'.format(resource_name) # Send mail to user mailer.mail_recipient(user_name, user_email, mail_subject, mail_body) - + # Sendo copy to admin mailer.mail_recipient('CKAN Admin', config.get('email_to'), 'Fwd: ' + mail_subject, mail_body) - + except: log.warning('restricted_mail_allowed_user: Failed to send mail to "{0}"'.format(user_id)) def restricted_allowed_user_mail_body(user, resource): - - resource_link = toolkit.url_for(controller='package', action='resource_read', + + resource_link = toolkit.url_for(controller='package', action='resource_read', id=resource.get('package_id'), resource_id=resource.get('id')) extra_vars = { 'site_title': config.get('ckan.site_title'), @@ -123,9 +128,9 @@ def _safe_json_loads(json_string, default={}): return json.loads(json_string) except: return default - + previous_restricted = _safe_json_loads(previous_value) - updated_restricted = _safe_json_loads(updated_resource.get('restricted', '')) + updated_restricted = _safe_json_loads(updated_resource.get('restricted', '')) # compare restricted users_allowed values updated_allowed_users = Set(updated_restricted.get('allowed_users','').split(',')) @@ -134,8 +139,3 @@ def _safe_json_loads(json_string, default={}): for user_id in updated_allowed_users: if user_id not in previous_allowed_users: restricted_mail_allowed_user(user_id, updated_resource) - - - - - diff --git a/ckanext/restricted/plugin.py b/ckanext/restricted/plugin.py index 8cb81a0..eb88336 100644 --- a/ckanext/restricted/plugin.py +++ b/ckanext/restricted/plugin.py @@ -20,7 +20,7 @@ class RestrictedPlugin(plugins.SingletonPlugin): plugins.implements(plugins.IAuthFunctions) plugins.implements(plugins.IRoutes, inherit=True) plugins.implements(plugins.IResourceController, inherit=True) - + # IConfigurer def update_config(self, config_): From dd05bdf7e2e477199ba7f4ecf0b2dba5bfcaf6dc Mon Sep 17 00:00:00 2001 From: m431m Date: Tue, 5 Sep 2017 11:15:11 +0200 Subject: [PATCH 2/3] Fixed UnicodeEncodeError --- ckanext/restricted/controller.py | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/ckanext/restricted/controller.py b/ckanext/restricted/controller.py index 64130e5..e1a1521 100644 --- a/ckanext/restricted/controller.py +++ b/ckanext/restricted/controller.py @@ -83,7 +83,7 @@ def _send_request_mail(self, data): extra_vars['resource_link'] = '[...]' extra_vars['resource_edit_link'] = '[...]' body = render_jinja2('restricted/emails/restricted_access_request.txt', extra_vars) - body_user = "Please find below a copy of the access request mail sent. \n\n >> {0}".format( body.replace("\n", "\n >> ")) + body_user = u"Please find below a copy of the access request mail sent. \n\n >> {0}".format( body.replace("\n", "\n >> ")) mailer.mail_recipient(name, email, 'Fwd: ' + subject, body_user, headers) success=True From d55a1b7a4e509d6bfdfc17a9b7eacfa4fca6afb4 Mon Sep 17 00:00:00 2001 From: m431m Date: Tue, 5 Sep 2017 16:23:33 +0200 Subject: [PATCH 3/3] Fixed some `import` errors --- ckanext/restricted/action.py | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/ckanext/restricted/action.py b/ckanext/restricted/action.py index 0343a71..bd742ea 100644 --- a/ckanext/restricted/action.py +++ b/ckanext/restricted/action.py @@ -1,9 +1,12 @@ +from ckan.lib.base import render_jinja2 +from ckan.lib.mailer import mail_recipient +from ckan.lib.mailer import MailerException import ckan.logic +from ckan.logic.action.create import user_create from ckan.logic import side_effect_free, check_access from ckan.logic.action.get import package_show, resource_show, resource_view_list, resource_search, package_search import ckan.logic.auth as logic_auth import ckan.authz as authz - from ckanext.restricted import helpers from ckanext.restricted import logic from ckanext.restricted import auth @@ -18,6 +21,8 @@ from logging import getLogger log = getLogger(__name__) +NotFound = ckan.logic.NotFound + _get_or_bust = ckan.logic.get_or_bust def restricted_user_create_and_notify(context, data_dict):