Skip to content

Commit

Permalink
Globally control DateBone localization
Browse files Browse the repository at this point in the history
As requested by viur-framework#565
  • Loading branch information
phorward committed Nov 28, 2022
1 parent b6dd1c1 commit fe8c81d
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 30 deletions.
32 changes: 16 additions & 16 deletions core/bones/date.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,8 @@
from viur.core.bones.base import BaseBone, ReadFromClientError, ReadFromClientErrorSeverity
from viur.core import db, request
from viur.core.i18n import translate
from visingleValueFromClientur.core.bones.base import BaseBone, ReadFromClientError, ReadFromClientErrorSeverity
from viur.core import conf, db, request
from viur.core.utils import currentRequest, currentRequestData, utcNow, isLocalDevelopmentServer
from datetime import datetime, timedelta
from typing import Dict, List, Optional, Union
from typing import Dict, Optional
import pytz, tzlocal


Expand All @@ -15,7 +14,7 @@ def __init__(
*,
creationMagic: bool = False,
date: bool = True,
localize: bool = False,
localize: bool = None,
time: bool = True,
updateMagic: bool = False,
**kwargs
Expand All @@ -33,24 +32,25 @@ def __init__(
"""
super().__init__(**kwargs)

if creationMagic or updateMagic:
self.readonly = True

self.creationMagic = creationMagic
self.updateMagic = updateMagic

if not (date or time):
raise ValueError("Attempt to create an empty datebone! Set date or time to True!")

if localize and not (date and time):
raise ValueError("Localization is only possible with date and time!")
if creationMagic or updateMagic:
self.readonly = True

if self.multiple and (creationMagic or updateMagic):
raise ValueError("Cannot be multiple and have a creation/update-magic set!")

self.date = date
self.time = time
self.localize = localize
if localize is None and date and time:
localize = conf["viur.bone.date.localize"]
elif localize and not (date and time):
raise ValueError("Localization is only possible with date and time!")

self.creationMagic = bool(creationMagic)
self.updateMagic = bool(updateMagic)
self.date = bool(date)
self.time = bool(time)
self.localize = bool(localize)

def singleValueFromClient(self, value: str, skel: 'viur.core.skeleton.SkeletonInstance', name: str, origData):
"""
Expand Down
3 changes: 3 additions & 0 deletions core/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,9 @@
# Allowed values that define a str to evaluate to true
"viur.bone.boolean.str2true": ("true", "yes", "1"),

# Globally localize DateBone
"viur.bone.date.localize": False,

# If set, this function will be called for each cache-attempt and the result will be included in
# the computed cache-key
"viur.cacheEnvironmentKey": None,
Expand Down
38 changes: 24 additions & 14 deletions core/skeleton.py
Original file line number Diff line number Diff line change
Expand Up @@ -607,24 +607,34 @@ class Skeleton(BaseSkeleton, metaclass=MetaSkel):
# The "key" bone stores the current database key of this skeleton.
# Warning: Assigning to this bones value now *will* set the key
# it gets stored in. Must be kept readOnly to avoid security-issues with add/edit.
key = KeyBone(descr="key", readOnly=True, visible=False)
key = KeyBone(
descr="Key",
readOnly=True,
visible=False
)

# The date (including time) when this entry has been created
creationdate = DateBone(descr="created at",
readOnly=True, visible=False,
creationMagic=True, indexed=True,
localize=bool(pytz))
creationdate = DateBone(
descr="Created at",
readOnly=True,
visible=False,
creationMagic=True
)

# The last date (including time) when this entry has been updated
changedate = DateBone(descr="updated at",
readOnly=True, visible=False,
updateMagic=True, indexed=True,
localize=bool(pytz))

viurCurrentSeoKeys = seoKeyBone(descr="Seo-Keys",
readOnly=True,
visible=False,
languages=conf["viur.availableLanguages"])
changedate = DateBone(
descr="Updated at",
readOnly=True,
visible=False,
updateMagic=True
)

viurCurrentSeoKeys = seoKeyBone(
descr="SEO-Keys",
readOnly=True,
visible=False,
languages=conf["viur.availableLanguages"]
)

def __repr__(self):
return "<skeleton %s with data=%r>" % (self.kindName, {k: self[k] for k in self.keys()})
Expand Down

0 comments on commit fe8c81d

Please sign in to comment.