Refactoring

This commit is contained in:
Lucas Mathews
2024-06-08 10:36:27 +02:00
parent 4f5f54d8fa
commit a65142c7b0
11 changed files with 39 additions and 116 deletions

View File

@@ -73,7 +73,7 @@ def on_transaction_double_click(event):
try:
selected_transaction = transactions_table.item(transactions_table.selection())
transaction_id = selected_transaction['values'][-1]
command = f"python application\\transaction.py {transaction_id} \"{selected_transaction['values'][4]}\""
command = f"python transaction.py {transaction_id} \"{selected_transaction['values'][4]}\""
return_code = os.system(command)
if return_code != 0:
print(f"Error: The command failed with return code {return_code}")
@@ -82,7 +82,7 @@ def on_transaction_double_click(event):
def add_transaction():
"""Open the add transaction dialog."""
command = f"python application\\new_transaction.py {account_id}"
command = f"python new_transaction.py {account_id}"
return_code = os.system(command)
if return_code != 0:
print(f"Error: The command failed with return code {return_code}")
@@ -92,7 +92,7 @@ def edit_account_details():
global edit_window, otp_entry, description_entry, notes_entry
edit_window = customtkinter.CTkToplevel(root)
edit_window.title("Edit Account Details")
edit_window.iconbitmap("application/luxbank.ico")
edit_window.iconbitmap("luxbank.ico")
edit_window.geometry("300x350")
edit_window.attributes('-topmost', True)
@@ -155,8 +155,8 @@ def save_details():
def open_transaction_window():
"""Opens a new window for creating a new transaction."""
try:
session = json.load(open('application\\session_data.json', 'r'))
command = f"python application\\new_transaction.py {session['client_id']}"
session = json.load(open('session_data.json', 'r'))
command = f"python new_transaction.py {session['client_id']}"
return_code = os.system(command)
if return_code != 0:
print(f"Error: The command failed with return code {return_code}")
@@ -170,7 +170,7 @@ def open_transaction_window():
# Initialise the main window
root = customtkinter.CTk()
root.title(f"Transactions for: {account_description}")
root.iconbitmap("application/luxbank.ico")
root.iconbitmap("luxbank.ico")
root.geometry("800x450")
if CONFIG["preferences"]["dark_theme"] == "dark": # Check if dark mode is enabled

View File

@@ -4,4 +4,4 @@
import configparser
CONFIG = configparser.ConfigParser()
CONFIG.read("application/app.ini")
CONFIG.read("app.ini")

View File

