2021-09-08 22:01:10 +00:00
|
|
|
import logging
|
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
from app.classes.models.users import Users, users_helper
|
2021-09-08 22:01:10 +00:00
|
|
|
from app.classes.shared.helpers import helper
|
|
|
|
from app.classes.shared.console import console
|
|
|
|
|
2022-01-15 00:23:50 +00:00
|
|
|
# To disable warning about unused import ; Users is imported from here in other places
|
2022-03-08 04:40:44 +00:00
|
|
|
# pylint: disable=self-assigning-variable
|
2022-01-15 00:23:50 +00:00
|
|
|
Users = Users
|
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
try:
|
2022-01-26 01:45:30 +00:00
|
|
|
# pylint: disable=unused-import
|
|
|
|
from peewee import SqliteDatabase, fn
|
2021-09-08 22:01:10 +00:00
|
|
|
from playhouse.shortcuts import model_to_dict
|
|
|
|
|
2022-01-26 01:45:30 +00:00
|
|
|
except ModuleNotFoundError as err:
|
2022-03-08 04:40:44 +00:00
|
|
|
helper.auto_installer_fix(err)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
2022-03-08 04:40:44 +00:00
|
|
|
logger = logging.getLogger(__name__)
|
|
|
|
peewee_logger = logging.getLogger('peewee')
|
|
|
|
peewee_logger.setLevel(logging.INFO)
|
|
|
|
database = SqliteDatabase(helper.db_path, pragmas = {
|
2021-09-08 22:01:10 +00:00
|
|
|
'journal_mode': 'wal',
|
|
|
|
'cache_size': -1024 * 10})
|
|
|
|
|
|
|
|
class db_builder:
|
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def default_settings():
|
|
|
|
logger.info("Fresh Install Detected - Creating Default Settings")
|
|
|
|
console.info("Fresh Install Detected - Creating Default Settings")
|
|
|
|
default_data = helper.find_default_password()
|
|
|
|
|
|
|
|
username = default_data.get("username", 'admin')
|
|
|
|
password = default_data.get("password", 'crafty')
|
2022-01-26 01:45:30 +00:00
|
|
|
|
|
|
|
users_helper.add_user(username=username, password=password, email="default@example.com", superuser=True)
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
@staticmethod
|
|
|
|
def is_fresh_install():
|
|
|
|
try:
|
|
|
|
user = 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
|
|
|
|
|
|
|
|
class db_shortcuts:
|
|
|
|
|
|
|
|
#************************************************************************************************
|
|
|
|
# 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:
|
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-01-26 01:45:30 +00:00
|
|
|
|
2021-09-08 22:01:10 +00:00
|
|
|
|
|
|
|
#************************************************************************************************
|
2022-01-26 01:45:30 +00:00
|
|
|
# Static Accessors
|
2021-09-08 22:01:10 +00:00
|
|
|
#************************************************************************************************
|
|
|
|
installer = db_builder()
|
|
|
|
db_helper = db_shortcuts()
|