Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

#222: Use ASnake to format the resource id #236

Merged
merged 3 commits into from
Sep 1, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion process_request/helpers.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,8 @@

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

Expand Down Expand Up @@ -439,3 +440,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)
6 changes: 4 additions & 2 deletions process_request/routines.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +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,
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)
Expand Down Expand Up @@ -58,6 +59,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,
Expand All @@ -66,7 +68,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),
Expand Down
20 changes: 15 additions & 5 deletions process_request/tests.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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)
Expand Down
1 change: 1 addition & 0 deletions request_broker/config.py.deploy
Original file line number Diff line number Diff line change
Expand Up @@ -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}
1 change: 1 addition & 0 deletions request_broker/config.py.example
Original file line number Diff line number Diff line change
Expand Up @@ -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 = ':'
1 change: 1 addition & 0 deletions request_broker/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -165,3 +165,4 @@

RESTRICTED_IN_CONTAINER = config.RESTRICTED_IN_CONTAINER
OFFSITE_BUILDINGS = getattr(config, 'OFFSITE_BUILDINGS', [])
RESOURCE_ID_SEPARATOR = config.RESOURCE_ID_SEPARATOR