Skip to content

Commit

Permalink
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Add fill_chars and docstrings
Browse files Browse the repository at this point in the history
ArneGudermann committed Apr 18, 2024
1 parent c0fbcaa commit 95c83a2
Showing 2 changed files with 15 additions and 10 deletions.
3 changes: 2 additions & 1 deletion src/viur/core/bones/base.py
Original file line number Diff line number Diff line change
@@ -283,7 +283,8 @@ def __init__(
if compute:
if not isinstance(compute, Compute):
raise TypeError("compute must be an instanceof of Compute")

if not isinstance(compute.fn, t.Callable):
raise ValueError("'compute.fn' must be callable")
# When readOnly is None, handle flag automatically
if readOnly is None:
self.readOnly = True
22 changes: 13 additions & 9 deletions src/viur/core/bones/uid.py
Original file line number Diff line number Diff line change
@@ -39,36 +39,39 @@ def transac(_key):

class UidBone(BaseBone):
"""
The "StringBone" represents a data field that contains text values.
The "UidBone" represents a data field that contains text values.
"""
type = "str"
type = "uid"

def __init__(
self,
*,
generate_fn: t.Callable = generate_uid,
fill_chars="",
length: int | None = 13,
length: int = 13,
pattern: str | t.Callable | None = "*",
compute: Compute = Compute(fn=generate_uid, interval=ComputeInterval(ComputeMethod.Once)),
unique=UniqueValue(UniqueLockMethod.SameValue, False, "Unique Value already in use"),
**kwargs
):
"""
Initializes a new UidBone.
:param generate_fn: The compute function to calculate the unique value,
:param fill_chars: The chars that are filed in when the uid has not the length.
:param length: The length allowed for values of this bone.
:param pattern: The pattern for this Bone. "*" will be replaced with the uid value.
:param kwargs: Inherited arguments from the BaseBone.
"""
# fixme: Remove in viur-core >= 4

super().__init__(compute=compute, unique=unique, **kwargs)
super().__init__(
compute=Compute(fn=generate_fn, interval=ComputeInterval(ComputeMethod.Once)),
unique=UniqueValue(UniqueLockMethod.SameValue, False, "Unique Value already in use"),
**kwargs
)

if self.multiple or self.languages:
raise ValueError("UidBone cannot be multiple or translated")
if not self.readOnly:
self.readOnly = True
# raise ValueError("UidBone must be readOnly")

self.fill_chars = str(fill_chars)
self.length = length
@@ -80,6 +83,7 @@ def __init__(

def structure(self) -> dict:
ret = super().structure() | {
"fill_chars": self.fill_chars,
"length": self.length,
"pattern": self.pattern
}

0 comments on commit 95c83a2

Please sign in to comment.