crafty-4/app/classes/shared/main_models.py
Andrew b1ed9ba2bd Add API routes from 3.x
Enhance security for permissions on API requests
Fix bug where server permissions and crafty permissions were flipped upon making a new token
Fix bug where new secret key would be created every time Crafty was started.
Fix bug where DB locks will occur with concurrent writes to the DB.
2022-04-10 19:39:31 +00:00

88 lines
2.6 KiB
Python

import logging
from app.classes.models.users import Users, users_helper
from app.classes.shared.helpers import helper
from app.classes.shared.console import console
# To disable warning about unused import ; Users is imported from here in other places
# pylint: disable=self-assigning-variable
Users = Users
try:
# pylint: disable=unused-import
from peewee import fn
from playhouse.sqliteq import SqliteQueueDatabase
from playhouse.shortcuts import model_to_dict
except ModuleNotFoundError as err:
helper.auto_installer_fix(err)
logger = logging.getLogger(__name__)
peewee_logger = logging.getLogger("peewee")
peewee_logger.setLevel(logging.INFO)
database = SqliteQueueDatabase(
helper.db_path
# This is commented out after presenting issues when
# moving from SQLiteDatabase to SqliteQueueDatabase
# //TODO Enable tuning
# pragmas={"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,
)
@staticmethod
def is_fresh_install():
try:
user = users_helper.get_by_id(1)
if user:
return False
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}")
return rows
@staticmethod
def return_db_rows(model):
data = [model_to_dict(row) for row in model]
return data
# **********************************************************************************
# Static Accessors
# **********************************************************************************
installer = db_builder()
db_helper = db_shortcuts()