Skip to content

Commit

Permalink
[QCDP24-26] revert back the facet to org id, and force show only the …
Browse files Browse the repository at this point in the history
…current users org
  • Loading branch information
awset committed Sep 19, 2024
1 parent 4f97d06 commit 9b79ad0
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 39 deletions.
36 changes: 21 additions & 15 deletions ckanext/datarequests/actions.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@
from ckan.lib import mailer
from ckan.lib.redis import connect_to_redis
from ckan.plugins import toolkit as tk
from ckan.plugins.toolkit import h, config
from ckan.plugins.toolkit import h, config, current_user

from . import common, constants, db, validator

Expand Down Expand Up @@ -527,10 +527,10 @@ def list_datarequests(context, data_dict):
tk.check_access(constants.LIST_DATAREQUESTS, context, data_dict)

# Get the organization
requesting_organisation = data_dict.get('requesting_organisation', None)
if requesting_organisation:
organization_id = data_dict.get('organization_id', None)
if organization_id:
# Get organization ID (organization name is received sometimes)
requesting_organisation = organization_show({'ignore_auth': True}, {'id': requesting_organisation}).get('id')
organization_id = organization_show({'ignore_auth': True}, {'id': organization_id}).get('id')

user_id = data_dict.get('user_id', None)
if user_id:
Expand All @@ -554,7 +554,7 @@ def list_datarequests(context, data_dict):
desc = True

# Call the function
db_datarequests = db.DataRequest.get_ordered_by_date(requesting_organisation=requesting_organisation,
db_datarequests = db.DataRequest.get_ordered_by_date(organization_id=organization_id,
user_id=user_id, status=status,
q=q, desc=desc, state=state)

Expand All @@ -575,24 +575,24 @@ def list_datarequests(context, data_dict):
'Assign to Internal Data Catalogue Support': 0
}
for data_req in db_datarequests:
requesting_organisation = data_req.requesting_organisation
organization_id = data_req.organization_id
status = data_req.status

if requesting_organisation:
no_processed_organization_facet[requesting_organisation] = no_processed_organization_facet.get(requesting_organisation, 0) + 1
if organization_id:
no_processed_organization_facet[organization_id] = no_processed_organization_facet.get(organization_id, 0) + 1

if status in no_processed_status_facet:
no_processed_status_facet[status] += 1

# Format facets
requesting_organization_facet = []
for requesting_organisation in no_processed_organization_facet:
organization_facet = []
for organization_id in no_processed_organization_facet:
try:
organization = organization_show({'ignore_auth': True}, {'id': requesting_organisation})
requesting_organization_facet.append({
organization = organization_show({'ignore_auth': True}, {'id': organization_id})
organization_facet.append({
'name': organization.get('name'),
'display_name': organization.get('display_name'),
'count': no_processed_organization_facet[requesting_organisation]
'count': no_processed_organization_facet[organization_id]
})
except Exception:
pass
Expand All @@ -613,8 +613,14 @@ def list_datarequests(context, data_dict):
}

# Facets can only be included if they contain something
if requesting_organization_facet:
result['facets']['requesting_organisation'] = {'items': requesting_organization_facet}
if organization_facet:
# If not sysadmin, only show organizations where the current user is a member/editor/org admin.
if not current_user.sysadmin:
current_user_orgs = h.organizations_available('read')
user_orgs = {org['name'] for org in current_user_orgs}
organization_facet = [org for org in organization_facet if org['name'] in user_orgs]

result['facets']['organization'] = {'items': organization_facet}

if status_facet:
result['facets']['status'] = {'items': status_facet}
Expand Down
4 changes: 2 additions & 2 deletions ckanext/datarequests/auth.py
Original file line number Diff line number Diff line change
Expand Up @@ -50,9 +50,9 @@ def show_datarequest(context, data_dict):
if data_dict.get('user_id', None) == current_user.id:
return {'success': True}

requesting_organisation = data_dict.get('requesting_organisation', None)
organization_id = data_dict.get('organization_id', None)
current_user_orgs = [org['id'] for org in h.organizations_available('read')] or []
if requesting_organisation not in current_user_orgs:
if organization_id not in current_user_orgs:
return {'success': False}

return {'success': True}
Expand Down
16 changes: 8 additions & 8 deletions ckanext/datarequests/controllers/controller_functions.py
Original file line number Diff line number Diff line change
Expand Up @@ -58,7 +58,7 @@ def _get_context():
'user': c.user, 'auth_user_obj': c.userobj}


def _show_index(user_id, requesting_organisation, include_organization_facet, url_func, file_to_render, extra_vars=None):
def _show_index(user_id, organization_id, include_organization_facet, url_func, file_to_render, extra_vars=None):
def pager_url(status=None, sort=None, q=None, page=None):
params = []

Expand Down Expand Up @@ -88,8 +88,8 @@ def pager_url(status=None, sort=None, q=None, page=None):
if q:
data_dict['q'] = q

if requesting_organisation:
data_dict['requesting_organisation'] = requesting_organisation
if organization_id:
data_dict['organization_id'] = organization_id

if user_id:
data_dict['user_id'] = user_id
Expand All @@ -105,7 +105,7 @@ def pager_url(status=None, sort=None, q=None, page=None):
c.filters = [(tk._('Newest'), 'desc'), (tk._('Oldest'), 'asc')]
c.sort = sort
c.q = q
c.requesting_organisation = requesting_organisation
c.organization = organization_id
c.status = status
c.datarequest_count = datarequests_list['count']
c.datarequests = datarequests_list['result']
Expand All @@ -123,14 +123,14 @@ def pager_url(status=None, sort=None, q=None, page=None):

# Organization facet cannot be shown when the user is viewing an org
if include_organization_facet is True:
c.facet_titles['requesting_organisation'] = tk._('Organizations')
c.facet_titles['organization'] = tk._('Organizations')

if not extra_vars:
extra_vars = {}
extra_vars['filters'] = c.filters
extra_vars['sort'] = c.sort
extra_vars['q'] = c.q
extra_vars['requesting_organisation'] = c.requesting_organisation
extra_vars['organization'] = c.organization
extra_vars['status'] = c.status
extra_vars['datarequest_count'] = c.datarequest_count
extra_vars['datarequests'] = c.datarequests
Expand All @@ -141,7 +141,7 @@ def pager_url(status=None, sort=None, q=None, page=None):
extra_vars['user'] = None
if 'user_dict' not in extra_vars:
extra_vars['user_dict'] = None
extra_vars['group_type'] = 'requesting_organisation'
extra_vars['group_type'] = 'organization'
return tk.render(file_to_render, extra_vars=extra_vars)
except ValueError as e:
# This exception should only occur if the page value is not valid
Expand All @@ -153,7 +153,7 @@ def pager_url(status=None, sort=None, q=None, page=None):


def index():
return _show_index(None, request_helpers.get_first_query_param('requesting_organisation', ''), True, search_url,
return _show_index(None, request_helpers.get_first_query_param('organization', ''), True, search_url,
'datarequests/index.html')


Expand Down
26 changes: 13 additions & 13 deletions ckanext/datarequests/db.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ def datarequest_exists(cls, title):
return query.filter(func.lower(cls.title) == func.lower(title)).first() is not None

@classmethod
def get_ordered_by_date(cls, requesting_organisation=None, user_id=None, closed=None, q=None, desc=False, status=None, state=None):
def get_ordered_by_date(cls, organization_id=None, user_id=None, closed=None, q=None, desc=False, status=None, state=None):
'''Personalized query'''
query = model.Session.query(cls).autoflush(False)
if state is None:
Expand All @@ -65,8 +65,8 @@ def get_ordered_by_date(cls, requesting_organisation=None, user_id=None, closed=

params = {}

if requesting_organisation is not None:
params['requesting_organisation'] = requesting_organisation
if organization_id is not None:
params['organization_id'] = organization_id

if user_id is not None:
params['user_id'] = user_id
Expand All @@ -88,27 +88,27 @@ def get_ordered_by_date(cls, requesting_organisation=None, user_id=None, closed=
# For sysadmins, we show all the data requests.
restricted_org_id = None

# If it is regular user, and the requesting_organisation is not provided, filter it based on current user's organizations.
# If it is regular user, and the organization_id is not provided, filter it based on current user's organizations.
if not current_user.sysadmin:
current_user_orgs = h.organizations_available('read') or []
restricted_org_id = [org['id'] for org in current_user_orgs]

if requesting_organisation is None:
# If the requesting_organisation is not provided, show the data requests created by the current user
if organization_id is None:
# If the organization_id is not provided, show the data requests created by the current user
# or all data request within the current user's organizations.
query = query.filter(or_(cls.user_id == current_user.id, cls.requesting_organisation.in_(restricted_org_id)))
query = query.filter(or_(cls.user_id == current_user.id, cls.organization_id.in_(restricted_org_id)))
else:
if requesting_organisation not in restricted_org_id:
# If the requesting_organisation is not within the current user's organizations,
if organization_id not in restricted_org_id:
# If the organization_id is not within the current user's organizations,
# show only the data requests created by the current user.
query = query.filter(cls.user_id == current_user.id)

# Remove the requesting_organisation from the filter.
query = query.filter(cls.requesting_organisation is not None)
# Remove the organization_id from the filter.
query = query.filter(cls.organization_id is not None)
else:
# Else the requesting_organisation is within the current user's organizations,
# Else the organization_id is within the current user's organizations,
# show the data requests created by the current user or all data request within selected organization.
query = query.filter(or_(cls.user_id == current_user.id, cls.requesting_organisation == requesting_organisation))
query = query.filter(or_(cls.user_id == current_user.id, cls.organization_id == organization_id))

current_user_id = current_user.id if current_user else None
if current_user_id:
Expand Down
2 changes: 1 addition & 1 deletion ckanext/datarequests/templates/datarequests/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@
<section class="module">
<div class="module-content">
{% block page_primary_action %}
{% snippet 'snippets/custom_search_form.html', query=q, fields=(('requesting_organisation', requesting_organisation), ('state', state)), sorting=filters, sorting_selected=sort, placeholder=_('Search Data Requests...'), no_bottom_border=true, count=datarequest_count, no_title=True %}
{% snippet 'snippets/custom_search_form.html', query=q, fields=(('organization', organization), ('state', state)), sorting=filters, sorting_selected=sort, placeholder=_('Search Data Requests...'), no_bottom_border=true, count=datarequest_count, no_title=True %}
{{ h.snippet('datarequests/snippets/datarequest_list.html', datarequest_count=datarequest_count, datarequests=datarequests, page=page, q=q)}}
{% endblock %}
</div>
Expand Down

0 comments on commit 9b79ad0

Please sign in to comment.