finale mongo port

This commit is contained in:
Hayden 2021-01-11 19:36:13 -09:00
commit db294e888f
6 changed files with 55 additions and 79 deletions

View 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="&lt;mxfile host=&quot;43d83f4e-bd8f-4433-8ef8-0a7ab65a65ed&quot; modified=&quot;2021-01-12T03:47:32.314Z&quot; agent=&quot;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&quot; version=&quot;13.10.0&quot; etag=&quot;46kvhJe_ueXgXasrlNqu&quot; type=&quot;embed&quot;&gt;&lt;diagram id=&quot;3j9sfWdaUOHFNAte1u_k&quot; name=&quot;Page-1&quot;&gt;dZHBDsIgDIafhjuDmOh5Tr142sEzGXWQsHVhmKFPLwvDSaYnytf/p7QlvOz82YpBXVGCIYxKT/iRMFZQWoRjJs9IDpRF0FotF9EKav2C5FzoQ0sYM6FDNE4POWyw76FxGRPW4pTL7mjyqoNoYQPqRpgtvWnpVKT7HV35BXSrUuXQccx0IokXMCohcfpCvCK8tIguRp0vwczDS3OJvtOf7OdjFnr3wxCC9e1wyTbEqzc=&lt;/diagram&gt;&lt;/mxfile&gt;">
<defs/>
<g/>
</svg>

After

Width:  |  Height:  |  Size: 901 B

View file

@ -46,7 +46,10 @@ class BaseDocument:
def get_all(self, limit: int = None, order_by: str = "dateAdded"): def get_all(self, limit: int = None, order_by: str = "dateAdded"):
if USE_MONGO: if USE_MONGO:
if order_by:
documents = self.document.objects.order_by(str(order_by)).limit(limit) documents = self.document.objects.order_by(str(order_by)).limit(limit)
else:
documents = self.document.objects().limit(limit)
docs = [] docs = []
for item in documents: for item in documents:
doc = BaseDocument._unpack_mongo(item) doc = BaseDocument._unpack_mongo(item)

View file

@ -2,7 +2,7 @@ from settings import USE_MONGO, USE_TINYDB
from db.db_base import BaseDocument from db.db_base import BaseDocument
from db.db_setup import USE_MONGO, USE_TINYDB, tiny_db 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): class _Themes(BaseDocument):
@ -12,8 +12,26 @@ class _Themes(BaseDocument):
self.store = tiny_db.themes self.store = tiny_db.themes
self.document = SiteThemeDocument self.document = SiteThemeDocument
def update(self, key: str, new_data: dict) -> dict: def save_new(self, theme_data: dict) -> None:
if USE_MONGO: 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: elif USE_TINYDB:
pass pass

View file

View file

View file

@ -2,12 +2,6 @@ import json
from typing import List, Optional from typing import List, Optional
from db.database import db from db.database import db
from db.mongo.settings_models import (
SiteSettingsDocument,
SiteThemeDocument,
ThemeColorsDocument,
WebhooksDocument,
)
from pydantic import BaseModel from pydantic import BaseModel
from startup import USE_TINYDB from startup import USE_TINYDB
from utils.logger import logger from utils.logger import logger
@ -35,15 +29,8 @@ class SiteSettings(BaseModel):
} }
} }
@staticmethod @classmethod
def _unpack_doc(document: SiteSettingsDocument): def get_site_settings(cls):
document = json.loads(document.to_json())
del document["_id"]
document["webhhooks"] = Webhooks(**document["webhooks"])
return SiteSettings(**document)
@staticmethod
def get_site_settings():
try: try:
document = db.settings.get("main") document = db.settings.get("main")
except: except:
@ -51,10 +38,9 @@ class SiteSettings(BaseModel):
default_entry = SiteSettings(name="main", webhooks=webhooks) default_entry = SiteSettings(name="main", webhooks=webhooks)
document = db.settings.save_new(default_entry.dict(), webhooks.dict()) document = db.settings.save_new(default_entry.dict(), webhooks.dict())
return SiteSettings(**document) return cls(**document)
def update(self): def update(self):
db.settings.update(name="main", new_data=self.dict()) db.settings.update(name="main", new_data=self.dict())
@ -88,15 +74,13 @@ class SiteTheme(BaseModel):
} }
} }
@staticmethod @classmethod
def get_by_name(theme_name): def get_by_name(cls, theme_name):
if USE_TINYDB: db_entry = db.themes.get(theme_name)
theme = Query() name = db_entry.get("name")
document = tinydb.themes.search(theme.name == theme_name) colors = Colors(**db_entry.get("colors"))
else:
document = SiteThemeDocument.objects.get(name=theme_name)
return SiteTheme._unpack_doc(document) return cls(name=name, colors=colors)
@staticmethod @staticmethod
def _unpack_doc(document): def _unpack_doc(document):
@ -111,48 +95,25 @@ class SiteTheme(BaseModel):
@staticmethod @staticmethod
def get_all(): def get_all():
all_themes = [] all_themes = db.themes.get_all()
for theme in SiteThemeDocument.objects(): for index, theme in enumerate(all_themes):
all_themes.append(SiteTheme._unpack_doc(theme)) name = theme.get("name")
colors = Colors(**theme.get("colors"))
all_themes[index] = SiteTheme(name=name, colors=colors)
return all_themes return all_themes
def save_to_db(self): def save_to_db(self):
if USE_TINYDB: db.themes.save_new(self.dict())
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()
def update_document(self): def update_document(self):
theme = self.dict() db.themes.update(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()
@staticmethod @staticmethod
def delete_theme(theme_name: str) -> str: def delete_theme(theme_name: str) -> str:
""" Removes the theme by name """ """ Removes the theme by name """
document = SiteThemeDocument.objects.get(name=theme_name) db.themes.delete(theme_name)
if document:
document.delete()
return "Document Deleted"
def default_theme_init(): def default_theme_init():
@ -166,16 +127,6 @@ def default_theme_init():
"error": "#EF5350", "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: try:
SiteTheme.get_by_name("default") SiteTheme.get_by_name("default")
return "default theme exists" return "default theme exists"