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

68 lines
2.0 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, management_helper):
self.database = database
self.helper = helper
self.management_helper = management_helper
self.users_helper = users_helper
def default_settings(self, password="crafty"):
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", password)
self.users_helper.add_user(
username=username,
password=password,
email="default@example.com",
superuser=True,
manager=None,
)
2021-09-08 22:01:10 +00:00
self.management_helper.create_crafty_row()
def is_fresh_install(self):
2021-09-08 22:01:10 +00:00
try:
2022-08-07 15:30:00 +00:00
num_user = self.users_helper.get_user_total()
return num_user <= 0
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)