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

77 lines
2.4 KiB
Python
Raw Normal View History

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:
# pylint: disable=unused-import
from peewee import SqliteDatabase, fn
2021-09-08 22:01:10 +00:00
from playhouse.shortcuts import model_to_dict
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')
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)
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:
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
2021-09-08 22:01:10 +00:00
#************************************************************************************************
# Static Accessors
2021-09-08 22:01:10 +00:00
#************************************************************************************************
installer = db_builder()
db_helper = db_shortcuts()