st-ten-1/src/lib/db/models/recipes.py

50 lines
1.7 KiB
Python
Raw Normal View History

2022-09-07 15:24:40 +00:00
from uuid import uuid4 as uuid
from peewee import BooleanField, TextField
2022-06-01 16:37:19 +00:00
from playhouse.sqlite_ext import JSONField
from .base_model import BaseModel
2022-07-19 09:59:00 +00:00
from .steps import Steps
2022-06-01 16:37:19 +00:00
class Recipes(BaseModel):
2022-09-07 15:24:40 +00:00
name = TextField(primary_key=True, unique=True, null=False, default=lambda: uuid().hex)
2022-07-26 13:43:11 +00:00
client = TextField(null=True)
2022-07-19 09:59:00 +00:00
part_number = TextField(null=False)
2022-06-01 16:37:19 +00:00
spec = JSONField(null=False) # keys inside spec must not overlap withthe model
2022-07-26 13:43:11 +00:00
description = TextField(null=True)
2022-06-01 16:37:19 +00:00
archived = BooleanField(null=False, default=False)
2022-07-19 09:59:00 +00:00
def get_steps(self):
2022-09-26 15:43:59 +00:00
steps = []
2022-09-27 12:09:43 +00:00
for step_pk in self.spec.get("steps", []):
2022-09-26 15:43:59 +00:00
steps.append(Steps.get_by_id(step_pk))
return steps
2022-09-20 15:42:59 +00:00
def delete_associated_steps(self):
return Steps.delete().where(Steps._meta.primary_key << list(self.spec.get("available_steps", {}).values())).execute()
2022-09-20 15:42:59 +00:00
def get_steps_map(self):
steps = {}
for step_name, step_pk in self.spec.get("available_steps", {}).items():
steps[step_name] = Steps.get_by_id(step_pk)
return steps
2022-07-19 09:59:00 +00:00
@classmethod
def crud_delete(cls, deleted_rows):
# return cls.delete().where(self._meta.primary_key << deleted_rows).execute()
recipes = list(cls.select().where(cls._meta.primary_key << deleted_rows).execute())
for recipe in recipes:
recipe.delete_associated_steps()
recipe.delete_instance()
return len(recipes)
2022-10-25 13:38:18 +00:00
# @classmethod
# def delete(cls, *args, **kwargs):
# # OVERRIDE DELETION
# # so that deleting a user will only archive it
# return cls.update(archived=True)
2022-06-01 16:37:19 +00:00
class Meta:
table_name = "recipes"