home
·
contact
·
privacy
projects
/
plomtask
/ blobdiff
commit
grep
author
committer
pickaxe
?
search:
re
summary
|
shortlog
|
log
|
commit
|
commitdiff
|
tree
raw
|
inline
| side by side
Refactor BaseModel.from_table_row testing.
[plomtask]
/
plomtask
/
db.py
diff --git
a/plomtask/db.py
b/plomtask/db.py
index 1cecc16f6985b555f25757ad6e9f65724311a287..853b4c68c65780e339b77785b961788370373648 100644
(file)
--- a/
plomtask/db.py
+++ b/
plomtask/db.py
@@
-275,6
+275,8
@@
class BaseModel(Generic[BaseModelId]):
def as_dict(self) -> dict[str, object]:
"""Return self as (json.dumps-coompatible) dict."""
d: dict[str, object] = {'id': self.id_}
def as_dict(self) -> dict[str, object]:
"""Return self as (json.dumps-coompatible) dict."""
d: dict[str, object] = {'id': self.id_}
+ if len(self.to_save_versioned) > 0:
+ d['_versioned'] = {}
for k in self.to_save:
attr = getattr(self, k)
if hasattr(attr, 'as_dict'):
for k in self.to_save:
attr = getattr(self, k)
if hasattr(attr, 'as_dict'):
@@
-282,7
+284,8
@@
class BaseModel(Generic[BaseModelId]):
d[k] = attr
for k in self.to_save_versioned:
attr = getattr(self, k)
d[k] = attr
for k in self.to_save_versioned:
attr = getattr(self, k)
- d[k] = attr.as_dict
+ assert isinstance(d['_versioned'], dict)
+ d['_versioned'][k] = attr.history
for r in self.to_save_relations:
attr_name = r[2]
d[attr_name] = [x.as_dict for x in getattr(self, attr_name)]
for r in self.to_save_relations:
attr_name = r[2]
d[attr_name] = [x.as_dict for x in getattr(self, attr_name)]
@@
-312,7
+315,13
@@
class BaseModel(Generic[BaseModelId]):
@classmethod
def empty_cache(cls) -> None:
@classmethod
def empty_cache(cls) -> None:
- """Empty class's cache."""
+ """Empty class's cache, and disappear all former inhabitants."""
+ # pylint: disable=protected-access
+ # (cause we remain within the class)
+ if hasattr(cls, 'cache_'):
+ to_disappear = list(cls.cache_.values())
+ for item in to_disappear:
+ item._disappear()
cls.cache_ = {}
@classmethod
cls.cache_ = {}
@classmethod
@@
-335,7
+344,7
@@
class BaseModel(Generic[BaseModelId]):
return obj
return None
return obj
return None
- def
_
cache(self) -> None:
+ def cache(self) -> None:
"""Update object in class's cache.
Also calls ._disappear if cache holds older reference to object of same
"""Update object in class's cache.
Also calls ._disappear if cache holds older reference to object of same
@@
-366,9
+375,15
@@
class BaseModel(Generic[BaseModelId]):
# pylint: disable=unused-argument
db_conn: DatabaseConnection,
row: Row | list[Any]) -> BaseModelInstance:
# pylint: disable=unused-argument
db_conn: DatabaseConnection,
row: Row | list[Any]) -> BaseModelInstance:
- """Make from DB row, update DB cache with it."""
+ """Make from DB row
(sans relations)
, update DB cache with it."""
obj = cls(*row)
obj = cls(*row)
- obj._cache()
+ assert obj.id_ is not None
+ for attr_name in cls.to_save_versioned:
+ attr = getattr(obj, attr_name)
+ table_name = attr.table_name
+ for row_ in db_conn.row_where(table_name, 'parent', obj.id_):
+ attr.history_from_row(row_)
+ obj.cache()
return obj
@classmethod
return obj
@classmethod
@@
-482,7
+497,7
@@
class BaseModel(Generic[BaseModelId]):
values)
if not isinstance(self.id_, str):
self.id_ = cursor.lastrowid # type: ignore[assignment]
values)
if not isinstance(self.id_, str):
self.id_ = cursor.lastrowid # type: ignore[assignment]
- self.
_
cache()
+ self.cache()
for attr_name in self.to_save_versioned:
getattr(self, attr_name).save(db_conn)
for table, column, attr_name, key_index in self.to_save_relations:
for attr_name in self.to_save_versioned:
getattr(self, attr_name).save(db_conn)
for table, column, attr_name, key_index in self.to_save_relations: