Implement task API with input validation; implement error handling; refactoring
This commit is contained in:
parent
19d264a03b
commit
50aff05614
24 changed files with 612 additions and 131 deletions
|
|
@ -1,14 +1,15 @@
|
|||
from typing import Any, Iterable, Optional
|
||||
|
||||
from sqlalchemy import Column, Integer, inspect
|
||||
from sqlalchemy.orm import InstanceState, as_declarative
|
||||
|
||||
from tofu_api.common.database import Col, MetaData
|
||||
|
||||
__all__ = [
|
||||
'BaseModel',
|
||||
]
|
||||
|
||||
from typing import Any, Iterable, Optional, Union
|
||||
|
||||
from sqlalchemy import Column, Integer, inspect
|
||||
from sqlalchemy.orm import InstanceState, as_declarative
|
||||
from validataclass.dataclasses import ValidataclassMixin
|
||||
|
||||
from tofu_api.common.database import Col, MetaData
|
||||
|
||||
|
||||
@as_declarative(metadata=MetaData())
|
||||
class BaseModel:
|
||||
|
|
@ -60,3 +61,15 @@ class BaseModel:
|
|||
return {
|
||||
field: getattr(self, field) for field in included_fields
|
||||
}
|
||||
|
||||
def update_from(self, data: Union[dict, ValidataclassMixin]) -> None:
|
||||
"""
|
||||
Updates the object with data from either a dictionary or a validataclass object (requires the ValidataclassMixin).
|
||||
"""
|
||||
if isinstance(data, ValidataclassMixin):
|
||||
data = data.to_dict()
|
||||
|
||||
# TODO: Is it a good idea to just iterate over data and setattr? Or should we check __table__.columns?
|
||||
for key, value in data.items():
|
||||
if hasattr(self, key):
|
||||
setattr(self, key, value)
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue