Skip to content

Commit

Permalink
Merge pull request #6419 from OCHA-DAP/feature/HDX-9987-implement-new…
Browse files Browse the repository at this point in the history
…-hdx-connect-form

HDX-9987 implement new HDX Connect form
  • Loading branch information
ccataalin authored Aug 26, 2024
2 parents 9a6b6fa + 3417d94 commit e7cd3f2
Show file tree
Hide file tree
Showing 30 changed files with 795 additions and 591 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@
import ckan.logic.action as core
import ckan.model as model
import ckan.plugins as plugins
from collections import OrderedDict
from ckan.common import _, c, config
import ckan.plugins.toolkit as toolkit
import ckan.lib.base as base
Expand Down Expand Up @@ -756,13 +757,14 @@ def hdx_user_in_org_or_group(group_id, include_pending=False):
return length != 0


def hdx_organization_type_list(include_default_value=None):
result = []
def hdx_organization_type_dict(include_default_value=None):
result = OrderedDict()

if include_default_value:
result.append({'value': '-1', 'text': _('-- Please select --')})
result.extend([{'value': t[1], 'text': _(t[0])} for t in static_lists.ORGANIZATION_TYPE_LIST])
# return [{'value': '-1', 'text': _('-- Please select --')}] + \
# [{'value': t[1], 'text': _(t[0])} for t in static_lists.ORGANIZATION_TYPE_LIST]
result['-1'] = _('-- Please select --')

result.update(OrderedDict({t[1]: _(t[0]) for t in static_lists.ORGANIZATION_TYPE_LIST}))

return result


