mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 14:33:33 -07:00
rewrite database model mixins
This commit is contained in:
parent
db36be0cd3
commit
2afc32bc4c
15 changed files with 410 additions and 145 deletions
File diff suppressed because one or more lines are too long
Before Width: | Height: | Size: 901 B After Width: | Height: | Size: 67 KiB |
File diff suppressed because one or more lines are too long
Binary file not shown.
|
@ -17,10 +17,18 @@ Recipe Actions
|
||||||
- [ ] Query by dateAdded
|
- [ ] Query by dateAdded
|
||||||
|
|
||||||
Progress:
|
Progress:
|
||||||
- [x] Recipes
|
- [ ] Recipes
|
||||||
- [ ] MealPlans
|
- [ ] MealPlans
|
||||||
- [ ] Site Settings
|
- [x] Site Settings
|
||||||
- [ ] Themes
|
- [x] Create
|
||||||
|
- [x] Read
|
||||||
|
- [x] Update
|
||||||
|
- [x] Delete
|
||||||
|
- [x] Themes
|
||||||
|
- [x] Create
|
||||||
|
- [x] Read
|
||||||
|
- [x] Update
|
||||||
|
- [x] Delete
|
||||||
|
|
||||||
"""
|
"""
|
||||||
|
|
||||||
|
|
|
@ -1,7 +1,9 @@
|
||||||
import json
|
import json
|
||||||
|
from typing import Union
|
||||||
|
|
||||||
import mongoengine
|
import mongoengine
|
||||||
from settings import USE_MONGO, USE_SQL
|
from settings import USE_MONGO, USE_SQL
|
||||||
|
from sqlalchemy.orm.session import Session
|
||||||
|
|
||||||
from db.sql.db_session import create_session
|
from db.sql.db_session import create_session
|
||||||
from db.sql.model_base import SqlAlchemyBase
|
from db.sql.model_base import SqlAlchemyBase
|
||||||
|
@ -13,6 +15,7 @@ class BaseDocument:
|
||||||
self.store: str
|
self.store: str
|
||||||
self.document: mongoengine.Document
|
self.document: mongoengine.Document
|
||||||
self.sql_model: SqlAlchemyBase
|
self.sql_model: SqlAlchemyBase
|
||||||
|
self.create_session = create_session
|
||||||
|
|
||||||
@staticmethod # TODO: Probably Put a version in each class to speed up reads?
|
@staticmethod # TODO: Probably Put a version in each class to speed up reads?
|
||||||
def _unpack_mongo(document) -> dict:
|
def _unpack_mongo(document) -> dict:
|
||||||
|
@ -71,6 +74,20 @@ class BaseDocument:
|
||||||
session.close()
|
session.close()
|
||||||
return list
|
return list
|
||||||
|
|
||||||
|
def _query_one(
|
||||||
|
self, match_value: str, match_key: str = None
|
||||||
|
) -> Union[Session, SqlAlchemyBase]:
|
||||||
|
session = self.create_session()
|
||||||
|
|
||||||
|
if match_key == None:
|
||||||
|
match_key = self.primary_key
|
||||||
|
|
||||||
|
result = (
|
||||||
|
session.query(self.sql_model).filter_by(**{match_key: match_value}).one()
|
||||||
|
)
|
||||||
|
|
||||||
|
return session, result
|
||||||
|
|
||||||
def get(
|
def get(
|
||||||
self, match_value: str, match_key: str = None, limit=1
|
self, match_value: str, match_key: str = None, limit=1
|
||||||
) -> dict or list[dict]:
|
) -> dict or list[dict]:
|
||||||
|
@ -94,14 +111,16 @@ class BaseDocument:
|
||||||
db_entry = BaseDocument._unpack_mongo(document)
|
db_entry = BaseDocument._unpack_mongo(document)
|
||||||
|
|
||||||
elif USE_SQL:
|
elif USE_SQL:
|
||||||
session = create_session()
|
session = self.create_session()
|
||||||
result = (
|
result = (
|
||||||
session.query(self.sql_model)
|
session.query(self.sql_model)
|
||||||
.filter_by(**{match_key: match_value})
|
.filter_by(**{match_key: match_value})
|
||||||
.one()
|
.one()
|
||||||
)
|
)
|
||||||
|
|
||||||
db_entry = result.dict()
|
db_entry = result.dict()
|
||||||
|
session.close()
|
||||||
|
|
||||||
|
return db_entry
|
||||||
|
|
||||||
else:
|
else:
|
||||||
raise Exception("No database type established")
|
raise Exception("No database type established")
|
||||||
|
@ -121,7 +140,9 @@ class BaseDocument:
|
||||||
|
|
||||||
def delete(self, primary_key_value) -> dict:
|
def delete(self, primary_key_value) -> dict:
|
||||||
if USE_MONGO:
|
if USE_MONGO:
|
||||||
document = self.document.objects.get(**{str(self.primary_key): primary_key_value})
|
document = self.document.objects.get(
|
||||||
|
**{str(self.primary_key): primary_key_value}
|
||||||
|
)
|
||||||
|
|
||||||
if document:
|
if document:
|
||||||
document.delete()
|
document.delete()
|
||||||
|
@ -138,3 +159,4 @@ class BaseDocument:
|
||||||
session.delete(result)
|
session.delete(result)
|
||||||
|
|
||||||
session.commit()
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
|
|
@ -3,14 +3,7 @@ from settings import USE_MONGO, USE_SQL
|
||||||
from db.db_base import BaseDocument
|
from db.db_base import BaseDocument
|
||||||
from db.mongo.recipe_models import RecipeDocument
|
from db.mongo.recipe_models import RecipeDocument
|
||||||
from db.sql.db_session import create_session
|
from db.sql.db_session import create_session
|
||||||
from db.sql.recipe_models import (
|
from db.sql.recipe_models import RecipeModel
|
||||||
ApiExtras,
|
|
||||||
Note,
|
|
||||||
RecipeIngredient,
|
|
||||||
RecipeInstruction,
|
|
||||||
RecipeModel,
|
|
||||||
Tag,
|
|
||||||
)
|
|
||||||
|
|
||||||
|
|
||||||
class _Recipes(BaseDocument):
|
class _Recipes(BaseDocument):
|
||||||
|
@ -18,68 +11,13 @@ class _Recipes(BaseDocument):
|
||||||
self.primary_key = "slug"
|
self.primary_key = "slug"
|
||||||
if USE_SQL:
|
if USE_SQL:
|
||||||
self.sql_model = RecipeModel
|
self.sql_model = RecipeModel
|
||||||
|
self.create_session = create_session
|
||||||
else:
|
else:
|
||||||
self.document = RecipeDocument
|
self.document = RecipeDocument
|
||||||
|
|
||||||
def save_new_sql(self, recipe_data: dict):
|
def save_new_sql(self, recipe_data: dict):
|
||||||
session = create_session()
|
session = self.create_session()
|
||||||
new_recipe: RecipeModel = self.sql_model()
|
new_recipe = self.sql_model(**recipe_data)
|
||||||
new_recipe.name = recipe_data.get("name")
|
|
||||||
new_recipe.description = recipe_data.get("description")
|
|
||||||
new_recipe.image = recipe_data.get("image")
|
|
||||||
new_recipe.totalTime = recipe_data.get("totalTime")
|
|
||||||
new_recipe.slug = recipe_data.get("slug")
|
|
||||||
new_recipe.rating = recipe_data.get("rating")
|
|
||||||
new_recipe.orgURL = recipe_data.get("orgURL")
|
|
||||||
new_recipe.dateAdded = recipe_data.get("dateAdded")
|
|
||||||
new_recipe.recipeYield = recipe_data.get("recipeYield")
|
|
||||||
|
|
||||||
for ingredient in recipe_data.get("recipeIngredient"):
|
|
||||||
new_ingredient = RecipeIngredient()
|
|
||||||
new_ingredient.ingredient = ingredient
|
|
||||||
new_recipe.recipeIngredient.append(new_ingredient)
|
|
||||||
|
|
||||||
for step in recipe_data.get("recipeInstructions"):
|
|
||||||
new_step = RecipeInstruction()
|
|
||||||
new_step.type = "Step"
|
|
||||||
new_step.text = step.get("text")
|
|
||||||
new_recipe.recipeInstructions.append(new_step)
|
|
||||||
|
|
||||||
try:
|
|
||||||
for tag in recipe_data.get("tags"):
|
|
||||||
new_tag = Tag()
|
|
||||||
new_tag.name = tag
|
|
||||||
new_recipe.tags.append(new_tag)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
|
||||||
for category in recipe_data.get("category"):
|
|
||||||
new_category = Tag()
|
|
||||||
new_category.name = category
|
|
||||||
new_recipe.categories.append(new_category)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
|
||||||
new_recipe.notes = recipe_data.get("name")
|
|
||||||
for note in recipe_data.get("notes"):
|
|
||||||
new_note = Note()
|
|
||||||
new_note.title = note.get("title")
|
|
||||||
new_note.text = note.get("text")
|
|
||||||
new_recipe.notes.append(note)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
try:
|
|
||||||
for key, value in recipe_data.get("extras").items():
|
|
||||||
new_extra = ApiExtras()
|
|
||||||
new_extra.key = key
|
|
||||||
new_extra.key = value
|
|
||||||
new_recipe.extras.append(new_extra)
|
|
||||||
except:
|
|
||||||
pass
|
|
||||||
|
|
||||||
session.add(new_recipe)
|
session.add(new_recipe)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
|
@ -111,7 +49,14 @@ class _Recipes(BaseDocument):
|
||||||
|
|
||||||
return new_data.get("slug")
|
return new_data.get("slug")
|
||||||
elif USE_SQL:
|
elif USE_SQL:
|
||||||
pass
|
session, recipe = self._query_one(match_value=slug)
|
||||||
|
recipe.update(**new_data)
|
||||||
|
recipe_dict = recipe.dict()
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
session.close()
|
||||||
|
|
||||||
|
return recipe_dict
|
||||||
|
|
||||||
def update_image(self, slug: str, extension: str) -> None:
|
def update_image(self, slug: str, extension: str) -> None:
|
||||||
if USE_MONGO:
|
if USE_MONGO:
|
||||||
|
|
|
@ -1,9 +1,10 @@
|
||||||
from db.sql.settings_models import SiteSettingsModel
|
|
||||||
from settings import USE_MONGO, USE_SQL
|
from settings import USE_MONGO, USE_SQL
|
||||||
|
|
||||||
from db.db_base import BaseDocument
|
from db.db_base import BaseDocument
|
||||||
from db.db_setup import USE_MONGO, USE_SQL
|
from db.db_setup import USE_MONGO, USE_SQL
|
||||||
from db.mongo.settings_models import SiteSettingsDocument, WebhooksDocument
|
from db.mongo.settings_models import SiteSettingsDocument, WebhooksDocument
|
||||||
|
from db.sql.db_session import create_session
|
||||||
|
from db.sql.settings_models import SiteSettingsModel
|
||||||
|
|
||||||
|
|
||||||
class _Settings(BaseDocument):
|
class _Settings(BaseDocument):
|
||||||
|
@ -24,7 +25,13 @@ class _Settings(BaseDocument):
|
||||||
return new_doc.save()
|
return new_doc.save()
|
||||||
|
|
||||||
elif USE_SQL:
|
elif USE_SQL:
|
||||||
pass
|
session = create_session()
|
||||||
|
new_settings = self.sql_model(main.get("name"), webhooks)
|
||||||
|
|
||||||
|
session.add(new_settings)
|
||||||
|
session.commit()
|
||||||
|
|
||||||
|
return new_settings.dict()
|
||||||
|
|
||||||
def update(self, name: str, new_data: dict) -> dict:
|
def update(self, name: str, new_data: dict) -> dict:
|
||||||
if USE_MONGO:
|
if USE_MONGO:
|
||||||
|
@ -33,4 +40,13 @@ class _Settings(BaseDocument):
|
||||||
document.update(set__webhooks=WebhooksDocument(**new_data["webhooks"]))
|
document.update(set__webhooks=WebhooksDocument(**new_data["webhooks"]))
|
||||||
document.save()
|
document.save()
|
||||||
elif USE_SQL:
|
elif USE_SQL:
|
||||||
pass
|
session = create_session()
|
||||||
|
updated_settings = (
|
||||||
|
session.query(self.sql_model)
|
||||||
|
.filter_by(**{self.primary_key: name})
|
||||||
|
.one()
|
||||||
|
)
|
||||||
|
updated_settings.update(**new_data)
|
||||||
|
|
||||||
|
session.commit()
|
||||||
|
return
|
||||||
|
|
|
@ -3,7 +3,7 @@ from settings import DATA_DIR, USE_MONGO, USE_SQL
|
||||||
from db.sql.db_session import globa_init as sql_global_init
|
from db.sql.db_session import globa_init as sql_global_init
|
||||||
|
|
||||||
if USE_SQL:
|
if USE_SQL:
|
||||||
db_file = DATA_DIR.joinpath("mealie.sqlite")
|
db_file = DATA_DIR.joinpath("db", "mealie.sqlite")
|
||||||
sql_global_init(db_file)
|
sql_global_init(db_file)
|
||||||
|
|
||||||
elif USE_MONGO:
|
elif USE_MONGO:
|
||||||
|
|
|
@ -4,7 +4,7 @@ from db.db_base import BaseDocument
|
||||||
from db.db_setup import USE_MONGO, USE_SQL
|
from db.db_setup import USE_MONGO, USE_SQL
|
||||||
from db.mongo.settings_models import SiteThemeDocument, ThemeColorsDocument
|
from db.mongo.settings_models import SiteThemeDocument, ThemeColorsDocument
|
||||||
from db.sql.db_session import create_session
|
from db.sql.db_session import create_session
|
||||||
from db.sql.settings_models import SiteThemeModel, ThemeColorsModel
|
from db.sql.settings_models import SiteThemeModel
|
||||||
|
|
||||||
|
|
||||||
class _Themes(BaseDocument):
|
class _Themes(BaseDocument):
|
||||||
|
@ -12,6 +12,7 @@ class _Themes(BaseDocument):
|
||||||
self.primary_key = "name"
|
self.primary_key = "name"
|
||||||
if USE_SQL:
|
if USE_SQL:
|
||||||
self.sql_model = SiteThemeModel
|
self.sql_model = SiteThemeModel
|
||||||
|
self.create_session = create_session
|
||||||
else:
|
else:
|
||||||
self.document = SiteThemeDocument
|
self.document = SiteThemeDocument
|
||||||
|
|
||||||
|
@ -23,26 +24,16 @@ class _Themes(BaseDocument):
|
||||||
|
|
||||||
document.save()
|
document.save()
|
||||||
elif USE_SQL:
|
elif USE_SQL:
|
||||||
session = create_session()
|
session = self.create_session()
|
||||||
|
new_theme = self.sql_model(**theme_data)
|
||||||
colors = ThemeColorsModel()
|
|
||||||
new_colors = theme_data.get("colors")
|
|
||||||
colors.primary = new_colors.get("primary")
|
|
||||||
colors.secondary = new_colors.get("secondary")
|
|
||||||
colors.accent = new_colors.get("accent")
|
|
||||||
colors.success = new_colors.get("success")
|
|
||||||
colors.info = new_colors.get("info")
|
|
||||||
colors.warning = new_colors.get("warning")
|
|
||||||
colors.error = new_colors.get("error")
|
|
||||||
|
|
||||||
new_theme = self.sql_model(name=theme_data.get("name"))
|
|
||||||
|
|
||||||
new_theme.colors = colors
|
|
||||||
|
|
||||||
session.add(new_theme)
|
session.add(new_theme)
|
||||||
session.commit()
|
session.commit()
|
||||||
|
|
||||||
return
|
return_data = new_theme.dict()
|
||||||
|
|
||||||
|
session.close()
|
||||||
|
return return_data
|
||||||
|
|
||||||
def update(self, data: dict) -> dict:
|
def update(self, data: dict) -> dict:
|
||||||
if USE_MONGO:
|
if USE_MONGO:
|
||||||
|
@ -56,4 +47,10 @@ class _Themes(BaseDocument):
|
||||||
raise Exception("No database entry was found to update")
|
raise Exception("No database entry was found to update")
|
||||||
|
|
||||||
elif USE_SQL:
|
elif USE_SQL:
|
||||||
pass
|
session, theme_model = self._query_one(
|
||||||
|
match_value=data["name"], match_key="name"
|
||||||
|
)
|
||||||
|
|
||||||
|
theme_model.update(**data)
|
||||||
|
session.commit()
|
||||||
|
session.close()
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
from db.sql.meal_models import *
|
from db.sql.meal_models import *
|
||||||
from db.sql.recipe_models import *
|
from db.sql.recipe_models import *
|
||||||
from db.sql.settings_models import *
|
from db.sql.settings_models import *
|
||||||
|
from db.sql.settings_models import SiteSettingsModel
|
||||||
|
|
|
@ -1,3 +1,4 @@
|
||||||
|
import datetime
|
||||||
from datetime import date
|
from datetime import date
|
||||||
from typing import List
|
from typing import List
|
||||||
|
|
||||||
|
@ -23,7 +24,7 @@ class Category(SqlAlchemyBase):
|
||||||
parent_id = sa.Column(sa.String, sa.ForeignKey("recipes.slug"))
|
parent_id = sa.Column(sa.String, sa.ForeignKey("recipes.slug"))
|
||||||
name = sa.Column(sa.String, index=True)
|
name = sa.Column(sa.String, index=True)
|
||||||
|
|
||||||
def dict(self):
|
def to_str(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
|
@ -33,7 +34,7 @@ class Tag(SqlAlchemyBase):
|
||||||
parent_id = sa.Column(sa.String, sa.ForeignKey("recipes.slug"))
|
parent_id = sa.Column(sa.String, sa.ForeignKey("recipes.slug"))
|
||||||
name = sa.Column(sa.String, index=True)
|
name = sa.Column(sa.String, index=True)
|
||||||
|
|
||||||
def dict(self):
|
def to_str(self):
|
||||||
return self.name
|
return self.name
|
||||||
|
|
||||||
|
|
||||||
|
@ -54,7 +55,7 @@ class RecipeIngredient(SqlAlchemyBase):
|
||||||
parent_id = sa.Column(sa.String, sa.ForeignKey("recipes.slug"))
|
parent_id = sa.Column(sa.String, sa.ForeignKey("recipes.slug"))
|
||||||
ingredient = sa.Column(sa.String)
|
ingredient = sa.Column(sa.String)
|
||||||
|
|
||||||
def dict(self):
|
def to_str(self):
|
||||||
return self.ingredient
|
return self.ingredient
|
||||||
|
|
||||||
|
|
||||||
|
@ -62,7 +63,7 @@ class RecipeInstruction(SqlAlchemyBase):
|
||||||
__tablename__ = "recipe_instructions"
|
__tablename__ = "recipe_instructions"
|
||||||
id = sa.Column(sa.Integer, primary_key=True)
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
parent_id = sa.Column(sa.String, sa.ForeignKey("recipes.slug"))
|
parent_id = sa.Column(sa.String, sa.ForeignKey("recipes.slug"))
|
||||||
type = sa.Column(sa.String)
|
type = sa.Column(sa.String, default="")
|
||||||
text = sa.Column(sa.String)
|
text = sa.Column(sa.String)
|
||||||
|
|
||||||
def dict(self):
|
def dict(self):
|
||||||
|
@ -77,19 +78,101 @@ class RecipeModel(SqlAlchemyBase):
|
||||||
description = sa.Column(sa.String)
|
description = sa.Column(sa.String)
|
||||||
image = sa.Column(sa.String)
|
image = sa.Column(sa.String)
|
||||||
recipeYield = sa.Column(sa.String)
|
recipeYield = sa.Column(sa.String)
|
||||||
recipeIngredient: List[RecipeIngredient] = orm.relation("RecipeIngredient")
|
recipeIngredient: List[RecipeIngredient] = orm.relationship(
|
||||||
recipeInstructions: List[RecipeInstruction] = orm.relation("RecipeInstruction")
|
"RecipeIngredient", cascade="all, delete"
|
||||||
|
)
|
||||||
|
recipeInstructions: List[RecipeInstruction] = orm.relationship(
|
||||||
|
"RecipeInstruction", cascade="all, delete"
|
||||||
|
)
|
||||||
totalTime = sa.Column(sa.String)
|
totalTime = sa.Column(sa.String)
|
||||||
|
|
||||||
# Mealie Specific
|
# Mealie Specific
|
||||||
slug = sa.Column(sa.String, primary_key=True, index=True, unique=True)
|
slug = sa.Column(sa.String, primary_key=True, index=True, unique=True)
|
||||||
categories: List[Category] = orm.relation("Category")
|
categories: List[Category] = orm.relationship("Category", cascade="all, delete")
|
||||||
tags: List[Tag] = orm.relation("Tag")
|
tags: List[Tag] = orm.relationship("Tag", cascade="all, delete")
|
||||||
dateAdded = sa.Column(sa.Date, default=date.today)
|
dateAdded = sa.Column(sa.Date, default=date.today)
|
||||||
notes: List[Note] = orm.relation("Note")
|
notes: List[Note] = orm.relationship("Note", cascade="all, delete")
|
||||||
rating = sa.Column(sa.Integer)
|
rating = sa.Column(sa.Integer)
|
||||||
orgURL = sa.Column(sa.String)
|
orgURL = sa.Column(sa.String)
|
||||||
extras: List[ApiExtras] = orm.relation("ApiExtras")
|
extras: List[ApiExtras] = orm.relationship("ApiExtras", cascade="all, delete")
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self,
|
||||||
|
name: str = None,
|
||||||
|
description: str = None,
|
||||||
|
image: str = None,
|
||||||
|
recipeYield: str = None,
|
||||||
|
recipeIngredient: List[str] = None,
|
||||||
|
recipeInstructions: List[dict] = None,
|
||||||
|
totalTime: str = None,
|
||||||
|
slug: str = None,
|
||||||
|
categories: List[str] = None,
|
||||||
|
tags: List[str] = None,
|
||||||
|
dateAdded: datetime.date = None,
|
||||||
|
notes: List[dict] = None,
|
||||||
|
rating: int = None,
|
||||||
|
orgURL: str = None,
|
||||||
|
extras: dict = None,
|
||||||
|
) -> None:
|
||||||
|
self.name = name
|
||||||
|
self.description = description
|
||||||
|
self.image = image
|
||||||
|
self.recipeYield = recipeYield
|
||||||
|
print(recipeIngredient)
|
||||||
|
self.recipeIngredient = [
|
||||||
|
RecipeIngredient(ingredient=ingr) for ingr in recipeIngredient
|
||||||
|
]
|
||||||
|
self.recipeInstructions = [
|
||||||
|
RecipeInstruction(text=instruc.get("text"), type=instruc.get("text"))
|
||||||
|
for instruc in recipeInstructions
|
||||||
|
]
|
||||||
|
self.totalTime = totalTime
|
||||||
|
|
||||||
|
# Mealie Specific
|
||||||
|
self.slug = slug
|
||||||
|
self.categories = [Category(cat) for cat in categories]
|
||||||
|
self.tags = [Tag(name=tag) for tag in tags]
|
||||||
|
self.dateAdded = dateAdded
|
||||||
|
self.notes = [Note(note) for note in notes]
|
||||||
|
self.rating = rating
|
||||||
|
self.orgURL = orgURL
|
||||||
|
self.extras = [ApiExtras(extra) for extra in extras]
|
||||||
|
|
||||||
|
def update(
|
||||||
|
self,
|
||||||
|
name: str = None,
|
||||||
|
description: str = None,
|
||||||
|
image: str = None,
|
||||||
|
recipeYield: str = None,
|
||||||
|
recipeIngredient: List[str] = None,
|
||||||
|
recipeInstructions: List[dict] = None,
|
||||||
|
totalTime: str = None,
|
||||||
|
slug: str = None,
|
||||||
|
categories: List[str] = None,
|
||||||
|
tags: List[str] = None,
|
||||||
|
dateAdded: datetime.date = None,
|
||||||
|
notes: List[dict] = None,
|
||||||
|
rating: int = None,
|
||||||
|
orgURL: str = None,
|
||||||
|
extras: dict = None,
|
||||||
|
):
|
||||||
|
self.__init__(
|
||||||
|
name,
|
||||||
|
description,
|
||||||
|
image,
|
||||||
|
recipeYield,
|
||||||
|
recipeIngredient,
|
||||||
|
recipeInstructions,
|
||||||
|
totalTime,
|
||||||
|
slug,
|
||||||
|
categories,
|
||||||
|
tags,
|
||||||
|
dateAdded,
|
||||||
|
notes,
|
||||||
|
rating,
|
||||||
|
orgURL,
|
||||||
|
extras,
|
||||||
|
)
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def _flatten_dict(list_of_dict: List[dict]):
|
def _flatten_dict(list_of_dict: List[dict]):
|
||||||
|
@ -105,13 +188,13 @@ class RecipeModel(SqlAlchemyBase):
|
||||||
"description": self.description,
|
"description": self.description,
|
||||||
"image": self.image,
|
"image": self.image,
|
||||||
"recipeYield": self.recipeYield,
|
"recipeYield": self.recipeYield,
|
||||||
"recipeIngredient": [x.dict() for x in self.recipeIngredient],
|
"recipeIngredient": [x.to_str() for x in self.recipeIngredient],
|
||||||
"recipeInstructions": [x.dict() for x in self.recipeInstructions],
|
"recipeInstructions": [x.dict() for x in self.recipeInstructions],
|
||||||
"totalTime": self.totalTime,
|
"totalTime": self.totalTime,
|
||||||
# Mealie
|
# Mealie
|
||||||
"slug": self.slug,
|
"slug": self.slug,
|
||||||
"categories": [x.dict() for x in self.categories],
|
"categories": [x.to_str() for x in self.categories],
|
||||||
"tags": [x.dict() for x in self.tags],
|
"tags": [x.to_str() for x in self.tags],
|
||||||
"dateAdded": self.dateAdded,
|
"dateAdded": self.dateAdded,
|
||||||
"notes": [x.dict() for x in self.notes],
|
"notes": [x.dict() for x in self.notes],
|
||||||
"rating": self.rating,
|
"rating": self.rating,
|
||||||
|
|
|
@ -1,15 +1,18 @@
|
||||||
import sqlalchemy as sa
|
import sqlalchemy as sa
|
||||||
import sqlalchemy.orm as orm
|
import sqlalchemy.orm as orm
|
||||||
from db.sql.model_base import SqlAlchemyBase
|
from db.sql.model_base import SqlAlchemyBase
|
||||||
from sqlalchemy.sql.expression import true
|
|
||||||
|
|
||||||
|
|
||||||
class WebhookURLModel(SqlAlchemyBase):
|
class WebhookURLModel(SqlAlchemyBase):
|
||||||
__tablename__ = "webhook_urls"
|
__tablename__ = "webhook_urls"
|
||||||
url = sa.Column(sa.String, primary_key=True, unique=True)
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
|
url = sa.Column(sa.String)
|
||||||
parent_id = sa.Column(sa.Integer, sa.ForeignKey("webhook_settings.id"))
|
parent_id = sa.Column(sa.Integer, sa.ForeignKey("webhook_settings.id"))
|
||||||
|
|
||||||
def dict(self):
|
def update(self, url) -> str:
|
||||||
|
self.url = url
|
||||||
|
|
||||||
|
def to_str(self):
|
||||||
return self.url
|
return self.url
|
||||||
|
|
||||||
|
|
||||||
|
@ -17,13 +20,38 @@ class WebHookModel(SqlAlchemyBase):
|
||||||
__tablename__ = "webhook_settings"
|
__tablename__ = "webhook_settings"
|
||||||
id = sa.Column(sa.Integer, primary_key=True)
|
id = sa.Column(sa.Integer, primary_key=True)
|
||||||
parent_id = sa.Column(sa.String, sa.ForeignKey("site_settings.name"))
|
parent_id = sa.Column(sa.String, sa.ForeignKey("site_settings.name"))
|
||||||
webhookURLs = orm.relation("WebhookURLModel")
|
webhookURLs = orm.relationship(
|
||||||
|
"WebhookURLModel", uselist=True, cascade="all, delete"
|
||||||
|
)
|
||||||
webhookTime = sa.Column(sa.String, default="00:00")
|
webhookTime = sa.Column(sa.String, default="00:00")
|
||||||
enabled = sa.Column(sa.Boolean, default=False)
|
enabled = sa.Column(sa.Boolean, default=False)
|
||||||
|
|
||||||
|
def __init__(
|
||||||
|
self, webhookURLs: list, webhookTime: str, enabled: bool = False
|
||||||
|
) -> None:
|
||||||
|
self.webhookURLs = [WebhookURLModel(x) for x in webhookURLs]
|
||||||
|
self.webhookTime = webhookTime
|
||||||
|
self.enabled = enabled
|
||||||
|
|
||||||
|
def update(self, webhookURLs: list, webhookTime: str, enabled: bool) -> None:
|
||||||
|
current_webhooks = 0
|
||||||
|
|
||||||
|
for webhook_url in self.webhookURLs:
|
||||||
|
try:
|
||||||
|
webhook_url.update(webhookURLs[current_webhooks])
|
||||||
|
current_webhooks += 1
|
||||||
|
except:
|
||||||
|
self.webhookURLs.remove(webhook_url)
|
||||||
|
|
||||||
|
for webhook_url in webhookURLs[current_webhooks:]:
|
||||||
|
self.webhookURLs.append(WebhookURLModel(webhook_url))
|
||||||
|
|
||||||
|
self.webhookTime = webhookTime
|
||||||
|
self.enabled = enabled
|
||||||
|
|
||||||
def dict(self):
|
def dict(self):
|
||||||
data = {
|
data = {
|
||||||
"webhookURLs": [url.dict for url in self.webhookURLS],
|
"webhookURLs": [url.to_str() for url in self.webhookURLs],
|
||||||
"webhookTime": self.webhookTime,
|
"webhookTime": self.webhookTime,
|
||||||
"enabled": self.enabled,
|
"enabled": self.enabled,
|
||||||
}
|
}
|
||||||
|
@ -33,7 +61,16 @@ class WebHookModel(SqlAlchemyBase):
|
||||||
class SiteSettingsModel(SqlAlchemyBase):
|
class SiteSettingsModel(SqlAlchemyBase):
|
||||||
__tablename__ = "site_settings"
|
__tablename__ = "site_settings"
|
||||||
name = sa.Column(sa.String, primary_key=True)
|
name = sa.Column(sa.String, primary_key=True)
|
||||||
webhooks = orm.relation("WebHookModel")
|
webhooks = orm.relationship("WebHookModel", uselist=False, cascade="all, delete")
|
||||||
|
|
||||||
|
def __init__(self, name: str = None, webhooks: dict = None) -> None:
|
||||||
|
self.name = name
|
||||||
|
self.webhooks = WebHookModel(**webhooks)
|
||||||
|
|
||||||
|
def update(self, name, webhooks: dict) -> dict:
|
||||||
|
self.name = name
|
||||||
|
self.webhooks.update(**webhooks)
|
||||||
|
return
|
||||||
|
|
||||||
def dict(self):
|
def dict(self):
|
||||||
data = {"name": self.name, "webhooks": self.webhooks.dict()}
|
data = {"name": self.name, "webhooks": self.webhooks.dict()}
|
||||||
|
@ -52,6 +89,24 @@ class ThemeColorsModel(SqlAlchemyBase):
|
||||||
warning = sa.Column(sa.String)
|
warning = sa.Column(sa.String)
|
||||||
error = sa.Column(sa.String)
|
error = sa.Column(sa.String)
|
||||||
|
|
||||||
|
def update(
|
||||||
|
self,
|
||||||
|
primary: str = None,
|
||||||
|
accent: str = None,
|
||||||
|
secondary: str = None,
|
||||||
|
success: str = None,
|
||||||
|
info: str = None,
|
||||||
|
warning: str = None,
|
||||||
|
error: str = None,
|
||||||
|
) -> None:
|
||||||
|
self.primary = primary
|
||||||
|
self.accent = accent
|
||||||
|
self.secondary = secondary
|
||||||
|
self.success = success
|
||||||
|
self.info = info
|
||||||
|
self.warning = warning
|
||||||
|
self.error = error
|
||||||
|
|
||||||
def dict(self):
|
def dict(self):
|
||||||
data = {
|
data = {
|
||||||
"primary": self.primary,
|
"primary": self.primary,
|
||||||
|
@ -70,6 +125,14 @@ class SiteThemeModel(SqlAlchemyBase):
|
||||||
name = sa.Column(sa.String, primary_key=True)
|
name = sa.Column(sa.String, primary_key=True)
|
||||||
colors = orm.relationship("ThemeColorsModel", uselist=False, cascade="all, delete")
|
colors = orm.relationship("ThemeColorsModel", uselist=False, cascade="all, delete")
|
||||||
|
|
||||||
|
def __init__(self, name: str, colors: dict) -> None:
|
||||||
|
self.name = name
|
||||||
|
self.colors = ThemeColorsModel(**colors)
|
||||||
|
|
||||||
|
def update(self, name, colors: dict) -> dict:
|
||||||
|
self.colors.update(**colors)
|
||||||
|
return self.dict()
|
||||||
|
|
||||||
def dict(self):
|
def dict(self):
|
||||||
data = {"name": self.name, "colors": self.colors.dict()}
|
data = {"name": self.name, "colors": self.colors.dict()}
|
||||||
return data
|
return data
|
||||||
|
|
|
@ -24,15 +24,15 @@ def test_webhooks():
|
||||||
@router.post("/api/site-settings/update/", tags=["Settings"])
|
@router.post("/api/site-settings/update/", tags=["Settings"])
|
||||||
def update_settings(data: SiteSettings):
|
def update_settings(data: SiteSettings):
|
||||||
""" Returns Site Settings """
|
""" Returns Site Settings """
|
||||||
|
data.update()
|
||||||
|
# try:
|
||||||
|
# data.update()
|
||||||
|
# except:
|
||||||
|
# raise HTTPException(
|
||||||
|
# status_code=400, detail=SnackResponse.error("Unable to Save Settings")
|
||||||
|
# )
|
||||||
|
|
||||||
try:
|
# scheduler.reschedule_webhooks()
|
||||||
data.update()
|
|
||||||
except:
|
|
||||||
raise HTTPException(
|
|
||||||
status_code=400, detail=SnackResponse.error("Unable to Save Settings")
|
|
||||||
)
|
|
||||||
|
|
||||||
scheduler.reschedule_webhooks()
|
|
||||||
return SnackResponse.success("Settings Updated")
|
return SnackResponse.success("Settings Updated")
|
||||||
|
|
||||||
|
|
||||||
|
@ -52,13 +52,13 @@ def get_single_theme(theme_name: str):
|
||||||
@router.post("/api/site-settings/themes/create/", tags=["Themes"])
|
@router.post("/api/site-settings/themes/create/", tags=["Themes"])
|
||||||
def create_theme(data: SiteTheme):
|
def create_theme(data: SiteTheme):
|
||||||
""" Creates a site color theme database entry """
|
""" Creates a site color theme database entry """
|
||||||
|
data.save_to_db()
|
||||||
try:
|
# try:
|
||||||
data.save_to_db()
|
# data.save_to_db()
|
||||||
except:
|
# except:
|
||||||
raise HTTPException(
|
# raise HTTPException(
|
||||||
status_code=400, detail=SnackResponse.error("Unable to Save Theme")
|
# status_code=400, detail=SnackResponse.error("Unable to Save Theme")
|
||||||
)
|
# )
|
||||||
|
|
||||||
return SnackResponse.success("Theme Saved")
|
return SnackResponse.success("Theme Saved")
|
||||||
|
|
||||||
|
@ -66,12 +66,13 @@ def create_theme(data: SiteTheme):
|
||||||
@router.post("/api/site-settings/themes/{theme_name}/update/", tags=["Themes"])
|
@router.post("/api/site-settings/themes/{theme_name}/update/", tags=["Themes"])
|
||||||
def update_theme(theme_name: str, data: SiteTheme):
|
def update_theme(theme_name: str, data: SiteTheme):
|
||||||
""" Update a theme database entry """
|
""" Update a theme database entry """
|
||||||
try:
|
data.update_document()
|
||||||
data.update_document()
|
|
||||||
except:
|
# try:
|
||||||
raise HTTPException(
|
# except:
|
||||||
status_code=400, detail=SnackResponse.error("Unable to Update Theme")
|
# raise HTTPException(
|
||||||
)
|
# status_code=400, detail=SnackResponse.error("Unable to Update Theme")
|
||||||
|
# )
|
||||||
|
|
||||||
return SnackResponse.success("Theme Updated")
|
return SnackResponse.success("Theme Updated")
|
||||||
|
|
||||||
|
|
|
@ -9,10 +9,6 @@ from slugify import slugify
|
||||||
|
|
||||||
from services.image_services import delete_image
|
from services.image_services import delete_image
|
||||||
|
|
||||||
CWD = Path(__file__).parent
|
|
||||||
ALL_RECIPES = CWD.parent.joinpath("data", "all_recipes.json")
|
|
||||||
IMG_DIR = CWD.parent.joinpath("data", "img")
|
|
||||||
|
|
||||||
|
|
||||||
class RecipeNote(BaseModel):
|
class RecipeNote(BaseModel):
|
||||||
title: str
|
title: str
|
||||||
|
@ -129,7 +125,7 @@ class Recipe(BaseModel):
|
||||||
def update(self, recipe_slug: str):
|
def update(self, recipe_slug: str):
|
||||||
""" Updates the recipe from the database by slug"""
|
""" Updates the recipe from the database by slug"""
|
||||||
updated_slug = db.recipes.update(recipe_slug, self.dict())
|
updated_slug = db.recipes.update(recipe_slug, self.dict())
|
||||||
return updated_slug
|
return updated_slug.get("slug")
|
||||||
|
|
||||||
@staticmethod
|
@staticmethod
|
||||||
def update_image(slug: str, extension: str):
|
def update_image(slug: str, extension: str):
|
||||||
|
|
|
@ -8,7 +8,7 @@ from utils.logger import logger
|
||||||
class Webhooks(BaseModel):
|
class Webhooks(BaseModel):
|
||||||
webhookTime: str = "00:00"
|
webhookTime: str = "00:00"
|
||||||
webhookURLs: Optional[List[str]] = []
|
webhookURLs: Optional[List[str]] = []
|
||||||
enabled: bool = "false"
|
enabled: bool = False
|
||||||
|
|
||||||
|
|
||||||
class SiteSettings(BaseModel):
|
class SiteSettings(BaseModel):
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue