Skip to content

Commit

Permalink
Include init_app scritp
Browse files Browse the repository at this point in the history
Signed-off-by: William José Moreno Reyes <williamjmorenor@gmail.com>
  • Loading branch information
williamjmorenor committed Mar 27, 2024
1 parent 2fa12db commit 751c534
Showing 1 changed file with 33 additions and 7 deletions.
40 changes: 33 additions & 7 deletions cacaoaccounting.py
Original file line number Diff line number Diff line change
Expand Up @@ -24,13 +24,12 @@
"w",
)


# Import go here

# ---------------------------------------------------------------------------------------
# Libreria estandar
# ---------------------------------------------------------------------------------------
from datetime import datetime
from pathlib import Path
from shutil import copyfile
from typing import TYPE_CHECKING

# ---------------------------------------------------------------------------------------
Expand All @@ -48,20 +47,29 @@
if TYPE_CHECKING:
from flask import Flask

APP_DIRS: AppDirs = AppDirs("Cacao Accounting Desktop", "BMO Soluciones")

APP_DIRS: AppDirs = AppDirs("Cacao Accounting Desktop", "BMO Soluciones")
APP_CONFIG_DIR = Path(os.path.join(APP_DIRS.user_config_dir))
APP_DATA_DIR = Path(os.path.join(APP_DIRS.user_data_dir))
APP_HOME_DIR = os.path.expanduser("~/Cacao Accounting")
APP_BACKUP_DIR = Path(os.path.join(APP_HOME_DIR, "Backups"))
SECURE_KEY_FILE = Path(os.path.join(APP_CONFIG_DIR, "secret.key"))
DATABASE_FILE = Path(os.path.join(APP_DATA_DIR), "cacao-accounting.db")
DATABASE_URI = "sqlite:///" + str(DATABASE_FILE)

# Ensure dir exists
# Ensure app directories exists
APP_CONFIG_DIR.mkdir(parents=True, exist_ok=True)
APP_DATA_DIR.mkdir(parents=True, exist_ok=True)
APP_BACKUP_DIR.mkdir(parents=True, exist_ok=True)


def get_secret_key():
"""
Populate the SECRET_KEY config.
Is SECURE_KEY_FILE exist will read the content of the file a return it,
if not will generate a ramond string a save the value for future use.
"""
if Path.exists(SECURE_KEY_FILE):
with open(SECURE_KEY_FILE) as f:
return f.readline()
Expand All @@ -83,14 +91,32 @@ def get_secret_key():


def init_db():
"""
Database initializacion script.
If DATABASE_FILE not exist will create a new databa file and pupulate it with
the inicial data.
"""
from cacao_accounting.database.helpers import inicia_base_de_datos

if Path.exists(DATABASE_FILE):
pass
TIME_STAMP = datetime.today().strftime("%Y.%m.%d")
BACKUP_FILE = Path(
os.path.join(
APP_BACKUP_DIR, str(TIME_STAMP) + "-cacao_accounting_backup.db"
)
)
copyfile(DATABASE_FILE, BACKUP_FILE)

else:
inicia_base_de_datos(APPLICATION)


def init_app():
init_db()


if __name__ == "__main__":

FlaskUI(
Expand All @@ -101,5 +127,5 @@ def init_db():
profile_dir_prefix="cacao_accounting",
height=600,
width=1200,
on_startup=init_db,
on_startup=init_app,
).run()

0 comments on commit 751c534

Please sign in to comment.