From 6f15b8bc2a423ff181b60b33c35311d4959f7a26 Mon Sep 17 00:00:00 2001 From: Dan Mihaila Date: Tue, 19 Sep 2023 13:21:49 +0300 Subject: [PATCH] HDX-9010 - adding test and validator for title (include inactive) --- .../hdx_org_group/helpers/custom_validator.py | 18 ++ .../ckanext/hdx_org_group/plugin.py | 232 +++++++++--------- .../tests/test_closed_organization.py | 45 ++++ .../tests/test_org_custom_fields.py | 8 +- .../hdx_theme/fanstatic/search_/search.js | 4 +- .../snippets/organization_form.html | 4 +- 6 files changed, 188 insertions(+), 123 deletions(-) create mode 100644 ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_closed_organization.py diff --git a/ckanext-hdx_org_group/ckanext/hdx_org_group/helpers/custom_validator.py b/ckanext-hdx_org_group/ckanext/hdx_org_group/helpers/custom_validator.py index 56490dc839..cbd745c6ab 100644 --- a/ckanext-hdx_org_group/ckanext/hdx_org_group/helpers/custom_validator.py +++ b/ckanext-hdx_org_group/ckanext/hdx_org_group/helpers/custom_validator.py @@ -47,3 +47,21 @@ def active_if_missing(key, data, errors, context): if value is missing or value is None: data[key] = 'active' +def set_inactive_if_closed_organization(key, data, errors, context): + value = data.get(key) + title = data.get(('title',)) + if title: + if value=='true' or value=='True': + title= title.replace('(closed)','') + if not 'inactive' in title: + if title.endswith(' '): + title = title + '(inactive)' + else: + title = title + ' (inactive)' + else: + title = title.replace('(closed)', '') + title = title.replace('(inactive)', '') + if title.endswith(' '): + title = title[:-1] + data[('title',)]= title + diff --git a/ckanext-hdx_org_group/ckanext/hdx_org_group/plugin.py b/ckanext-hdx_org_group/ckanext/hdx_org_group/plugin.py index d3e24d96da..9372c97d21 100644 --- a/ckanext-hdx_org_group/ckanext/hdx_org_group/plugin.py +++ b/ckanext-hdx_org_group/ckanext/hdx_org_group/plugin.py @@ -67,7 +67,7 @@ def get_actions(self): 'invalidate_data_completeness_for_location': update_actions.invalidate_data_completeness_for_location, 'hdx_organization_follower_list': get_actions.hdx_organization_follower_list, 'hdx_user_invite': create_actions.hdx_user_invite, - 'member_create': create_actions.hdx_member_create + 'member_create': create_actions.hdx_member_create } @@ -106,8 +106,8 @@ def get_validators(self): return { 'correct_hdx_org_type': custom_validator.general_value_in_list(org_type_keys, False), 'hdx_org_keep_prev_value_if_empty_unless_sysadmin': org_custom_validator.hdx_org_keep_prev_value_if_empty_unless_sysadmin, - 'active_if_missing': org_custom_validator.active_if_missing - + 'active_if_missing': org_custom_validator.active_if_missing, + 'set_inactive_if_closed_organization': org_custom_validator.set_inactive_if_closed_organization } def _modify_group_schema(self, schema): @@ -122,7 +122,9 @@ def _modify_group_schema(self, schema): tk.get_converter('convert_to_extras')], 'custom_org': [tk.get_validator('ignore_missing'), tk.get_converter('convert_to_extras')], 'request_membership': [tk.get_validator('ignore_missing'), tk.get_converter('convert_to_extras')], - 'closed_organization': [tk.get_validator('hdx_boolean_string_converter'), tk.get_converter('convert_to_extras')], + 'closed_organization': [tk.get_validator('hdx_boolean_string_converter'), + tk.get_validator('set_inactive_if_closed_organization'), + tk.get_converter('convert_to_extras')], 'customization': [tk.get_validator('ignore_missing'), tk.get_converter('convert_to_extras')], # 'less': [tk.get_validator('ignore_missing'), tk.get_converter('convert_to_extras')], 'visualization_config': [tk.get_validator('ignore_missing'), tk.get_converter('convert_to_extras')], @@ -191,118 +193,118 @@ def db_to_form_schema(self): return None # def after_map(self, map): - # map.connect('organization_read', '/organization/{id}', - # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', - # action='read') - # map.connect('organization_members', '/organization/members/{id}', - # controller='ckanext.hdx_org_group.controllers.member_controller:HDXOrgMemberController', - # action='members') - # map.connect('organization_activity', '/organization/activity/{id}', - # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', - # action='activity_stream') - # map.connect('organization_activity', '/organization/activity/{id}', - # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', - # action='activity_stream') - # map.connect('organization_activity_offset', '/organization/activity/{id}/{offset}', - # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', - # action='activity_stream') - # return map + # map.connect('organization_read', '/organization/{id}', + # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', + # action='read') + # map.connect('organization_members', '/organization/members/{id}', + # controller='ckanext.hdx_org_group.controllers.member_controller:HDXOrgMemberController', + # action='members') + # map.connect('organization_activity', '/organization/activity/{id}', + # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', + # action='activity_stream') + # map.connect('organization_activity', '/organization/activity/{id}', + # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', + # action='activity_stream') + # map.connect('organization_activity_offset', '/organization/activity/{id}/{offset}', + # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', + # action='activity_stream') + # return map # def before_map(self, map): - # map.connect('organization_bulk_process', - # '/organization/bulk_process/{org_id}', - # controller='ckanext.hdx_org_group.controllers.redirect_controller:RedirectController', - # action='redirect_to_org_list') - # map.connect('organization_bulk_process_no_id', '/organization/bulk_process', - # controller='ckanext.hdx_org_group.controllers.redirect_controller:RedirectController', - # action='redirect_to_org_list') - # map.connect('organizations_index', '/organization', - # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', - # action='index') - # map.connect('organization_new', '/organization/new', - # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', - # action='new') - # map.connect('organization_edit', '/organization/edit/{id}', - # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', - # action='edit', ckan_icon='edit') - # map.connect('request_membership', '/organization/{org_id}/request_membership', - # controller='ckanext.hdx_org_group.controllers.request_controller:HDXReqsOrgController', action='request_membership') - # map.connect('request_editing_rights', '/organization/{org_id}/request_editing_rights', - # controller='ckanext.hdx_org_group.controllers.request_controller:HDXReqsOrgController', - # action='request_editor_for_org') - # map.connect('/organization/request_new', - # controller='ckanext.hdx_org_group.controllers.request_controller:HDXReqsOrgController', - # action='request_new_organization') - # map.connect('organization_members', - # '/organization/members/{id}', - # controller='ckanext.hdx_org_group.controllers.member_controller:HDXOrgMemberController', - # action='members') - # map.connect( - # '/organization/member_new/{id}', - # controller='ckanext.hdx_org_group.controllers.member_controller:HDXOrgMemberController', - # action='member_new') - # map.connect( - # '/organization/member_delete/{id}', - # controller='ckanext.hdx_org_group.controllers.member_controller:HDXOrgMemberController', - # action='member_delete') - - # map.connect('/organization/bulk_member_new/{id}', - # controller='ckanext.hdx_org_group.controllers.member_controller:HDXOrgMemberController', - # action='bulk_member_new') - - # map.connect('organization_activity', '/organization/activity/{id}', - # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', - # action='activity_stream' - # # conditions={'function': organization_controller.is_not_custom} - # ) - # map.connect('organization_activity_offset', '/organization/activity/{id}/{offset:([0-9]+)}', - # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', - # action='activity_stream') - # map.connect('custom_org_activity', '/organization/activity/{id}', - # controller='ckanext.hdx_org_group.controllers.custom_org_controller:CustomOrgController', - # action='activity_stream') - - # since the pattern of organization_read is so general it needs to be the last - # otherwise it will override other /organization routes - # map.connect('organization_read', '/organization/{id}', - # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', - # action='read') - - # map.connect('hdx_organization_stats', '/organization/stats/{id}', - # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', - # action='stats') - - # map.connect('browse_list', '/browse', - # controller='ckanext.hdx_org_group.controllers.browse_controller:BrowseController', action='index') - - # map.connect('browse_list', '/browse', - # controller='ckanext.hdx_org_group.controllers.redirect_controller:RedirectController', - # action='redirect_to_group_list') - - # map.connect('group_index', '/group', - # controller='ckanext.hdx_org_group.controllers.group_controller:HDXGroupController', action='index', - # highlight_actions='index search') - # map.connect('group_worldmap', '/worldmap', - # controller='ckanext.hdx_org_group.controllers.group_controller:HDXGroupController', action='group_worldmap') - # - # map.connect('group_eaa_worldmap', '/eaa-worldmap', - # controller='ckanext.hdx_org_group.controllers.group_controller:HDXGroupController', action='group_eaa_worldmap') - - # map.connect('group_new', '/group/new', controller='group', action='new') - - # map.connect('country_read', '/group/{id}', - # controller='ckanext.hdx_org_group.controllers.country_controller:CountryController', action='country_read') - # - # map.connect('country_topline', '/country/topline/{id}', - # controller='ckanext.hdx_org_group.controllers.country_controller:CountryController', action='country_topline') - - # map.connect('feed_org_atom', '/feeds/organization/{id}.atom', controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', - # action='feed_organization') - - # map.connect( - # 'custom_org_read', '/org/{id}', controller='ckanext.hdx_org_group.controllers.custom_org_controller:CustomOrgController', action='org_read') - - # return map + # map.connect('organization_bulk_process', + # '/organization/bulk_process/{org_id}', + # controller='ckanext.hdx_org_group.controllers.redirect_controller:RedirectController', + # action='redirect_to_org_list') + # map.connect('organization_bulk_process_no_id', '/organization/bulk_process', + # controller='ckanext.hdx_org_group.controllers.redirect_controller:RedirectController', + # action='redirect_to_org_list') + # map.connect('organizations_index', '/organization', + # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', + # action='index') + # map.connect('organization_new', '/organization/new', + # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', + # action='new') + # map.connect('organization_edit', '/organization/edit/{id}', + # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', + # action='edit', ckan_icon='edit') + # map.connect('request_membership', '/organization/{org_id}/request_membership', + # controller='ckanext.hdx_org_group.controllers.request_controller:HDXReqsOrgController', action='request_membership') + # map.connect('request_editing_rights', '/organization/{org_id}/request_editing_rights', + # controller='ckanext.hdx_org_group.controllers.request_controller:HDXReqsOrgController', + # action='request_editor_for_org') + # map.connect('/organization/request_new', + # controller='ckanext.hdx_org_group.controllers.request_controller:HDXReqsOrgController', + # action='request_new_organization') + # map.connect('organization_members', + # '/organization/members/{id}', + # controller='ckanext.hdx_org_group.controllers.member_controller:HDXOrgMemberController', + # action='members') + # map.connect( + # '/organization/member_new/{id}', + # controller='ckanext.hdx_org_group.controllers.member_controller:HDXOrgMemberController', + # action='member_new') + # map.connect( + # '/organization/member_delete/{id}', + # controller='ckanext.hdx_org_group.controllers.member_controller:HDXOrgMemberController', + # action='member_delete') + + # map.connect('/organization/bulk_member_new/{id}', + # controller='ckanext.hdx_org_group.controllers.member_controller:HDXOrgMemberController', + # action='bulk_member_new') + + # map.connect('organization_activity', '/organization/activity/{id}', + # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', + # action='activity_stream' + # # conditions={'function': organization_controller.is_not_custom} + # ) + # map.connect('organization_activity_offset', '/organization/activity/{id}/{offset:([0-9]+)}', + # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', + # action='activity_stream') + # map.connect('custom_org_activity', '/organization/activity/{id}', + # controller='ckanext.hdx_org_group.controllers.custom_org_controller:CustomOrgController', + # action='activity_stream') + + # since the pattern of organization_read is so general it needs to be the last + # otherwise it will override other /organization routes + # map.connect('organization_read', '/organization/{id}', + # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', + # action='read') + + # map.connect('hdx_organization_stats', '/organization/stats/{id}', + # controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', + # action='stats') + + # map.connect('browse_list', '/browse', + # controller='ckanext.hdx_org_group.controllers.browse_controller:BrowseController', action='index') + + # map.connect('browse_list', '/browse', + # controller='ckanext.hdx_org_group.controllers.redirect_controller:RedirectController', + # action='redirect_to_group_list') + + # map.connect('group_index', '/group', + # controller='ckanext.hdx_org_group.controllers.group_controller:HDXGroupController', action='index', + # highlight_actions='index search') + # map.connect('group_worldmap', '/worldmap', + # controller='ckanext.hdx_org_group.controllers.group_controller:HDXGroupController', action='group_worldmap') + # + # map.connect('group_eaa_worldmap', '/eaa-worldmap', + # controller='ckanext.hdx_org_group.controllers.group_controller:HDXGroupController', action='group_eaa_worldmap') + + # map.connect('group_new', '/group/new', controller='group', action='new') + + # map.connect('country_read', '/group/{id}', + # controller='ckanext.hdx_org_group.controllers.country_controller:CountryController', action='country_read') + # + # map.connect('country_topline', '/country/topline/{id}', + # controller='ckanext.hdx_org_group.controllers.country_controller:CountryController', action='country_topline') + + # map.connect('feed_org_atom', '/feeds/organization/{id}.atom', controller='ckanext.hdx_org_group.controllers.organization_controller:HDXOrganizationController', + # action='feed_organization') + + # map.connect( + # 'custom_org_read', '/org/{id}', controller='ckanext.hdx_org_group.controllers.custom_org_controller:CustomOrgController', action='org_read') + + # return map # IOrganizationController def create(self, org): @@ -387,7 +389,6 @@ def form_to_db_schema(self): # IGroupForm def db_to_form_schema(self): - schema = super(HDXGroupPlugin, self).form_to_db_schema() schema.update({ 'language_code': [tk.get_converter('convert_from_extras'), tk.get_validator('ignore_missing')], @@ -446,4 +447,3 @@ def get_blueprint(self): import ckanext.hdx_org_group.views.group as group import ckanext.hdx_org_group.views.light_group as light_group return [group.hdx_group, group.hdx_country_topline, light_group.hdx_light_group, light_group.hdx_group_eaa_maps] - diff --git a/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_closed_organization.py b/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_closed_organization.py new file mode 100644 index 0000000000..e79ad5f763 --- /dev/null +++ b/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_closed_organization.py @@ -0,0 +1,45 @@ +''' +reCreated on 19 Sep, 2023 + +@author: dan +''' + +import pytest +import logging as logging +import ckan.model as model +import ckan.plugins.toolkit as tk + +_get_action = tk.get_action +NotAuthorized = tk.NotAuthorized +log = logging.getLogger(__name__) + + +@pytest.mark.usefixtures('keep_db_tables_on_clean', 'clean_db', 'clean_index', 'setup_user_data', + 'with_request_context') +class TestClosedOrg(object): + + def test_closed_organization(self, app): + + orgadmin = 'orgadmin' + context = {'model': model, 'session': model.Session, 'user': orgadmin} + + org_dict = _get_action('organization_show')(context, {'id': 'hdx-test-org', 'include_users':True}) + + org_dict['closed_organization'] = True + result = _get_action('organization_update')(context, org_dict) + org_dict = _get_action('organization_show')(context, {'id': 'hdx-test-org', 'include_users':True}) + assert '(inactive)' in org_dict.get('title') + + org_dict['closed_organization'] = False + context = {'model': model, 'session': model.Session, 'user': orgadmin} + result = _get_action('organization_update')(context, org_dict) + org_dict = _get_action('organization_show')(context, {'id': 'hdx-test-org', 'include_users':True}) + assert '(inactive)' not in org_dict.get('title') + + org_dict['closed_organization'] = True + org_dict['title'] = 'HDX TEST ORG 123 (closed)' + context = {'model': model, 'session': model.Session, 'user': orgadmin} + result = _get_action('organization_update')(context, org_dict) + org_dict = _get_action('organization_show')(context, {'id': 'hdx-test-org'}) + assert '(inactive)' in org_dict.get('title') + assert '123' in org_dict.get('title') diff --git a/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_org_custom_fields.py b/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_org_custom_fields.py index b3b5149d05..61425c3652 100644 --- a/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_org_custom_fields.py +++ b/ckanext-hdx_org_group/ckanext/hdx_org_group/tests/test_org_custom_fields.py @@ -482,7 +482,7 @@ def test_closed_organization_api(self): new_org_dict = { 'name': 'test_org_d', 'title': 'Test Org D', - 'closed_organization': 'true', + 'closed_organization': True, 'org_url': 'www.exampleorganization.org', 'description': 'just a simple description', 'hdx_org_type': ORGANIZATION_TYPE_LIST[0][1] @@ -491,6 +491,7 @@ def test_closed_organization_api(self): org_dict = self._get_action('organization_create')(context_sysadmin, new_org_dict) assert 'closed_organization' in org_dict assert org_dict.get('closed_organization') == True + assert '(inactive)' in org_dict.get('title') except Exception as ex: assert False @@ -499,7 +500,7 @@ def test_closed_organization_api(self): 'id': org_dict.get('id'), 'name': org_dict.get('name'), 'title': org_dict.get('title'), - 'closed_organization': 'false', + 'closed_organization': False, 'org_url': org_dict.get('org_url'), 'description': org_dict.get('description'), 'hdx_org_type': org_dict.get('hdx_org_type'), @@ -508,6 +509,7 @@ def test_closed_organization_api(self): org_updated_dict = self._get_action('organization_show')(context_sysadmin, {'id': org_dict.get('id')}) assert 'closed_organization' in org_updated_dict assert org_updated_dict.get('closed_organization') == False + assert '(inactive)' not in org_updated_dict.get('title') except Exception as ex: assert False @@ -532,6 +534,7 @@ def test_closed_organization_api(self): org_updated_dict = self._get_action('organization_show')(context_sysadmin, {'id': _org_update_dict.get('id')}) assert 'closed_organization' in org_updated_dict assert org_updated_dict.get('closed_organization') == False + assert '(inactive)' not in org_updated_dict.get('title') except Exception as ex: assert False @@ -549,6 +552,7 @@ def test_closed_organization_api(self): org_updated_dict = self._get_action('organization_show')(context_sysadmin, {'id': org_dict.get('id')}) assert 'closed_organization' in org_updated_dict assert org_updated_dict.get('closed_organization') == True + assert '(inactive)' in org_updated_dict.get('title') except Exception as ex: assert False diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/search_/search.js b/ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/search_/search.js index 6b55f66272..664f1ff684 100644 --- a/ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/search_/search.js +++ b/ckanext-hdx_theme/ckanext/hdx_theme/fanstatic/search_/search.js @@ -31,13 +31,11 @@ $('document').ready(function(){ }); } - // filter out "closed" items - const not_closed_results = results.filter(obj => {return obj.title.includes("(closed)")===false; }); return { 'q': query, 'termList': termList, 'modifiedQ': modifiedQ, - 'results': not_closed_results + 'results': results }; }; diff --git a/ckanext-hdx_theme/ckanext/hdx_theme/templates/organization/snippets/organization_form.html b/ckanext-hdx_theme/ckanext/hdx_theme/templates/organization/snippets/organization_form.html index 98f1082128..a2dc4d62df 100644 --- a/ckanext-hdx_theme/ckanext/hdx_theme/templates/organization/snippets/organization_form.html +++ b/ckanext-hdx_theme/ckanext/hdx_theme/templates/organization/snippets/organization_form.html @@ -93,8 +93,8 @@

-{% set closed_organization = h.hdx_get_extras_element(data, value_key="closed_organization") or "false" %} -{% set closed_organization_checked = 'checked="checked"' if closed_organization=='true' else '' %} +{% set closed_organization = h.hdx_get_extras_element(data, value_key="closed_organization") or False %} +{% set closed_organization_checked = 'checked="checked"' if closed_organization==True else '' %}