-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(FCL-499): add new FCLID identifier class
This allows us to store and create SQID-based FCLIDs, using the document sequence counter in MarkLogic
- Loading branch information
1 parent
dc98793
commit 77e6e62
Showing
2 changed files
with
50 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters