mirror of
https://github.com/hay-kot/mealie.git
synced 2025-08-22 06:23:34 -07:00
finale mongo port
This commit is contained in:
parent
7285c9ce06
commit
db294e888f
6 changed files with 55 additions and 79 deletions
4
docs/docs/img/app_diagram.drawio.svg
Normal file
4
docs/docs/img/app_diagram.drawio.svg
Normal file
|
@ -0,0 +1,4 @@
|
|||
<svg host="65bd71144e" xmlns="http://www.w3.org/2000/svg" xmlns:xlink="http://www.w3.org/1999/xlink" version="1.1" width="1px" height="1px" viewBox="-0.5 -0.5 1 1" content="<mxfile host="43d83f4e-bd8f-4433-8ef8-0a7ab65a65ed" modified="2021-01-12T03:47:32.314Z" agent="5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Code/1.52.1 Chrome/83.0.4103.122 Electron/9.3.5 Safari/537.36" version="13.10.0" etag="46kvhJe_ueXgXasrlNqu" type="embed"><diagram id="3j9sfWdaUOHFNAte1u_k" name="Page-1">dZHBDsIgDIafhjuDmOh5Tr142sEzGXWQsHVhmKFPLwvDSaYnytf/p7QlvOz82YpBXVGCIYxKT/iRMFZQWoRjJs9IDpRF0FotF9EKav2C5FzoQ0sYM6FDNE4POWyw76FxGRPW4pTL7mjyqoNoYQPqRpgtvWnpVKT7HV35BXSrUuXQccx0IokXMCohcfpCvCK8tIguRp0vwczDS3OJvtOf7OdjFnr3wxCC9e1wyTbEqzc=</diagram></mxfile>">
|
||||
<defs/>
|
||||
<g/>
|
||||
</svg>
|
After Width: | Height: | Size: 901 B |
|
@ -46,7 +46,10 @@ class BaseDocument:
|
|||
|
||||
def get_all(self, limit: int = None, order_by: str = "dateAdded"):
|
||||
if USE_MONGO:
|
||||
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)
|
||||
|
|
|
@ -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
|
||||
|
|
0
mealie/services/backups/export.py
Normal file
0
mealie/services/backups/export.py
Normal file
0
mealie/services/backups/import.py
Normal file
0
mealie/services/backups/import.py
Normal file
|
@ -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,16 +127,6 @@ 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"
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue