Minor changes and added descriptions to all funcitons

This commit is contained in:
Lucas Mathews
2024-05-28 11:03:44 +02:00
parent 0732b26af3
commit 7b6ebbd0d2
12 changed files with 132 additions and 87 deletions

View File

@@ -1,7 +1,6 @@
import tkinter as tk
from tkinter import ttk
from tkinter import ttk, messagebox
import customtkinter
import json
from config import CONFIG
from connection import get_transactions, format_balance, get_account
import sys
@@ -14,23 +13,20 @@ if len(sys.argv) > 3: # Check if the account description is provided as a comma
account_id = sys.argv[1]
account_description = sys.argv[3]
else:
print("Error: Account description not provided.")
messagebox.showerror("Error", "Account ID and description were not provided by the server.")
sys.exit(1)
def populate_transactions_table(transactions_table, account_id):
"""Populate the transactions table with data for the given account ID."""
response = get_transactions(account_id) # Fetch the transactions for the account
print(f"Response from get_transactions: {response}") # Print the response
if response is None or 'data' not in response:
print(f"Error: Unable to fetch transactions for account {account_id}")
messagebox.showerror("Error", "Could not fetch transactions for the account.")
return
transactions = response['data']
if not isinstance(transactions, list):
print(f"Error: Expected a list of transactions, but got {type(transactions)}")
messagebox.showerror("Error", "Data is not formatted as expected.")
return
# Sort transactions by timestamp in descending order
transactions.sort(key=lambda x: x['timestamp'], reverse=True)
transactions.sort(key=lambda x: x['timestamp'], reverse=True) # Sort transactions by timestamp in descending order
for transaction in transactions: # Insert the transactions into the transactions_table
transactions_table.insert('', 'end', values=(
transaction['transaction_id'],

View File

@@ -1,14 +1,13 @@
[server]
ip=0.0.0.0
port=81
url=http://127.0.0.1:81
ip = 0.0.0.0
port = 81
url = http://127.0.0.1:81
[preferences]
dark_theme=dark
# Modes: light, dark
theme=dark-blue
# Themes: blue, dark-blue, green
dark_theme = dark
theme = dark-blue
[client]
default_id=31d90aad
default_password=Happymeal1
default_id = 31d90aad
default_password = Happymeal1

View File

@@ -8,9 +8,7 @@ import json
##############
def format_balance(balance):
"""
Formats the balance as a currency string with comma separators.
"""
"""Formats the balance as a currency string with comma separators."""
return f"{balance:,.2f}"
#####################
@@ -18,9 +16,7 @@ def format_balance(balance):
#####################
def authenticate_client(client_id, client_password):
"""
Authenticates a client with the given client_id and client_password.
"""
"""Authenticates a client with the given client_id and client_password."""
try:
response = requests.post(CONFIG["server"]["url"] + "/Client/Login", params={'client_id': client_id, 'password': client_password})
return response
@@ -32,9 +28,7 @@ def authenticate_client(client_id, client_password):
return response
def logout_client():
"""
Logs out the current client.
"""
"""Logs out the current client."""
try:
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
@@ -48,9 +42,7 @@ def logout_client():
return response
def get_client(client_id):
"""
Retrieves the client details for the given client_id.
"""
"""Retrieves the client details for the given client_id."""
try:
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
@@ -62,9 +54,7 @@ def get_client(client_id):
return {'success': False, 'message': "Could not connect to the server. Please try again later."}
def update_client(client_id, email=None, phone_number=None, address=None):
"""
Updates the client details for the given client_id.
"""
"""Updates the client details for the given client_id."""
try:
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
@@ -83,9 +73,7 @@ def update_client(client_id, email=None, phone_number=None, address=None):
return {'success': False, 'message': "Could not connect to the server. Please try again later."}
def get_accounts(client_id):
"""
Retrieves the accounts associated with the given client_id.
"""
"""Retrieves the accounts associated with the given client_id."""
try:
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
@@ -100,9 +88,7 @@ def get_accounts(client_id):
return {'success': False, 'message': "Could not connect to the server. Please try again later."}
def get_transactions(account_id):
"""
Retrieves the transactions for the given account_id.
"""
"""Retrieves the transactions for the given account_id."""
try:
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
@@ -117,9 +103,7 @@ def get_transactions(account_id):
return {'success': False, 'message': "Could not connect to the server. Please try again later."}
def get_account(account_id):
"""
Retrieves the account details for the given account_id.
"""
"""Retrieves the account details for the given account_id."""
try:
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
@@ -135,9 +119,7 @@ def get_account(account_id):
return {'success': False, 'message': "Could not connect to the server. Please try again later."}
def update_account(account_id, description=None, notes=None):
"""
Updates the account details for the given account_id.
"""
"""Updates the account details for the given account_id."""
try:
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)
@@ -154,9 +136,7 @@ def update_account(account_id, description=None, notes=None):
return {'success': False, 'message': "Could not connect to the server. Please try again later."}
def new_transaction(account):
"""
Creates a new transaction for the given account.
"""
"""Creates a new transaction for the given account."""
try:
with open('application\\session_data.json', 'r') as f:
session_data = json.load(f)

View File

@@ -15,13 +15,18 @@ frame = None
### Functions ###
#################
def go_to_login():
"""Closes the current window and opens the login page."""
root.destroy()
login_page() # Replace with your function or class that opens the login page
def logout():
"""Logs out the client and closes the application."""
"""Logs out the client and redirects to the login page."""
response = logout_client()
json_response = response.json()
if json_response['success']:
messagebox.showinfo("Logout", "You have been logged out.")
root.destroy()
go_to_login()
else:
messagebox.showerror("Logout failed", json_response['message'])

View File

@@ -6,13 +6,15 @@ import json
import requests
from connection import authenticate_client
from config import CONFIG
import configparser, sys
#################
### Functions ###
#################
def login():
"""Function to handle the login process."""
"""Authenticate the client and open the dashboard if successful."""
client_id = entry_username.get() if entry_username.get() else CONFIG["client"]["default_id"]
client_password = entry_password.get() if entry_password.get() else CONFIG["client"]["default_password"]
try:
@@ -32,6 +34,20 @@ def login():
except requests.exceptions.RequestException as e:
messagebox.showerror("Login failed", f"Could not connect to the server. Please try again later. Error: {str(e)}")
def change_dark_theme():
"""Change the theme between dark and light."""
config = configparser.ConfigParser()
config.read('application/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:
config.write(configfile)
os.execl(sys.executable, sys.executable, *sys.argv)
else:
messagebox.showerror("Error", "Could not change the theme. Please check the configuration file.")
##############
### Layout ###
##############
@@ -64,6 +80,10 @@ entry_password.pack(pady=10)
login_button = customtkinter.CTkButton(root, text="Login", command=login)
login_button.pack(pady=15)
# Create and pack the theme change button
theme_button = customtkinter.CTkButton(root, text="Change Theme", command=change_dark_theme)
theme_button.pack(side='bottom', anchor='w')
# Bind the Return key to the login function
root.bind('<Return>', lambda event=None: login())

View File

@@ -1 +1 @@
{"session_cookie": {"session": "snGl2pPHJKUmCdGIH4QdeIgGmzaPxjFZsUe30lxaMpk"}, "client_id": "31d90aad"}
{"session_cookie": {"session": "G1SQpUTHsed2pPeVe4-1vtqwOXzE5-HMGZKWxvNqzTs"}, "client_id": "31d90aad"}