Added custom extra fields to backend

No integration tests yet, TBD
This commit is contained in:
Donkie
2024-01-06 16:22:35 +01:00
parent 71c29d2467
commit 77ac9d6a5c
14 changed files with 556 additions and 25 deletions

View File

@@ -43,6 +43,7 @@ async def create(
lot_nr: Optional[str] = None,
comment: Optional[str] = None,
archived: bool = False,
extra: Optional[dict[str, str]] = None,
) -> models.Spool:
"""Add a new spool to the database. Leave weight empty to assume full spool."""
filament_item = await filament.get_by_id(db, filament_id)
@@ -71,6 +72,7 @@ async def create(
lot_nr=lot_nr,
comment=comment,
archived=archived,
extra=[models.SpoolField(key=k, value=v) for k, v in (extra or {}).items()],
)
db.add(spool)
await db.commit()
@@ -158,7 +160,7 @@ async def find(
stmt = stmt.order_by(field.desc())
rows = await db.execute(stmt)
result = list(rows.scalars().all())
result = list(rows.unique().scalars().all())
if total_count is None:
total_count = len(result)
@@ -182,6 +184,8 @@ async def update(
spool.used_weight = max(spool.filament.weight - v, 0)
elif isinstance(v, datetime):
setattr(spool, k, utc_timezone_naive(v))
elif k == "extra":
spool.extra = [models.SpoolField(key=k, value=v) for k, v in v.items()]
else:
setattr(spool, k, v)
await db.commit()