add creation timestamp to movies
This commit is contained in:
parent
5514a8d1e1
commit
159ffce4b4
4 changed files with 74 additions and 12 deletions
|
|
@ -35,6 +35,9 @@ def fields(class_or_instance):
|
|||
|
||||
for f in _fields(class_or_instance):
|
||||
|
||||
if f.name == "_is_lazy":
|
||||
continue
|
||||
|
||||
if (attn := annotations(f.type)) and _RelationSentinel in attn:
|
||||
continue # Relations are ignored
|
||||
|
||||
|
|
@ -167,8 +170,36 @@ class Movie:
|
|||
imdb_votes: Optional[int] = None
|
||||
runtime: Optional[int] = None # minutes
|
||||
genres: set[str] = None
|
||||
created: datetime = field(default_factory=utcnow)
|
||||
updated: datetime = field(default_factory=utcnow)
|
||||
|
||||
_is_lazy: bool = field(default=False, init=False, repr=False, compare=False)
|
||||
|
||||
@classmethod
|
||||
def lazy(cls, **kwds):
|
||||
"""Return a new instance without running default factories.
|
||||
|
||||
This is meant purely for optimization purposes, to postpone possibly
|
||||
expensive initialization operations.
|
||||
"""
|
||||
# XXX optimize using a metaclass & storing field refs on the class
|
||||
kwds.setdefault("id", None)
|
||||
kwds.setdefault("created", None)
|
||||
kwds.setdefault("updated", None)
|
||||
movie = cls(**kwds)
|
||||
movie._is_lazy = True
|
||||
return movie
|
||||
|
||||
def _lazy_init(self):
|
||||
if not self._is_lazy:
|
||||
return
|
||||
|
||||
for field in fields(Movie):
|
||||
if getattr(self, field.name) is None and field.default_factory:
|
||||
setattr(self, field.name, field.default_factory())
|
||||
|
||||
self._is_lazy = False
|
||||
|
||||
|
||||
T = TypeVar("T")
|
||||
_RelationSentinel = object()
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue