From 97d8d5f69b3ead4c21976cf84346496f1efcfc77 Mon Sep 17 00:00:00 2001 From: Clinton Graham Date: Thu, 1 Sep 2022 17:11:21 -0400 Subject: [PATCH 1/3] #222: Use ASnake to format the resource id --- process_request/helpers.py | 10 +++++++++- process_request/routines.py | 7 ++++--- request_broker/config.py.deploy | 1 + request_broker/config.py.example | 1 + request_broker/settings.py | 1 + 5 files changed, 16 insertions(+), 4 deletions(-) diff --git a/process_request/helpers.py b/process_request/helpers.py index 2a687f1..bac6417 100644 --- a/process_request/helpers.py +++ b/process_request/helpers.py @@ -3,7 +3,7 @@ import inflect import shortuuid -from asnake.utils import get_date_display, get_note_text, text_in_note +from asnake.utils import format_resource_id, get_date_display, get_note_text, text_in_note from django.conf import settings from ordered_set import OrderedSet @@ -439,3 +439,11 @@ def resolve_ref_id(repo_id, ref_id, client): aspace_obj = aspace_objs['archival_objects'][0]['ref'] resolved = identifier_from_uri(aspace_obj) return resolved + + +def get_formatted_resource_id(resource, client): + """Gets a formatted resource id from the resource + + Concatenates the resource id parts using the separator from the config + """ + return format_resource_id(resource, client, settings.RESOURCE_ID_SEPARATOR) diff --git a/process_request/routines.py b/process_request/routines.py index 7f406e7..5ec67f6 100644 --- a/process_request/routines.py +++ b/process_request/routines.py @@ -5,8 +5,8 @@ from django.conf import settings from django.core.mail import send_mail -from .helpers import (get_container_indicators, get_dates, get_parent_title, - get_preferred_format, get_resource_creators, +from .helpers import (get_container_indicators, get_dates, get_formatted_resource_id, + get_parent_title, get_preferred_format, get_resource_creators, get_restricted_in_container, get_rights_info, get_size, get_url, list_chunks) @@ -58,6 +58,7 @@ def get_data(self, uri_list, dimes_baseurl): parent = self.strip_tags(get_parent_title(item_json.get("ancestors")[0].get("_resolved"))) if len(item_json.get("ancestors")) > 1 else None format, container, subcontainer, location, barcode, container_uri = get_preferred_format(item_json) restrictions, restrictions_text = get_rights_info(item_json, aspace.client) + resource_id = get_formatted_resource_id(item_collection, aspace.client) data.append({ "creators": get_resource_creators(item_collection, aspace.client), "restrictions": restrictions, @@ -66,7 +67,7 @@ def get_data(self, uri_list, dimes_baseurl): "collection_name": self.strip_tags(item_collection.get("title")), "parent": parent, "dates": get_dates(item_json, aspace.client), - "resource_id": item_collection.get("id_0"), + "resource_id": resource_id, "title": self.strip_tags(item_json.get("display_string")), "uri": item_json["uri"], "dimes_url": get_url(item_json, dimes_baseurl, aspace.client), diff --git a/request_broker/config.py.deploy b/request_broker/config.py.deploy index 4576251..cef60ad 100644 --- a/request_broker/config.py.deploy +++ b/request_broker/config.py.deploy @@ -22,3 +22,4 @@ DEFAULT_FROM_EMAIL = "${DEFAULT_FROM_EMAIL}" DIMES_HOSTNAME = "${DIMES_HOSTNAME}" RESTRICTED_IN_CONTAINER = ${RESTRICTED_IN_CONTAINER} OFFSITE_BUILDINGS = ${OFFSITE_BUILDINGS} +RESOURCE_ID_SEPARATOR = ${RESOURCE_ID_SEPARATOR} diff --git a/request_broker/config.py.example b/request_broker/config.py.example index 0b2d701..9e8bf07 100644 --- a/request_broker/config.py.example +++ b/request_broker/config.py.example @@ -22,3 +22,4 @@ DEFAULT_FROM_EMAIL = "dimes@example.com" # user that should be set as the sende DIMES_HOSTNAME = "dimes.rockarch.org" # Hostname for DIMES application RESTRICTED_IN_CONTAINER = False # Fetch a list of restricted items in the same container as the requested item. OFFSITE_BUILDINGS = ["Armonk", "Greenrock"] # Names of offsite buildings, which will be added to locations (list of strings) +RESOURCE_ID_SEPARATOR = ':' diff --git a/request_broker/settings.py b/request_broker/settings.py index de8ce08..e978cfd 100644 --- a/request_broker/settings.py +++ b/request_broker/settings.py @@ -165,3 +165,4 @@ RESTRICTED_IN_CONTAINER = config.RESTRICTED_IN_CONTAINER OFFSITE_BUILDINGS = getattr(config, 'OFFSITE_BUILDINGS', []) +RESOURCE_ID_SEPARATOR = config.RESOURCE_ID_SEPARATOR From dba476d5492a20e49f81661734aa259859009ec9 Mon Sep 17 00:00:00 2001 From: Hillel Arnold Date: Thu, 1 Sep 2022 17:37:43 -0400 Subject: [PATCH 2/3] linting --- process_request/helpers.py | 3 ++- process_request/routines.py | 5 +++-- 2 files changed, 5 insertions(+), 3 deletions(-) diff --git a/process_request/helpers.py b/process_request/helpers.py index bac6417..15cd9bb 100644 --- a/process_request/helpers.py +++ b/process_request/helpers.py @@ -3,7 +3,8 @@ import inflect import shortuuid -from asnake.utils import format_resource_id, get_date_display, get_note_text, text_in_note +from asnake.utils import (format_resource_id, get_date_display, get_note_text, + text_in_note) from django.conf import settings from ordered_set import OrderedSet diff --git a/process_request/routines.py b/process_request/routines.py index 5ec67f6..e9911e2 100644 --- a/process_request/routines.py +++ b/process_request/routines.py @@ -5,8 +5,9 @@ from django.conf import settings from django.core.mail import send_mail -from .helpers import (get_container_indicators, get_dates, get_formatted_resource_id, - get_parent_title, get_preferred_format, get_resource_creators, +from .helpers import (get_container_indicators, get_dates, + get_formatted_resource_id, get_parent_title, + get_preferred_format, get_resource_creators, get_restricted_in_container, get_rights_info, get_size, get_url, list_chunks) From 228a2ce369bf3efdbe8c0c3a258389646a793849 Mon Sep 17 00:00:00 2001 From: Hillel Arnold Date: Thu, 1 Sep 2022 17:45:18 -0400 Subject: [PATCH 3/3] adds test --- process_request/tests.py | 20 +++++++++++++++----- 1 file changed, 15 insertions(+), 5 deletions(-) diff --git a/process_request/tests.py b/process_request/tests.py index 66685b0..bcedd1c 100644 --- a/process_request/tests.py +++ b/process_request/tests.py @@ -13,11 +13,11 @@ from rest_framework.test import APIRequestFactory from .helpers import (get_container_indicators, get_dates, get_file_versions, - get_instance_data, get_locations, get_parent_title, - get_preferred_format, get_resource_creators, - get_restricted_in_container, get_rights_info, - get_rights_status, get_rights_text, get_size, - indicator_to_integer, prepare_values) + get_formatted_resource_id, get_instance_data, + get_locations, get_parent_title, get_preferred_format, + get_resource_creators, get_restricted_in_container, + get_rights_info, get_rights_status, get_rights_text, + get_size, indicator_to_integer, prepare_values) from .models import User from .routines import AeonRequester, Mailer, Processor from .test_helpers import json_from_fixture, random_list, random_string @@ -229,6 +229,16 @@ def test_get_restricted_in_container(self, mock_client): result = get_restricted_in_container("/repositories/2/top_container/1", mock_client) self.assertEqual(result, expected) + @patch("asnake.client.web_client.ASnakeClient") + def test_get_formatted_resource_id(self, mock_client): + for fixture, expected in [ + ({"id_0": "FA123"}, "FA123"), + ({"id_0": "FA123", "id_1": "001"}, "FA123:001"), + ({"id_0": "FA123", "id_1": "001", "id_2": "A"}, "FA123:001:A"), + ({"id_0": "FA123", "id_1": "001", "id_2": "A", "id_3": "dev"}, "FA123:001:A:dev")]: + result = get_formatted_resource_id(fixture, mock_client) + self.assertEqual(result, expected) + # Test is commented out as the code is currently not used, and this allows us to shed a few configs # def test_aeon_client(self): # baseurl = random_string(20)