crafty-4/app/classes/shared/main_models.py
amcmanu3 2d9529ba0c Add cookie secret to DB
Allow users to reset cookie and api secret
Add Crafty row to table on fresh install
Change inserts to updates for crafty settings table.
2023-01-29 16:54:02 -05:00

68 lines
2.0 KiB
Python

import logging
from playhouse.shortcuts import model_to_dict
from app.classes.shared.helpers import Helpers # pylint: disable=unused-import
from app.classes.shared.console import Console
logger = logging.getLogger(__name__)
class DatabaseBuilder:
def __init__(self, database, helper, users_helper, management_helper):
self.database = database
self.helper = helper
self.management_helper = management_helper
self.users_helper = users_helper
def default_settings(self):
logger.info("Fresh Install Detected - Creating Default Settings")
Console.info("Fresh Install Detected - Creating Default Settings")
default_data = self.helper.find_default_password()
username = default_data.get("username", "admin")
password = default_data.get("password", "crafty")
self.users_helper.add_user(
username=username,
password=password,
email="default@example.com",
superuser=True,
manager=None,
)
self.management_helper.create_crafty_row()
def is_fresh_install(self):
try:
num_user = self.users_helper.get_user_total()
return num_user <= 0
except:
return True
class DatabaseShortcuts:
# **********************************************************************************
# Generic Databse Methods
# **********************************************************************************
@staticmethod
def return_rows(query):
rows = []
try:
if query.count() > 0:
for s in query:
rows.append(model_to_dict(s))
except Exception as e:
logger.warning(f"Database Error: {e}")
return rows
@staticmethod
def return_db_rows(model):
data = [model_to_dict(row) for row in model]
return data
@staticmethod
def get_data_obj(obj):
return model_to_dict(obj)