Skip to content

Commit

Permalink
feat(FCL-499): add new FCLID identifier class
Browse files Browse the repository at this point in the history
This allows us to store and create SQID-based FCLIDs, using the document sequence counter in MarkLogic
  • Loading branch information
jacksonj04 committed Dec 16, 2024
1 parent dc98793 commit 77e6e62
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 0 deletions.
48 changes: 48 additions & 0 deletions src/caselawclient/models/identifiers/fclid.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,48 @@
import re
from typing import TYPE_CHECKING

from sqids import Sqids

from . import Identifier, IdentifierSchema

if TYPE_CHECKING:
from caselawclient.Client import MarklogicApiClient


VALID_FCLID_PATTERN = re.compile(r"^[bcdfghjkmnpqrstvwxyz23456789]{4,}$")

FCLID_MINIMUM_LENGTH = 8
FCLID_ALPHABET = "bcdfghjkmnpqrstvwxyz23456789"

sqids = Sqids(
min_length=FCLID_MINIMUM_LENGTH,
alphabet=FCLID_ALPHABET,
)


class FindCaseLawIdentifierSchema(IdentifierSchema):
"""
Identifier schema describing a Find Case Law Identifier.
"""

name = "Find Case Law Identifier"
namespace = "fclid"

@classmethod
def validate_identifier(cls, value: str) -> bool:
return bool(VALID_FCLID_PATTERN.match(value))

@classmethod
def compile_identifier_url_slug(cls, value: str) -> str:
return "tna." + value

@classmethod
def mint(cls, api_client: "MarklogicApiClient") -> "FindCaseLawIdentifier":
"""Generate a totally new Find Case Law identifier."""
next_sequence_number = api_client.get_next_document_sequence_number()
new_identifier = sqids.encode([next_sequence_number])
return FindCaseLawIdentifier(value=new_identifier)


class FindCaseLawIdentifier(Identifier):
schema = FindCaseLawIdentifierSchema
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

0 comments on commit 77e6e62

Please sign in to comment.