crafty-4/app/classes/shared/main_models.py

65 lines
1.9 KiB
Python
Raw Normal View History

2021-09-08 22:01:10 +00:00
import logging
from playhouse.shortcuts import model_to_dict
2021-09-08 22:01:10 +00:00
from app.classes.shared.helpers import Helpers # pylint: disable=unused-import
from app.classes.shared.console import Console
2021-09-08 22:01:10 +00:00
2022-03-08 04:40:44 +00:00
logger = logging.getLogger(__name__)
2021-09-08 22:01:10 +00:00
2022-04-11 10:08:36 +00:00
class DatabaseBuilder:
def __init__(self, database, helper, users_helper):
self.database = database
self.helper = helper
self.users_helper = users_helper
def default_settings(self):
2021-09-08 22:01:10 +00:00
logger.info("Fresh Install Detected - Creating Default Settings")
Console.info("Fresh Install Detected - Creating Default Settings")
default_data = self.helper.find_default_password()
2021-09-08 22:01:10 +00:00
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,
)
2021-09-08 22:01:10 +00:00
def is_fresh_install(self):
2021-09-08 22:01:10 +00:00
try:
user = self.users_helper.get_by_id(1)
if user:
return False
2021-09-08 22:01:10 +00:00
except:
return True
class DatabaseShortcuts:
# **********************************************************************************
2021-09-08 22:01:10 +00:00
# Generic Databse Methods
# **********************************************************************************
2021-09-08 22:01:10 +00:00
@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}")
2021-09-08 22:01:10 +00:00
return rows
@staticmethod
def return_db_rows(model):
data = [model_to_dict(row) for row in model]
return data
2022-05-26 21:19:59 +00:00
@staticmethod
def get_data_obj(obj):
return model_to_dict(obj)