Expand Down
2 changes: 1 addition & 1 deletion ckanext-hdx_org_group/ckanext/hdx_org_group/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ def get_helpers(self):
from ckanext.hdx_org_group.helpers import organization_helper as hdx_org_h
from ckanext.hdx_org_group.helpers import country_helper as hdx_country_h
return {
'hdx_organization_type_list': hdx_org_h.hdx_organization_type_list,
'hdx_organization_type_dict': hdx_org_h.hdx_organization_type_dict,
'hdx_organization_type_get_value': hdx_org_h.hdx_organization_type_get_value,
'hdx_datagrid_org_get_display_text': hdx_country_h.hdx_datagrid_org_get_display_text
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ def find_organisation() -> str:



def _set_custom_rect_logo_url(org_dict):
def set_custom_rect_logo_url(org_dict):
if 'customization' in org_dict and org_dict.get('customization'):
customization = json.loads(org_dict.get('customization'))
if customization and customization.get('image_rect', None):
Expand All @@ -82,7 +82,7 @@ def confirm_organisation() -> str:
org_id = request.form.get('org_id')
if org_id is not None:
org_dict = get_action(u'organization_show')(context, {'id':org_id})
_set_custom_rect_logo_url(org_dict)
set_custom_rect_logo_url(org_dict)
else:
return redirect(url_for('hdx_org_join.find_organisation'))
except Exception as ex:
Expand Down
13 changes: 8 additions & 5 deletions ckanext-hdx_package/ckanext/hdx_package/actions/authorize.py
Original file line number Diff line number Diff line change
Expand Up @@ -166,10 +166,13 @@ def hdx_mark_resource_in_hapi(context: Context, data_dict: DataDict):
return _check_hdx_user_permission(context, Permissions.PERMISSION_MANAGE_IN_HAPI_FLAG)


def hdx_contact_contributor(context: Context, data_dict: DataDict):
logged_in = not new_authz.auth_is_anon_user(context)
def hdx_request_access(context: Context):
"""
Only a logged-in user can request data access.
"""

if logged_in:
user_obj = context.get('auth_user_obj') or context.get('user_obj')
if user_obj:
return {'success': True}
else:
return {'success': False, 'msg': _('You must be logged in to contact the contributor.')}

return {'success': False, 'msg': _('Not authorized to perform this request.')}
Original file line number Diff line number Diff line change
Expand Up @@ -45,3 +45,18 @@ def validate(self, data_dict: DataDict):
log.error(ex)

return validated_response

def send_mail(self):
data_dict = {
'topic': self.request.form.get('topic'),
'fullname': self.request.form.get('fullname'),
'email': self.request.form.get('email'),
'msg': self.request.form.get('msg'),
'pkg_owner_org': self.request.form.get('pkg_owner_org'),
'pkg_title': self.request.form.get('pkg_title'),
'pkg_id': self.request.form.get('pkg_id'),
'pkg_url': h.url_for('dataset_read', id=self.request.form.get('pkg_id'), qualified=True),
'hdx_email': config.get('hdx.faqrequest.email', '[email protected]'),
}

get_action('hdx_send_mail_contributor')(self.context, data_dict)
Original file line number Diff line number Diff line change
@@ -0,0 +1,165 @@
import json
import logging

import ckan.lib.navl.dictization_functions as dictization_functions
import ckan.logic as logic
import ckan.model as model
import ckan.plugins.toolkit as tk
import ckanext.hdx_users.helpers.mailer as hdx_mailer
from ckan.types import Context, DataDict, Request
from ckan.lib.navl.dictization_functions import validate
from ckanext.requestdata.logic.schema import request_create_schema
from ckanext.requestdata.view_helper import process_extras_fields

get_action = tk.get_action
check_access = tk.check_access
config = tk.config
h = tk.h
g = tk.g
NotAuthorized = tk.NotAuthorized
NotFound = tk.ObjectNotFound
unicode_safe = tk.get_validator('unicode_safe')
log = logging.getLogger(__name__)


class DatasetRequestAccessLogic(object):
def __init__(self, context: Context, request: Request):
self.request = request
self.context = context
self.form = request.form
self.schema = request_create_schema()

def read(self) -> DataDict:
data_dict = logic.clean_dict(dictization_functions.unflatten(logic.tuplize_dict(logic.parse_params(self.form))))
return data_dict

def validate(self, data_dict: DataDict):
try:
validated_response = validate(data_dict, self.schema, self.context)
except Exception as ex:
log.error(ex)

return validated_response

def send_request(self) -> tuple[bool, str]:
data = self.request.form.to_dict()
get_action('requestdata_request_create')(self.context, data)

pkg_dict = get_action('package_show')(self.context, {'id': data['package_id']})

maintainer_id = pkg_dict['maintainer']
if maintainer_id is None:
return False, 'Dataset maintainer email not found.'

user_obj = self.context['auth_user_obj']

# Get users objects from maintainers list
context_user_show = {
'model': model,
'session': model.Session,
'user': g.user,
'auth_user_obj': g.userobj,
'keep_email': True,
}

data_dict = {
'users': []
}
recipients = []
maintainer_dict = {}
try:
maintainer_dict = get_action('user_show')(context_user_show, {'id': maintainer_id})
data_dict['users'].append(maintainer_dict)
recipients.append({'display_name': maintainer_dict.get('fullname'), 'email': maintainer_dict.get('email')})
except NotFound:
pass

if len(recipients) == 0:
admins = _org_admins_for_dataset(self.context, pkg_dict['name'])

for admin in admins:
recipients.append({'display_name': admin.get('fullname'), 'email': admin.get('email')})

sender_name = data.get('sender_name', '')
sender_email = data.get('email_address', '')
user_email = user_obj.email
message = data['message_content']

try:
sender_org = get_action('organization_show')(self.context, {'id': data.get('sender_organization_id')})
except NotFound:
sender_org = None

organizations = get_action('organization_list_for_user')(self.context, {
'id': user_obj.id,
'permission': 'read'
})
extras = json.loads(process_extras_fields(data, organizations, sender_org))

_send_email_to_maintainer(sender_name, message, user_email, extras, recipients, maintainer_dict, pkg_dict)
_send_email_to_requester(sender_name, sender_email, message, user_email, pkg_dict)

# notify package creator that new data request was made
get_action('requestdata_notification_create')(self.context, data_dict)

data_dict = {
'package_id': data['package_id'],
'flag': 'request'
}
get_action('requestdata_increment_request_data_counters')(self.context, data_dict)

return True, 'Email message was successfully sent.'


def _org_admins_for_dataset(context: Context, dataset_name: str):
pkg_dict = get_action('package_show')(context, {'id': dataset_name})
owner_org = pkg_dict['owner_org']

org = get_action('organization_show')(context, {'id': owner_org})

admins = []
for user in org['users']:
if user['capacity'] == 'admin':
db_user = model.User.get(user['id'])
data = {
'email': db_user.email,
'fullname': db_user.fullname or db_user.name
}
admins.append(data)

return admins


def _send_email_to_requester(sender_name: str, sender_email: str, message: str, user_email: str,
pkg_dict: DataDict) -> None:
subject = u'Request for access to metadata-only dataset'
email_data = {
'user_fullname': sender_name,
'msg': message,
'org_name': pkg_dict.get('organization').get('title'),
'dataset_link': h.url_for('dataset_read', id=pkg_dict['name'], qualified=True),
'dataset_title': pkg_dict['title'],
}
senders_email = [{'display_name': sender_name, 'email': sender_email}]
hdx_mailer.mail_recipient(senders_email, subject, email_data, footer=user_email,
snippet='email/content/request_data_to_user.html')


def _send_email_to_maintainer(sender_name: str, message: str, user_email: str, extras, recipients,
maintainer_dict: DataDict, pkg_dict: DataDict):
subject = sender_name + u' has requested access to one of your datasets: ' + pkg_dict['title']
email_data = {
'user_fullname': sender_name,
'user_email': user_email,
'msg': message,
'extras': extras,
'org_name': pkg_dict.get('organization').get('title'),
'dataset_link': h.url_for('dataset_read', id=pkg_dict['name'], qualified=True),
'dataset_title': pkg_dict['title'],
'maintainer_fullname': maintainer_dict.get('display_name') or maintainer_dict.get(
'fullname') if maintainer_dict else 'HDX user',
'requestdata_org_url': h.url_for('requestdata_organization_requests.requested_data',
id=pkg_dict.get('owner_org'), qualified=True)
}
hdx_mailer.mail_recipient(recipients, subject, email_data, footer='[email protected]',
snippet='email/content/request_data_to_admins.html')
2 changes: 1 addition & 1 deletion ckanext-hdx_package/ckanext/hdx_package/plugin.py
Original file line number Diff line number Diff line change
Expand Up @@ -551,7 +551,7 @@ def get_auth_functions(self):
'hdx_dataseries_update': authorize.hdx_dataseries_update,
'hdx_p_coded_resource_update': authorize.hdx_p_coded_resource_update,
'hdx_mark_resource_in_hapi': authorize.hdx_mark_resource_in_hapi,
'hdx_contact_contributor': authorize.hdx_contact_contributor,
'hdx_request_access': authorize.hdx_request_access,
}

def make_middleware(self, app, config):
Expand Down
Loading

0 comments on commit e7cd3f2

Please sign in to comment.