Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jacksonj04 committed Dec 16, 2024
1 parent 24fb7f6 commit f9ffa87
Show file tree
Hide file tree
Showing 6 changed files with 22 additions and 0 deletions.
7 changes: 7 additions & 0 deletions src/caselawclient/models/documents/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,7 @@
NotSupportedOnVersion,
OnlySupportedOnVersion,
)
from caselawclient.models.identifiers.fclid import FindCaseLawIdentifierSchema, FindCaseLawIdentifier
from caselawclient.models.identifiers.unpacker import unpack_all_identifiers_from_etree
from caselawclient.models.utilities import VersionsDict, extract_version, render_versions
from caselawclient.models.utilities.aws import (
Expand Down Expand Up @@ -431,6 +432,12 @@ def publish(self) -> None:
"""
if not self.is_publishable:
raise CannotPublishUnpublishableDocument

## If it doesn't already have one, get a new FCLID for this document and assign
if len(self.identifiers.of_type(FindCaseLawIdentifier)) < 1:
document_fclid = FindCaseLawIdentifierSchema.mint(self.api_client)
self.identifiers.add(document_fclid)
self.save_identifiers()

publish_documents(uri_for_s3(self.uri))
self.api_client.set_published(self.uri, True)
Expand Down
5 changes: 5 additions & 0 deletions src/caselawclient/models/identifiers/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -127,6 +127,11 @@ def __delitem__(self, key: Union[Identifier, str]) -> None:
else:
super().__delitem__(key)

def of_type(self, identifier_type: type[Identifier]) -> list[Identifier]:
""" Return a list of all identifiers of a given type. """
uuids = self.keys()
return [self[uuid] for uuid in list(uuids) if isinstance(self[uuid], identifier_type)]

def delete_type(self, deleted_identifier_type: type[Identifier]) -> None:
"For when we want an identifier to be the only valid identifier of that type, delete the others first"
uuids = self.keys()
Expand Down
2 changes: 2 additions & 0 deletions src/caselawclient/models/identifiers/unpacker.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,11 @@
from lxml import etree

from . import IDENTIFIER_UNPACKABLE_ATTRIBUTES, Identifier, Identifiers, InvalidIdentifierXMLRepresentationException
from .fclid import FindCaseLawIdentifier
from .neutral_citation import NeutralCitationNumber

IDENTIFIER_NAMESPACE_MAP: dict[str, type[Identifier]] = {
"fclid": FindCaseLawIdentifier,
"ukncn": NeutralCitationNumber,
}

Expand Down
1 change: 1 addition & 0 deletions tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,5 +130,6 @@ def mock_api_client():
mock_client = Mock(spec=MarklogicApiClient)
mock_client.get_judgment_xml_bytestring.return_value = b"<xml>content</xml>"
mock_client.get_property_as_node.return_value = None
mock_client.get_next_document_sequence_number.return_value = 1

return mock_client
1 change: 1 addition & 0 deletions tests/models/documents/test_document_verbs.py
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,7 @@ def test_publish(
):
document = Document(DocumentURIString("test/1234"), mock_api_client)
document.is_publishable = True

document.publish()
mock_publish_documents.assert_called_once_with("test/1234")
mock_api_client.set_published.assert_called_once_with("test/1234", True)
Expand Down
6 changes: 6 additions & 0 deletions tests/models/identifiers/test_identifiers.py
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,12 @@ def test_contains(self, identifiers):
assert not identifiers.contains(TestIdentifier(value="TEST-333"))
assert not identifiers.contains(NeutralCitationNumber(value="TEST-111"))

def test_of_type(self, mixed_identifiers):
only_ncns = mixed_identifiers.of_type(NeutralCitationNumber)
assert "TEST-999" not in str(only_ncns)
assert "[1701] UKSC 999" in str(only_ncns)
assert "[1234] UKSC 999" in str(only_ncns)

def test_delete_type(self, mixed_identifiers):
mixed_identifiers.delete_type(NeutralCitationNumber)
assert "TEST-999" in str(mixed_identifiers)
Expand Down

0 comments on commit f9ffa87

Please sign in to comment.