@@ -38,7 +38,7 @@ def authenticate_client(client_id, client_hash):
def logout_client():
"""Logs out the current client."""
try:
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
response = requests.post(CONFIG["server"]["url"] + "/Client/Logout", cookies=session_data['session_cookie'])
return response
@@ -52,7 +52,7 @@ def logout_client():
def get_client(client_id):
"""Retrieves the client details for the given client_id."""
try:
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
response = requests.get(CONFIG["server"]["url"] + "/Client", cookies=session_data['session_cookie'], params={'client_id': client_id})
response.raise_for_status()
@@ -64,7 +64,7 @@ def get_client(client_id):
def update_client(client_id, otp_code, email=None, phone_number=None, address=None):
"""Updates the client details for the given client_id."""
try:
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
params = {'client_id': client_id, 'otp_code': otp_code}
if email is not None:
@@ -88,7 +88,7 @@ def update_client(client_id, otp_code, email=None, phone_number=None, address=No
def get_accounts(client_id):
"""Retrieves the accounts associated with the given client_id."""
try:
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
response = requests.get(CONFIG["server"]["url"] + "/Client/Accounts", cookies=session_data['session_cookie'], params={'client_id': client_id})
response.raise_for_status()
@@ -103,7 +103,7 @@ def get_accounts(client_id):
def get_transactions(account_id):
"""Retrieves the transactions for the given account_id."""
try:
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
response = requests.get(CONFIG["server"]["url"] + "/Transaction/History", cookies=session_data['session_cookie'], params={'account_id': account_id})
response.raise_for_status()
@@ -118,7 +118,7 @@ def get_transactions(account_id):
def get_account(account_id):
"""Retrieves the account details for the given account_id."""
try:
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
response = requests.get(CONFIG["server"]["url"] + "/Account", cookies=session_data['session_cookie'], params={'account_id': account_id})
response.raise_for_status()
@@ -134,7 +134,7 @@ def get_account(account_id):
def get_transaction(transaction_id):
"""Retrieves the transaction details for the given transaction_id."""
try:
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
response = requests.get(CONFIG["server"]["url"] + "/Transaction", cookies=session_data['session_cookie'], params={'transaction_id': transaction_id})
response.raise_for_status()
@@ -150,7 +150,7 @@ def get_transaction(transaction_id):
def update_account(account_id, otp_code:int, description=None, notes=None):
"""Updates the account details for the given account_id."""
try:
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
params = {'account_id': account_id, 'otp_code': otp_code}
if description is not None:
@@ -172,7 +172,7 @@ def update_account(account_id, otp_code:int, description=None, notes=None):
def new_transaction(account_id, description, amount, recipient_account_id, otp_code):
"""Creates a new transaction for the given account."""
try:
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
# Prepare the query parameters
params = {
@@ -205,7 +205,7 @@ def new_transaction(account_id, description, amount, recipient_account_id, otp_c
def generate_otp():
"""Generates a new OTP for the current client, which is sent by Email."""
try:
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
client_id = session_data['client_id']
response = requests.post(f"{CONFIG['server']['url']}/OTP/Generate", cookies=session_data['session_cookie'], params={'client_id': client_id})
@@ -227,7 +227,7 @@ def change_password(client_id, old_password, new_password, otp_code):
return {'success': False, 'message': "Invalid OTP code format: must be an integer."}
try:
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
payload = { # Prepare the payload to be sent in the request body
'client_id': client_id,

View File

@@ -21,7 +21,7 @@ frame = None
def go_to_login():
"""Closes the current window and opens the login page."""
root.destroy()
os.system("python application/login.py")
os.system("python login.py")
def logout():
"""Logs out the client and redirects to the login page."""
@@ -54,7 +54,7 @@ def display_client_info():
frame = customtkinter.CTkFrame(root)
frame.grid(row=1, column=0, padx=20, pady=20)
try:
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
client_id = session_data['client_id']
client_info = get_client(client_id)
@@ -79,7 +79,7 @@ def edit_details():
global edit_window, email_entry, phone_entry, address_entry, otp_entry
edit_window = customtkinter.CTkToplevel(root)
edit_window.title("Edit Details")
edit_window.iconbitmap("application/luxbank.ico")
edit_window.iconbitmap("luxbank.ico")
edit_window.geometry("300x330")
edit_window.attributes('-topmost', True)
@@ -122,7 +122,7 @@ def save_details():
if not otp_code:
messagebox.showerror("Error", "OTP code must be entered.")
return
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
client_id = session_data['client_id']
if not messagebox.askyesno("Confirmation", "Are you sure you want to update the details?"):
@@ -146,7 +146,7 @@ def populate_table():
for row in table.get_children(): # Clear the table before populating it
table.delete(row)
try:
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
client_id = session_data['client_id']
response = get_accounts(client_id)
@@ -163,9 +163,9 @@ def on_account_double_click(event):
"""Handles double-click event on an account in the table."""
try:
selected_account = table.item(table.selection())
session = json.load(open('application\\session_data.json', 'r'))
session = json.load(open('session_data.json', 'r'))
account_description = selected_account['values'][0]
command = f"python application\\account.py {selected_account['values'][1]} {session['client_id']} \"{account_description}\""
command = f"python account.py {selected_account['values'][1]} {session['client_id']} \"{account_description}\""
return_code = os.system(command)
if return_code != 0:
print(f"Error: The command failed with return code {return_code}")
@@ -182,7 +182,7 @@ def change_password_box():
global edit_window,password_entry, old_password_entry, confirm_password_entry, otp_entry
edit_window = customtkinter.CTkToplevel(root)
edit_window.title("Change Password")
edit_window.iconbitmap("application/luxbank.ico")
edit_window.iconbitmap("luxbank.ico")
edit_window.geometry("300x350")
edit_window.attributes('-topmost', True)
@@ -233,7 +233,7 @@ def change_password_save():
if new_password != confirm_password:
messagebox.showerror("Error", "New password and confirm password do not match.")
return
with open('application\\session_data.json', 'r') as f:
with open('session_data.json', 'r') as f:
session_data = json.load(f)
client_id = session_data['client_id']
if not messagebox.askyesno("Confirmation", "Are you sure you want to change the password?"):
@@ -251,8 +251,8 @@ def change_password_save():
def open_transaction_window():
"""Opens a new window for creating a new transaction."""
try:
session = json.load(open('application\\session_data.json', 'r'))
command = f"python application\\new_transaction.py {session['client_id']}"
session = json.load(open('session_data.json', 'r'))
command = f"python new_transaction.py {session['client_id']}"
return_code = os.system(command)
if return_code != 0:
print(f"Error: The command failed with return code {return_code}")
@@ -265,7 +265,7 @@ def open_transaction_window():
root = customtkinter.CTk()
root.title("Luxbank Dashboard")
root.iconbitmap("application/luxbank.ico")
root.iconbitmap("luxbank.ico")
root.geometry("800x350")
# Set appearance mode based on configuration

View File

@@ -26,10 +26,10 @@ def login():
'session_cookie': response.cookies.get_dict(),
'client_id': client_id
}
with open('application/session_data.json', 'w') as f: # Save the session data to a file
with open('session_data.json', 'w') as f: # Save the session data to a file
json.dump(session_data, f)
root.destroy()
os.system("python application/dashboard.py")
os.system("python dashboard.py")
elif response.status_code == 401:
messagebox.showerror("Login failed", "Invalid client ID or password.")
else:
@@ -42,12 +42,12 @@ def login():
def change_dark_theme():
"""Change the theme between dark and light."""
config = configparser.ConfigParser()
config.read('application/app.ini')
config.read('app.ini')
if 'preferences' in config:
current_theme = config.get('preferences', 'dark_theme')
new_theme = 'light' if current_theme == 'dark' else 'dark'
config.set('preferences', 'dark_theme', new_theme)
with open('application/app.ini', 'w') as configfile:
with open('app.ini', 'w') as configfile:
config.write(configfile)
os.execl(sys.executable, sys.executable, *sys.argv)
else:
@@ -69,7 +69,7 @@ else:
# Initialize the main window
root = customtkinter.CTk()
root.title("Luxbank Login")
root.iconbitmap("application/luxbank.ico")
root.iconbitmap("luxbank.ico")
root.geometry("400x300")
# Create and pack the label for the title

View File

@@ -100,7 +100,7 @@ def populate_accounts(client_id):
# Initialise the main window
root = customtkinter.CTk()
root.title("New Transaction")
root.iconbitmap("application/luxbank.ico")
root.iconbitmap("luxbank.ico")
root.geometry("400x600")
if CONFIG["preferences"]["dark_theme"] == "dark": # Check if dark mode is enabled

View File

@@ -44,7 +44,7 @@ def display_transaction_info(transaction_id):
# Initialise the main window
root = customtkinter.CTk()
root.title("Banking System Transaction Page")
root.iconbitmap("application/luxbank.ico")
root.iconbitmap("luxbank.ico")
root.geometry("370x300")
# Create a close button at the top of the window