2021-09-08 22:01:10 +00:00
|
|
|
import logging
|
2022-04-11 05:23:55 +00:00
|
|
|
from playhouse.shortcuts import model_to_dict
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
from app.classes.shared.helpers import Helpers # pylint: disable=unused-import
|
2022-04-17 17:27:00 +00:00
|
|
|
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
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
class DatabaseBuilder:
|
2022-04-11 05:23:55 +00:00
|
|
|
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")
|
2022-04-17 17:27:00 +00:00
|
|
|
Console.info("Fresh Install Detected - Creating Default Settings")
|
2022-04-11 05:23:55 +00:00
|
|
|
default_data = self.helper.find_default_password()
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
username = default_data.get("username", "admin")
|
|
|
|
password = default_data.get("password", "crafty")
|
2022-01-26 01:45:30 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
self.users_helper.add_user(
|
2022-03-23 02:50:12 +00:00
|
|
|
username=username,
|
|
|
|
password=password,
|
|
|
|
email="default@example.com",
|
|
|
|
superuser=True,
|
|
|
|
)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-04-11 05:23:55 +00:00
|
|
|
def is_fresh_install(self):
|
2021-09-08 22:01:10 +00:00
|
|
|
try:
|
2022-04-11 05:23:55 +00:00
|
|
|
user = self.users_helper.get_by_id(1)
|
2022-01-26 01:45:30 +00:00
|
|
|
if user:
|
|
|
|
return False
|
2021-09-08 22:01:10 +00:00
|
|
|
except:
|
|
|
|
return True
|
|
|
|
|
2022-03-23 02:50:12 +00:00
|
|
|
|
2022-04-14 02:10:25 +00:00
|
|
|
class DatabaseShortcuts:
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
2021-09-08 22:01:10 +00:00
|
|
|
# Generic Databse Methods
|
2022-03-23 06:06:13 +00:00
|
|
|
# **********************************************************************************
|
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:
|
2022-01-26 01:45:30 +00:00
|
|
|
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)
|