round 2
This commit is contained in:
32
models/base.py
Normal file
32
models/base.py
Normal file
@@ -0,0 +1,32 @@
|
||||
from dataclasses import dataclass, field
|
||||
import uuid
|
||||
import time
|
||||
|
||||
@dataclass
|
||||
class BaseModel:
|
||||
id: str = field(init=False)
|
||||
created_at: float = field(init=False)
|
||||
updated_at: float = field(init=False)
|
||||
|
||||
def __post_init__(self):
|
||||
self.id = str(uuid.uuid4())
|
||||
now = time.time()
|
||||
self.created_at = now
|
||||
self.updated_at = now
|
||||
|
||||
def touch(self):
|
||||
self.updated_at = time.time()
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
'created_at': self.created_at,
|
||||
'updated_at': self.updated_at
|
||||
}
|
||||
|
||||
@classmethod
|
||||
def _apply_base_fields(cls, obj, d: dict):
|
||||
obj.id = d.get('id', obj.id)
|
||||
obj.created_at = d.get('created_at', obj.created_at)
|
||||
obj.updated_at = d.get('updated_at', obj.updated_at)
|
||||
return obj
|
||||
@@ -1,24 +1,18 @@
|
||||
from dataclasses import dataclass, field
|
||||
import uuid
|
||||
from models.base import BaseModel
|
||||
|
||||
@dataclass
|
||||
class Child:
|
||||
class Child(BaseModel):
|
||||
name: str
|
||||
age: int | None = None
|
||||
tasks: list[str] = field(default_factory=list)
|
||||
rewards: list[str] = field(default_factory=list)
|
||||
points: int = 0
|
||||
image_id: str | None = None
|
||||
id: str | None = None
|
||||
|
||||
def __post_init__(self):
|
||||
if self.id is None:
|
||||
self.id = str(uuid.uuid4())
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, d: dict):
|
||||
return cls(
|
||||
id=d.get('id'),
|
||||
obj = cls(
|
||||
name=d.get('name'),
|
||||
age=d.get('age'),
|
||||
tasks=d.get('tasks', []),
|
||||
@@ -26,14 +20,16 @@ class Child:
|
||||
points=d.get('points', 0),
|
||||
image_id=d.get('image_id')
|
||||
)
|
||||
return cls._apply_base_fields(obj, d)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
base = super().to_dict()
|
||||
base.update({
|
||||
'name': self.name,
|
||||
'age': self.age,
|
||||
'tasks': self.tasks,
|
||||
'rewards': self.rewards,
|
||||
'points': self.points,
|
||||
'image_id': self.image_id
|
||||
}
|
||||
})
|
||||
return base
|
||||
|
||||
@@ -1,30 +1,26 @@
|
||||
from dataclasses import dataclass
|
||||
import uuid
|
||||
from models.base import BaseModel
|
||||
|
||||
@dataclass
|
||||
class Image:
|
||||
class Image(BaseModel):
|
||||
type: int
|
||||
extension: str
|
||||
permanent: bool = False
|
||||
id: str | None = None
|
||||
|
||||
def __post_init__(self):
|
||||
if self.id is None:
|
||||
self.id = str(uuid.uuid4())
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, d: dict):
|
||||
return cls(
|
||||
id=d.get('id'),
|
||||
obj = cls(
|
||||
type=d.get('type'),
|
||||
permanent=d.get('permanent', False),
|
||||
extension=d.get('extension')
|
||||
extension=d.get('extension'),
|
||||
permanent=d.get('permanent', False)
|
||||
)
|
||||
return cls._apply_base_fields(obj, d)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
base = super().to_dict()
|
||||
base.update({
|
||||
'type': self.type,
|
||||
'permanent': self.permanent,
|
||||
'extension': self.extension
|
||||
}
|
||||
})
|
||||
return base
|
||||
|
||||
@@ -1,33 +1,29 @@
|
||||
from dataclasses import dataclass
|
||||
import uuid
|
||||
from models.base import BaseModel
|
||||
|
||||
@dataclass
|
||||
class Reward:
|
||||
class Reward(BaseModel):
|
||||
name: str
|
||||
description: str
|
||||
cost: int
|
||||
image_id: str | None = None
|
||||
id: str | None = None
|
||||
|
||||
def __post_init__(self):
|
||||
if self.id is None:
|
||||
self.id = str(uuid.uuid4())
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, d: dict):
|
||||
return cls(
|
||||
id=d.get('id'),
|
||||
obj = cls(
|
||||
name=d.get('name'),
|
||||
description=d.get('description'),
|
||||
cost=d.get('cost', 0),
|
||||
image_id=d.get('image_id')
|
||||
)
|
||||
return cls._apply_base_fields(obj, d)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
base = super().to_dict()
|
||||
base.update({
|
||||
'name': self.name,
|
||||
'description': self.description,
|
||||
'cost': self.cost,
|
||||
'image_id': self.image_id
|
||||
}
|
||||
})
|
||||
return base
|
||||
|
||||
@@ -1,33 +1,29 @@
|
||||
from dataclasses import dataclass
|
||||
import uuid
|
||||
from models.base import BaseModel
|
||||
|
||||
@dataclass
|
||||
class Task:
|
||||
class Task(BaseModel):
|
||||
name: str
|
||||
points: int
|
||||
is_good: bool
|
||||
image_id: str | None = None
|
||||
id: str | None = None
|
||||
|
||||
def __post_init__(self):
|
||||
if self.id is None:
|
||||
self.id = str(uuid.uuid4())
|
||||
|
||||
@classmethod
|
||||
def from_dict(cls, d: dict):
|
||||
return cls(
|
||||
id=d.get('id'),
|
||||
obj = cls(
|
||||
name=d.get('name'),
|
||||
points=d.get('points', 0),
|
||||
is_good=d.get('is_good', True),
|
||||
image_id=d.get('image_id')
|
||||
)
|
||||
return cls._apply_base_fields(obj, d)
|
||||
|
||||
def to_dict(self):
|
||||
return {
|
||||
'id': self.id,
|
||||
base = super().to_dict()
|
||||
base.update({
|
||||
'name': self.name,
|
||||
'points': self.points,
|
||||
'is_good': self.is_good,
|
||||
'image_id': self.image_id
|
||||
}
|
||||
})
|
||||
return base
|
||||
|
||||
Reference in New Issue
Block a user