Add LibraryFolderDatabaseIdField

Handles the decoding and encoding of Library Folders which ID must start with an 'F'
This commit is contained in:
davelopez
2022-08-24 12:00:25 +02:00
committed by mvdbeek
parent dc6d61c690
commit 5e2a263924
2 changed files with 30 additions and 10 deletions
+28 -10
View File
@@ -26,6 +26,15 @@ class BaseDatabaseIdField:
def validate(cls, v):
return v
@classmethod
def ensure_valid(cls, v: str):
len_v = len(v)
if len_v % ENCODED_ID_LENGTH_MULTIPLE:
raise ValueError("Invalid id length, must be multiple of 16")
m = ENCODED_DATABASE_ID_PATTERN.fullmatch(v.lower())
if not m:
raise ValueError("Invalid characters in encoded ID")
@classmethod
def __modify_schema__(cls, field_schema):
# __modify_schema__ should mutate the dict it receives in place,
@@ -46,22 +55,31 @@ class DecodedDatabaseIdField(int, BaseDatabaseIdField):
def validate(cls, v):
if not isinstance(v, str):
raise TypeError("String required")
if v.startswith("F"):
# Library Folder ids start with an additional "F"
v = v[1:]
len_v = len(v)
if len_v % ENCODED_ID_LENGTH_MULTIPLE:
raise ValueError("Invalid id length, must be multiple of 16")
m = ENCODED_DATABASE_ID_PATTERN.fullmatch(v.lower())
if not m:
raise ValueError("Invalid characters in encoded ID")
cls.ensure_valid(v)
return cls(cls.security.decode_id(v))
@classmethod
def encode(cls, v):
def encode(cls, v) -> str:
return cls.security.encode_id(v)
class LibraryFolderDatabaseIdField(int, BaseDatabaseIdField):
@classmethod
def validate(cls, v):
if not isinstance(v, str):
raise TypeError("String required")
if not v.startswith("F"):
raise TypeError("Invalid library folder ID. Folder IDs must start with an 'F'")
v = v[1:]
cls.ensure_valid(v)
return cls(cls.security.decode_id(v))
@classmethod
def encode(cls, v) -> str:
return f"F{cls.security.encode_id(v)}"
# TODO: remove after replace
class EncodedDatabaseIdField(str, BaseDatabaseIdField):
@classmethod
def validate(cls, v):
+2
View File
@@ -40,6 +40,7 @@ from galaxy.model import (
from galaxy.schema.fields import (
DecodedDatabaseIdField,
EncodedDatabaseIdField,
LibraryFolderDatabaseIdField,
ModelClassField,
)
from galaxy.schema.types import (
@@ -188,6 +189,7 @@ class Model(BaseModel):
json_encoders = {
# This will ensure all IDs are encoded when serialized to JSON
DecodedDatabaseIdField: lambda v: DecodedDatabaseIdField.encode(v),
LibraryFolderDatabaseIdField: lambda v: LibraryFolderDatabaseIdField.encode(v),
}