Skip to content

Commit

Permalink
Initial work in initial windows app
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 28, 2024
1 parent 751c534 commit 6cf7acb
Show file tree
Hide file tree
Showing 4 changed files with 163 additions and 27 deletions.
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,7 @@ Requires [pyvan](https://github.com/ClimenteA/pyvan) to generate a Windows Stand

```
pip install pyvan
pyvan cacaoaccounting.py --no-console --icon-file cacao-accounting.ico
pyvan cacaoaccounting.py --no-console --icon-file assets/icon.ico
```

# Copyright
Expand Down
Binary file added assets/CacaoAccounting.png
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
File renamed without changes.
188 changes: 162 additions & 26 deletions cacaoaccounting.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,14 @@
import sys, os

if sys.executable.endswith("pythonw.exe"):
sys.stdout = open(os.devnull, "w")
sys.stderr = open(
os.path.join(
os.getenv("TEMP"), "stderr-{}".format(os.path.basename(sys.argv[0]))
),
"w",
)

# Copyright 2024 BMO Soluciones
#
# Licensed under the Apache License, Version 2.0 (the "License");
Expand Down Expand Up @@ -35,9 +46,10 @@
# ---------------------------------------------------------------------------------------
# Librerias de terceros
# ---------------------------------------------------------------------------------------
import customtkinter
from appdirs import AppDirs
from flaskwebgui import FlaskUI
from loguru import logger
from PIL import Image

# ---------------------------------------------------------------------------------------
# Recursos locales
Expand All @@ -54,8 +66,13 @@
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)


FILE_LIST = os.listdir(APP_DATA_DIR)
DB_FILES = []
for file in FILE_LIST:
if file.endswith(".db"):
DB_FILES.append(file)

# Ensure app directories exists
APP_CONFIG_DIR.mkdir(parents=True, exist_ok=True)
Expand Down Expand Up @@ -83,14 +100,7 @@ def get_secret_key():
return SECURE_KEY


DESKTOP_CONFIG: dict = {
"SECRET_KEY": get_secret_key(),
"SQLALCHEMY_DATABASE_URI": DATABASE_URI,
}
APPLICATION: "Flask" = create_app(DESKTOP_CONFIG)


def init_db():
def init_db(file, app):
"""
Database initializacion script.
Expand All @@ -100,32 +110,158 @@ def init_db():
"""
from cacao_accounting.database.helpers import inicia_base_de_datos

if Path.exists(DATABASE_FILE):
if Path.exists(file):
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)
copyfile(file, BACKUP_FILE)

else:
inicia_base_de_datos(APPLICATION)
inicia_base_de_datos(app=app)


class NewDabataseWin(customtkinter.CTkToplevel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.geometry("400x300")

self.label = customtkinter.CTkLabel(self, text="ToplevelWindow")
self.label.pack(padx=20, pady=20)


class RestoreDabataseWin(customtkinter.CTkToplevel):
def __init__(self, *args, **kwargs):
super().__init__(*args, **kwargs)
self.geometry("400x300")

self.label = customtkinter.CTkLabel(self, text="ToplevelWindow")
self.label.pack(padx=20, pady=20)


class App(customtkinter.CTk):
def __init__(self):
super().__init__()

customtkinter.set_default_color_theme("green")
customtkinter.set_appearance_mode("system")
self.title("Cacao Accounting Desktop Init")
self.geometry("550x450")
self.logo = customtkinter.CTkImage(
Image.open(os.path.join(os.getcwd(), "assets", "CacaoAccounting.png")),
size=(500, 150),
)

self.home = customtkinter.CTkFrame(
self, corner_radius=0, fg_color="transparent", bg_color="transparent"
)
self.home.grid(row=0, column=0, sticky="nsew")
self.home.grid_rowconfigure(4, weight=1)
self.home_logo = customtkinter.CTkLabel(self.home, text="", image=self.logo)
self.home_logo.grid(row=0, column=0, padx=20, pady=10)

self.new_db = customtkinter.CTkButton(
self.home,
corner_radius=20,
height=40,
border_spacing=10,
text="Crear nueva base de datos",
bg_color="transparent",
text_color=("gray10", "gray90"),
hover_color=("gray70", "gray30"),
anchor="w",
command=self.new_database,
)
self.new_db.grid(row=2, column=0, padx=10, pady=10, sticky="s")

self.restore_db = customtkinter.CTkButton(
self.home,
corner_radius=20,
height=40,
border_spacing=10,
text="Restaurar base de datos",
bg_color="transparent",
text_color=("gray10", "gray90"),
hover_color=("gray70", "gray30"),
anchor="w",
command=self.restore_database,
)
self.restore_db.grid(row=3, column=0, padx=10, pady=10, sticky="s")

self.select_db_title = customtkinter.CTkLabel(
self.home, text="Seleccionar base de datos existente:"
)
self.select_db_title.grid(row=4, column=0, padx=10, pady=10, sticky="s")

self.select_db = customtkinter.CTkOptionMenu(
self.home, values=DB_FILES, command=self.create_sqlite_url
)
self.select_db.grid(row=5, column=0, padx=10, pady=10, sticky="s")

self.init_server = customtkinter.CTkButton(
self.home,
corner_radius=20,
height=40,
border_spacing=10,
text="Iniciar Cacao Accounting",
bg_color="transparent",
text_color=("gray10", "gray90"),
hover_color=("gray70", "gray30"),
anchor="w",
command=self.run_wsgi_server,
)
self.init_server.grid(row=6, column=0, padx=10, pady=10, sticky="s")

self.app_server = FlaskUI(
app=create_app(
{
"SECRET_KEY": get_secret_key(),
"SQLALCHEMY_DATABASE_URI": self.create_sqlite_url(
self.select_db.get()
),
}
),
server="flask",
port=9871,
fullscreen=False,
profile_dir_prefix="cacao_accounting",
height=600,
width=1200,
)

self.toplevel_window = None

def create_sqlite_url(self, file_name):
self.DATABASE_FILE = Path(os.path.join(APP_DATA_DIR, file_name))
self.DATABASE_URI = "sqlite:///" + str(self.DATABASE_FILE)

return self.DATABASE_URI

def run_wsgi_server(self):
print(self.create_sqlite_url(self.select_db.get()))
self.deiconify()
self.app_server.run()

def new_database(self):
if self.toplevel_window is None or not self.toplevel_window.winfo_exists():
self.toplevel_window = NewDabataseWin(self)
self.toplevel_window.focus()
else:
self.toplevel_window.focus()

def restore_database(self):
if self.toplevel_window is None or not self.toplevel_window.winfo_exists():
self.toplevel_window = RestoreDabataseWin(self)
self.toplevel_window.focus()
else:
self.toplevel_window.focus()


def init_app():
init_db()
start_app = App()


if __name__ == "__main__":

FlaskUI(
app=APPLICATION,
server="flask",
port=9871,
fullscreen=False,
profile_dir_prefix="cacao_accounting",
height=600,
width=1200,
on_startup=init_app,
).run()
start_app.mainloop()

0 comments on commit 6cf7acb

Please sign in to comment.