diff --git a/docs/docs/img/app_diagram.drawio.svg b/docs/docs/img/app_diagram.drawio.svg new file mode 100644 index 000000000..fdb899f92 --- /dev/null +++ b/docs/docs/img/app_diagram.drawio.svg @@ -0,0 +1,4 @@ + + + + \ No newline at end of file diff --git a/mealie/db/db_base.py b/mealie/db/db_base.py index 165ae8623..4a907a2e3 100644 --- a/mealie/db/db_base.py +++ b/mealie/db/db_base.py @@ -46,7 +46,10 @@ class BaseDocument: def get_all(self, limit: int = None, order_by: str = "dateAdded"): if USE_MONGO: - documents = self.document.objects.order_by(str(order_by)).limit(limit) + if order_by: + documents = self.document.objects.order_by(str(order_by)).limit(limit) + else: + documents = self.document.objects().limit(limit) docs = [] for item in documents: doc = BaseDocument._unpack_mongo(item) diff --git a/mealie/db/db_themes.py b/mealie/db/db_themes.py index 1f009787f..c792fac9a 100644 --- a/mealie/db/db_themes.py +++ b/mealie/db/db_themes.py @@ -2,7 +2,7 @@ from settings import USE_MONGO, USE_TINYDB from db.db_base import BaseDocument from db.db_setup import USE_MONGO, USE_TINYDB, tiny_db -from db.mongo.settings_models import SiteThemeDocument +from db.mongo.settings_models import SiteThemeDocument, ThemeColorsDocument class _Themes(BaseDocument): @@ -12,8 +12,26 @@ class _Themes(BaseDocument): self.store = tiny_db.themes self.document = SiteThemeDocument - def update(self, key: str, new_data: dict) -> dict: + def save_new(self, theme_data: dict) -> None: if USE_MONGO: - pass + theme_data["colors"] = ThemeColorsDocument(**theme_data["colors"]) + + document = self.document(**theme_data) + + document.save() + elif USE_TINYDB: + pass + + def update(self, data: dict) -> dict: + if USE_MONGO: + colors = ThemeColorsDocument(**data["colors"]) + theme_document = SiteThemeDocument.objects.get(name=data.get("name")) + + if theme_document: + theme_document.update(set__colors=colors) + theme_document.save() + else: + raise Exception("No database entry was found to update") + elif USE_TINYDB: pass diff --git a/mealie/services/backups/export.py b/mealie/services/backups/export.py new file mode 100644 index 000000000..e69de29bb diff --git a/mealie/services/backups/import.py b/mealie/services/backups/import.py new file mode 100644 index 000000000..e69de29bb diff --git a/mealie/services/settings_services.py b/mealie/services/settings_services.py index e483a86ac..3c6afbdba 100644 --- a/mealie/services/settings_services.py +++ b/mealie/services/settings_services.py @@ -2,12 +2,6 @@ import json from typing import List, Optional from db.database import db -from db.mongo.settings_models import ( - SiteSettingsDocument, - SiteThemeDocument, - ThemeColorsDocument, - WebhooksDocument, -) from pydantic import BaseModel from startup import USE_TINYDB from utils.logger import logger @@ -35,15 +29,8 @@ class SiteSettings(BaseModel): } } - @staticmethod - def _unpack_doc(document: SiteSettingsDocument): - document = json.loads(document.to_json()) - del document["_id"] - document["webhhooks"] = Webhooks(**document["webhooks"]) - return SiteSettings(**document) - - @staticmethod - def get_site_settings(): + @classmethod + def get_site_settings(cls): try: document = db.settings.get("main") except: @@ -51,10 +38,9 @@ class SiteSettings(BaseModel): default_entry = SiteSettings(name="main", webhooks=webhooks) document = db.settings.save_new(default_entry.dict(), webhooks.dict()) - return SiteSettings(**document) + return cls(**document) def update(self): - db.settings.update(name="main", new_data=self.dict()) @@ -88,15 +74,13 @@ class SiteTheme(BaseModel): } } - @staticmethod - def get_by_name(theme_name): - if USE_TINYDB: - theme = Query() - document = tinydb.themes.search(theme.name == theme_name) - else: - document = SiteThemeDocument.objects.get(name=theme_name) + @classmethod + def get_by_name(cls, theme_name): + db_entry = db.themes.get(theme_name) + name = db_entry.get("name") + colors = Colors(**db_entry.get("colors")) - return SiteTheme._unpack_doc(document) + return cls(name=name, colors=colors) @staticmethod def _unpack_doc(document): @@ -111,48 +95,25 @@ class SiteTheme(BaseModel): @staticmethod def get_all(): - all_themes = [] - for theme in SiteThemeDocument.objects(): - all_themes.append(SiteTheme._unpack_doc(theme)) + all_themes = db.themes.get_all() + for index, theme in enumerate(all_themes): + name = theme.get("name") + colors = Colors(**theme.get("colors")) + + all_themes[index] = SiteTheme(name=name, colors=colors) return all_themes def save_to_db(self): - if USE_TINYDB: - self._save_to_tinydb() - else: - self._save_to_mongo() - - def _save_to_tinydb(self): - tinydb.themes.insert(self.dict()) - - def _save_to_mongo(self): - theme = self.dict() - theme["colors"] = ThemeColorsDocument(**theme["colors"]) - - theme_document = SiteThemeDocument(**theme) - - theme_document.save() + db.themes.save_new(self.dict()) def update_document(self): - theme = self.dict() - theme["colors"] = ThemeColorsDocument(**theme["colors"]) - - theme_document = SiteThemeDocument.objects.get(name=self.name) - - if theme_document: - theme_document.update(set__colors=theme["colors"]) - - theme_document.save() + db.themes.update(self.dict()) @staticmethod def delete_theme(theme_name: str) -> str: """ Removes the theme by name """ - document = SiteThemeDocument.objects.get(name=theme_name) - - if document: - document.delete() - return "Document Deleted" + db.themes.delete(theme_name) def default_theme_init(): @@ -166,24 +127,14 @@ def default_theme_init(): "error": "#EF5350", } - if USE_TINYDB: - theme = Query() - item = tinydb.themes.search(theme.name == "default") - print(item) - if item == []: - logger.info("Generating Default Theme") - colors = Colors(**default_colors) - default_theme = SiteTheme(name="default", colors=colors) - default_theme.save_to_db() - else: - try: - SiteTheme.get_by_name("default") - return "default theme exists" - except: - logger.info("Generating Default Theme") - colors = Colors(**default_colors) - default_theme = SiteTheme(name="default", colors=colors) - default_theme.save_to_db() + try: + SiteTheme.get_by_name("default") + return "default theme exists" + except: + logger.info("Generating Default Theme") + colors = Colors(**default_colors) + default_theme = SiteTheme(name="default", colors=colors) + default_theme.save_to_db() # default_theme_init()