cleanup + tag models

This commit is contained in:
hay-kot 2021-03-08 21:02:44 -09:00
commit 26b6dc6d4f
13 changed files with 23 additions and 66 deletions

View file

@ -1,4 +1,4 @@
from schema.category import RecipeCategoryResponse
from schema.category import RecipeCategoryResponse, RecipeTagResponse
from schema.meal import MealPlanInDB
from schema.recipe import Recipe
from schema.settings import SiteSettings as SiteSettingsSchema
@ -44,7 +44,8 @@ class _Tags(BaseDocument):
def __init__(self) -> None:
self.primary_key = "slug"
self.sql_model = Tag
self.orm_mode = False
self.orm_mode = True
self.schema = RecipeTagResponse
class _Meals(BaseDocument):

View file

@ -116,23 +116,12 @@ class BaseDocument:
.all()
)
if self.orm_mode:
if limit == 1:
try:
return self.schema.from_orm(result[0])
except IndexError:
return None
return [self.schema.from_orm(x) for x in result]
db_entries = [x.dict() for x in result]
if limit == 1:
try:
return db_entries[0]
return self.schema.from_orm(result[0])
except IndexError:
return None
return db_entries
return [self.schema.from_orm(x) for x in result]
def create(self, session: Session, document: dict) -> dict:
"""Creates a new database entry for the given SQL Alchemy Model.

View file

@ -14,6 +14,3 @@ class ApiExtras(SqlAlchemyBase):
def __init__(self, key, value) -> None:
self.key_name = key
self.value = value
def dict(self):
return {self.key_name: self.value}

View file

@ -11,6 +11,3 @@ class RecipeIngredient(SqlAlchemyBase):
def update(self, ingredient):
self.ingredient = ingredient
def to_str(self):
return self.ingredient

View file

@ -9,8 +9,3 @@ class RecipeInstruction(SqlAlchemyBase):
position = sa.Column(sa.Integer)
type = sa.Column(sa.String, default="")
text = sa.Column(sa.String)
def dict(self):
data = {"@type": self.type, "text": self.text}
return data

View file

@ -13,5 +13,3 @@ class Note(SqlAlchemyBase):
self.title = title
self.text = text
def dict(self):
return {"title": self.title, "text": self.text}

View file

@ -29,12 +29,3 @@ class Nutrition(SqlAlchemyBase):
self.sodiumContent = sodiumContent
self.sugarContent = sugarContent
def dict(self) -> dict:
return {
"calories": self.calories,
"fatContent": self.fatContent,
"fiberContent": self.fiberContent,
"proteinContent": self.proteinContent,
"sodiumContent": self.sodiumContent,
"sugarContent": self.sugarContent,
}

View file

@ -27,20 +27,10 @@ class Tag(SqlAlchemyBase):
assert not name == ""
return name
def to_str(self):
return self.name
def __init__(self, name) -> None:
self.name = name.strip()
self.slug = slugify(self.name)
def dict(self):
return {
"id": self.id,
"slug": self.slug,
"name": self.name,
"recipes": [x.dict() for x in self.recipes],
}
@staticmethod
def create_if_not_exist(session, name: str = None):

View file

@ -15,13 +15,19 @@ class SiteSettings(SqlAlchemyBase, BaseMixins):
single_parent=True,
)
show_recent = sa.Column(sa.Boolean, default=True)
cards_per_section = sa.Column(sa.Integer)
def __init__(
self, session: Session = None, language="en", categories: list = [], show_recent=True
self,
session: Session = None,
language="en",
categories: list = [],
show_recent=True,
cards_per_section: int = 9,
) -> None:
session.commit()
self.language = language
self.cards_per_section = cards_per_section
self.show_recent = show_recent
self.categories = [
Category.create_if_not_exist(session=session, name=cat.get("name"))

View file

@ -16,10 +16,6 @@ class SiteThemeModel(SqlAlchemyBase):
self.colors.update(**colors)
return self.dict()
def dict(self):
data = {"name": self.name, "colors": self.colors.dict()}
return data
class ThemeColorsModel(SqlAlchemyBase):
__tablename__ = "theme_colors"
@ -50,15 +46,3 @@ class ThemeColorsModel(SqlAlchemyBase):
self.info = info
self.warning = warning
self.error = error
def dict(self):
data = {
"primary": self.primary,
"accent": self.accent,
"secondary": self.secondary,
"success": self.success,
"info": self.info,
"warning": self.warning,
"error": self.error,
}
return data

View file

@ -19,3 +19,11 @@ class RecipeCategoryResponse(CategoryBase):
class Config:
schema_extra = {"example": {"id": 1, "name": "dinner", "recipes": [{}]}}
class TagBase(CategoryBase):
pass
class RecipeTagResponse(TagBase):
pass

View file

@ -8,6 +8,7 @@ from schema.category import CategoryBase
class SiteSettings(CamelModel):
language: str = "en"
show_recent: bool = True
cards_per_section: int = 9
categories: Optional[list[CategoryBase]] = []
class Config:

View file

@ -5,7 +5,7 @@ class Colors(BaseModel):
primary: str = "#E58325"
accent: str = "#00457A"
secondary: str = "#973542"
success: str = "#5AB1BB"
success: str = "#4CAF50"
info: str = "#4990BA"
warning: str = "#FF4081"
error: str = "#EF5350"