Skip to content
Snippets Groups Projects
Commit 7c6e5ec8 authored by Nguyen12.Minh@live.uwe.ac.uk's avatar Nguyen12.Minh@live.uwe.ac.uk
Browse files

Add many things

parent c12c952b
No related branches found
No related tags found
2 merge requests!5Minh,!4Minh
...@@ -2,10 +2,13 @@ import customtkinter as ctk ...@@ -2,10 +2,13 @@ import customtkinter as ctk
import requests import requests
from tkinter import messagebox from tkinter import messagebox
def category_frame(parent, switch_func, API_URL, access_token): def category_frame(parent, switch_func, API_URL, access_token):
frame = ctk.CTkFrame(parent) frame = ctk.CTkFrame(parent)
ctk.CTkLabel(frame, text="Category Management", font=("Helvetica", 18, "bold")).pack(pady=10) ctk.CTkLabel(
frame, text="Category Management", font=("Helvetica", 18, "bold")
).pack(pady=10)
ctk.CTkLabel(frame, text="Category Name:").pack(pady=5) ctk.CTkLabel(frame, text="Category Name:").pack(pady=5)
entry_name = ctk.CTkEntry(frame) entry_name = ctk.CTkEntry(frame)
...@@ -24,12 +27,18 @@ def category_frame(parent, switch_func, API_URL, access_token): ...@@ -24,12 +27,18 @@ def category_frame(parent, switch_func, API_URL, access_token):
return return
try: try:
response = requests.post(f"{API_URL}/category", data={"name": name}, headers=headers) response = requests.post(
f"{API_URL}/category", data={"name": name}, headers=headers
)
if response.status_code == 200: if response.status_code == 200:
messagebox.showinfo("Success", f"Category '{name}' created successfully!") messagebox.showinfo(
entry_name.delete(0, 'end') "Success", f"Category '{name}' created successfully!"
)
entry_name.delete(0, "end")
else: else:
messagebox.showerror("Error", response.json().get("detail", "Failed to create category")) messagebox.showerror(
"Error", response.json().get("detail", "Failed to create category")
)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
messagebox.showerror("Error", f"Failed to connect to server: {e}") messagebox.showerror("Error", f"Failed to connect to server: {e}")
...@@ -39,7 +48,9 @@ def category_frame(parent, switch_func, API_URL, access_token): ...@@ -39,7 +48,9 @@ def category_frame(parent, switch_func, API_URL, access_token):
if response.status_code == 200: if response.status_code == 200:
categories = response.json() categories = response.json()
if categories: if categories:
category_list = "\n".join([f"{cat['id']}: {cat['name']}" for cat in categories]) category_list = "\n".join(
[f"{cat['id']}: {cat['name']}" for cat in categories]
)
messagebox.showinfo("Categories", category_list) messagebox.showinfo("Categories", category_list)
else: else:
messagebox.showinfo("Categories", "No categories available.") messagebox.showinfo("Categories", "No categories available.")
...@@ -55,17 +66,25 @@ def category_frame(parent, switch_func, API_URL, access_token): ...@@ -55,17 +66,25 @@ def category_frame(parent, switch_func, API_URL, access_token):
messagebox.showwarning("Input Error", "Enter a valid Category ID!") messagebox.showwarning("Input Error", "Enter a valid Category ID!")
return return
if not name: if not name:
messagebox.showwarning("Input Error", "Category name is required for update!") messagebox.showwarning(
"Input Error", "Category name is required for update!"
)
return return
try: try:
response = requests.put(f"{API_URL}/category/{category_id}", data={"name": name}, headers=headers) response = requests.put(
f"{API_URL}/category/{category_id}",
data={"name": name},
headers=headers,
)
if response.status_code == 200: if response.status_code == 200:
messagebox.showinfo("Success", "Category updated successfully!") messagebox.showinfo("Success", "Category updated successfully!")
entry_id.delete(0, 'end') entry_id.delete(0, "end")
entry_name.delete(0, 'end') entry_name.delete(0, "end")
else: else:
messagebox.showerror("Error", response.json().get("detail", "Failed to update category")) messagebox.showerror(
"Error", response.json().get("detail", "Failed to update category")
)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
messagebox.showerror("Error", f"Failed to connect to server: {e}") messagebox.showerror("Error", f"Failed to connect to server: {e}")
...@@ -76,12 +95,16 @@ def category_frame(parent, switch_func, API_URL, access_token): ...@@ -76,12 +95,16 @@ def category_frame(parent, switch_func, API_URL, access_token):
return return
try: try:
response = requests.delete(f"{API_URL}/category/{category_id}", headers=headers) response = requests.delete(
f"{API_URL}/category/{category_id}", headers=headers
)
if response.status_code == 200: if response.status_code == 200:
messagebox.showinfo("Success", "Category deleted successfully!") messagebox.showinfo("Success", "Category deleted successfully!")
entry_id.delete(0, 'end') entry_id.delete(0, "end")
else: else:
messagebox.showerror("Error", response.json().get("detail", "Failed to delete category")) messagebox.showerror(
"Error", response.json().get("detail", "Failed to delete category")
)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
messagebox.showerror("Error", f"Failed to connect to server: {e}") messagebox.showerror("Error", f"Failed to connect to server: {e}")
......
...@@ -2,6 +2,7 @@ import customtkinter as ctk ...@@ -2,6 +2,7 @@ import customtkinter as ctk
from tkinter import messagebox from tkinter import messagebox
import requests import requests
def login_frame(parent, switch_func, API_URL): def login_frame(parent, switch_func, API_URL):
frame = ctk.CTkFrame(parent) frame = ctk.CTkFrame(parent)
...@@ -24,7 +25,9 @@ def login_frame(parent, switch_func, API_URL): ...@@ -24,7 +25,9 @@ def login_frame(parent, switch_func, API_URL):
messagebox.showinfo("Login Successful", f"Welcome back, {email}!") messagebox.showinfo("Login Successful", f"Welcome back, {email}!")
switch_func("create_shop", access_token) switch_func("create_shop", access_token)
else: else:
messagebox.showerror("Login Failed", response_data.get("detail", "Invalid credentials")) messagebox.showerror(
"Login Failed", response_data.get("detail", "Invalid credentials")
)
except requests.exceptions.JSONDecodeError: except requests.exceptions.JSONDecodeError:
messagebox.showerror("Login Failed", "Server returned an invalid response.") messagebox.showerror("Login Failed", "Server returned an invalid response.")
......
...@@ -2,6 +2,7 @@ import customtkinter as ctk ...@@ -2,6 +2,7 @@ import customtkinter as ctk
from tkinter import messagebox from tkinter import messagebox
import requests import requests
def register_frame(parent, switch_func, API_URL): def register_frame(parent, switch_func, API_URL):
frame = ctk.CTkFrame(parent) frame = ctk.CTkFrame(parent)
...@@ -12,7 +13,13 @@ def register_frame(parent, switch_func, API_URL): ...@@ -12,7 +13,13 @@ def register_frame(parent, switch_func, API_URL):
password = entry_password.get() password = entry_password.get()
confirm_password = entry_confirm_password.get() confirm_password = entry_confirm_password.get()
if not username or not email or not phone_number or not password or not confirm_password: if (
not username
or not email
or not phone_number
or not password
or not confirm_password
):
messagebox.showwarning("Input Error", "All fields are required!") messagebox.showwarning("Input Error", "All fields are required!")
return return
...@@ -22,7 +29,12 @@ def register_frame(parent, switch_func, API_URL): ...@@ -22,7 +29,12 @@ def register_frame(parent, switch_func, API_URL):
response = requests.post( response = requests.post(
f"{API_URL}/auth/signup", f"{API_URL}/auth/signup",
json={"username": username, "email": email, "phone_number": phone_number, "password": password}, json={
"username": username,
"email": email,
"phone_number": phone_number,
"password": password,
},
) )
try: try:
...@@ -31,9 +43,13 @@ def register_frame(parent, switch_func, API_URL): ...@@ -31,9 +43,13 @@ def register_frame(parent, switch_func, API_URL):
messagebox.showinfo("Registration Successful", f"Welcome, {username}!") messagebox.showinfo("Registration Successful", f"Welcome, {username}!")
switch_func("login") switch_func("login")
else: else:
messagebox.showerror("Registration Failed", response_data.get("detail", "Unknown error")) messagebox.showerror(
"Registration Failed", response_data.get("detail", "Unknown error")
)
except requests.exceptions.JSONDecodeError: except requests.exceptions.JSONDecodeError:
messagebox.showerror("Registration Failed", "Server returned an invalid response.") messagebox.showerror(
"Registration Failed", "Server returned an invalid response."
)
ctk.CTkLabel(frame, text="Register", font=("Helvetica", 18, "bold")).pack(pady=10) ctk.CTkLabel(frame, text="Register", font=("Helvetica", 18, "bold")).pack(pady=10)
......
...@@ -2,10 +2,13 @@ import customtkinter as ctk ...@@ -2,10 +2,13 @@ import customtkinter as ctk
import requests import requests
from tkinter import messagebox from tkinter import messagebox
def product_frame(parent, switch_func, API_URL, token): def product_frame(parent, switch_func, API_URL, token):
frame = ctk.CTkFrame(parent) frame = ctk.CTkFrame(parent)
ctk.CTkLabel(frame, text="Product Management", font=("Helvetica", 18, "bold")).pack(pady=10) ctk.CTkLabel(frame, text="Product Management", font=("Helvetica", 18, "bold")).pack(
pady=10
)
ctk.CTkLabel(frame, text="Product Name:").pack(pady=5) ctk.CTkLabel(frame, text="Product Name:").pack(pady=5)
entry_name = ctk.CTkEntry(frame) entry_name = ctk.CTkEntry(frame)
...@@ -32,7 +35,9 @@ def product_frame(parent, switch_func, API_URL, token): ...@@ -32,7 +35,9 @@ def product_frame(parent, switch_func, API_URL, token):
price = float(price) price = float(price)
stock = int(stock) stock = int(stock)
except ValueError: except ValueError:
messagebox.showwarning("Input Error", "Price must be a number and Stock must be an integer.") messagebox.showwarning(
"Input Error", "Price must be a number and Stock must be an integer."
)
return return
headers = {"Authorization": f"Bearer {token}"} headers = {"Authorization": f"Bearer {token}"}
...@@ -42,17 +47,23 @@ def product_frame(parent, switch_func, API_URL, token): ...@@ -42,17 +47,23 @@ def product_frame(parent, switch_func, API_URL, token):
response = requests.post(f"{API_URL}/products", json=data, headers=headers) response = requests.post(f"{API_URL}/products", json=data, headers=headers)
if response.status_code == 200: if response.status_code == 200:
messagebox.showinfo("Success", f"Product '{name}' created successfully!") messagebox.showinfo(
entry_name.delete(0, 'end') "Success", f"Product '{name}' created successfully!"
entry_price.delete(0, 'end') )
entry_stock.delete(0, 'end') entry_name.delete(0, "end")
entry_price.delete(0, "end")
entry_stock.delete(0, "end")
else: else:
error_message = response.json().get("detail", "Failed to create product") error_message = response.json().get(
"detail", "Failed to create product"
)
messagebox.showerror("Error", error_message) messagebox.showerror("Error", error_message)
except requests.exceptions.RequestException as e: except requests.exceptions.RequestException as e:
messagebox.showerror("Error", f"Failed to connect to server: {e}") messagebox.showerror("Error", f"Failed to connect to server: {e}")
ctk.CTkButton(frame, text="Create Product", command=create_product).pack(pady=15) ctk.CTkButton(frame, text="Create Product", command=create_product).pack(pady=15)
ctk.CTkButton(frame, text="Back", command=lambda: switch_func("view_shop")).pack(pady=5) ctk.CTkButton(frame, text="Back", command=lambda: switch_func("view_shop")).pack(
pady=5
)
return frame return frame
...@@ -63,7 +63,9 @@ def create_shop_frame(parent, switch_func, API_URL, token): ...@@ -63,7 +63,9 @@ def create_shop_frame(parent, switch_func, API_URL, token):
except Exception as e: except Exception as e:
messagebox.showerror("Request Error", str(e)) messagebox.showerror("Request Error", str(e))
ctk.CTkLabel(frame, text="Create Shop", font=("Helvetica", 18, "bold")).pack(pady=10) ctk.CTkLabel(frame, text="Create Shop", font=("Helvetica", 18, "bold")).pack(
pady=10
)
ctk.CTkLabel(frame, text="Shop Name:").pack(pady=5) ctk.CTkLabel(frame, text="Shop Name:").pack(pady=5)
entry_name = ctk.CTkEntry(frame, placeholder_text="Enter shop name") entry_name = ctk.CTkEntry(frame, placeholder_text="Enter shop name")
...@@ -77,7 +79,9 @@ def create_shop_frame(parent, switch_func, API_URL, token): ...@@ -77,7 +79,9 @@ def create_shop_frame(parent, switch_func, API_URL, token):
file_label = ctk.CTkLabel(frame, text="No file selected") file_label = ctk.CTkLabel(frame, text="No file selected")
file_label.pack(pady=5) file_label.pack(pady=5)
ctk.CTkButton(frame, text="Create Shop", fg_color="green", command=create_shop).pack(pady=15) ctk.CTkButton(
frame, text="Create Shop", fg_color="green", command=create_shop
).pack(pady=15)
ctk.CTkButton( ctk.CTkButton(
frame, frame,
......
...@@ -4,6 +4,7 @@ from tkinter import messagebox ...@@ -4,6 +4,7 @@ from tkinter import messagebox
from PIL import Image, ImageTk from PIL import Image, ImageTk
import io import io
def view_shop_frame(parent, switch_func, API_URL, token): def view_shop_frame(parent, switch_func, API_URL, token):
frame = ctk.CTkFrame(parent) frame = ctk.CTkFrame(parent)
...@@ -12,10 +13,14 @@ def view_shop_frame(parent, switch_func, API_URL, token): ...@@ -12,10 +13,14 @@ def view_shop_frame(parent, switch_func, API_URL, token):
title_label.pack(pady=10) title_label.pack(pady=10)
# Shop Details # Shop Details
shop_name_label = ctk.CTkLabel(frame, text="Shop Name: ", font=("Helvetica", 14, "bold")) shop_name_label = ctk.CTkLabel(
frame, text="Shop Name: ", font=("Helvetica", 14, "bold")
)
shop_name_label.pack(pady=5) shop_name_label.pack(pady=5)
shop_description_label = ctk.CTkLabel(frame, text="Description: ", font=("Helvetica", 12)) shop_description_label = ctk.CTkLabel(
frame, text="Description: ", font=("Helvetica", 12)
)
shop_description_label.pack(pady=5) shop_description_label.pack(pady=5)
shop_image_label = ctk.CTkLabel(frame, text="") # Placeholder for shop image shop_image_label = ctk.CTkLabel(frame, text="") # Placeholder for shop image
...@@ -29,11 +34,15 @@ def view_shop_frame(parent, switch_func, API_URL, token): ...@@ -29,11 +34,15 @@ def view_shop_frame(parent, switch_func, API_URL, token):
"""Fetch the shop created by the logged-in user""" """Fetch the shop created by the logged-in user"""
headers = {"Authorization": f"Bearer {token}"} headers = {"Authorization": f"Bearer {token}"}
try: try:
response = requests.get(f"{API_URL}/shops/my-shop", headers=headers) # Adjust the endpoint as needed response = requests.get(
f"{API_URL}/shops/my-shop", headers=headers
) # Adjust the endpoint as needed
if response.status_code == 200: if response.status_code == 200:
shop_data = response.json() shop_data = response.json()
shop_name_label.configure(text=f"Shop Name: {shop_data['name']}") shop_name_label.configure(text=f"Shop Name: {shop_data['name']}")
shop_description_label.configure(text=f"Description: {shop_data.get('description', 'No description')}") shop_description_label.configure(
text=f"Description: {shop_data.get('description', 'No description')}"
)
# Load and display shop image if available # Load and display shop image if available
if "image_url" in shop_data and shop_data["image_url"]: if "image_url" in shop_data and shop_data["image_url"]:
...@@ -58,7 +67,9 @@ def view_shop_frame(parent, switch_func, API_URL, token): ...@@ -58,7 +67,9 @@ def view_shop_frame(parent, switch_func, API_URL, token):
"""Fetch products that belong to the user's shop""" """Fetch products that belong to the user's shop"""
headers = {"Authorization": f"Bearer {token}"} headers = {"Authorization": f"Bearer {token}"}
try: try:
response = requests.get(f"{API_URL}/products?shop_id={shop_id}", headers=headers) response = requests.get(
f"{API_URL}/products?shop_id={shop_id}", headers=headers
)
if response.status_code == 200: if response.status_code == 200:
products = response.json() products = response.json()
display_products(products) display_products(products)
...@@ -73,7 +84,9 @@ def view_shop_frame(parent, switch_func, API_URL, token): ...@@ -73,7 +84,9 @@ def view_shop_frame(parent, switch_func, API_URL, token):
widget.destroy() widget.destroy()
if not products: if not products:
ctk.CTkLabel(product_list_frame, text="No products found.", font=("Helvetica", 12)).pack(pady=10) ctk.CTkLabel(
product_list_frame, text="No products found.", font=("Helvetica", 12)
).pack(pady=10)
return return
for product in products: for product in products:
...@@ -102,15 +115,23 @@ def view_shop_frame(parent, switch_func, API_URL, token): ...@@ -102,15 +115,23 @@ def view_shop_frame(parent, switch_func, API_URL, token):
details_frame = ctk.CTkFrame(product_frame) details_frame = ctk.CTkFrame(product_frame)
details_frame.pack(side="left", fill="x", expand=True, padx=10) details_frame.pack(side="left", fill="x", expand=True, padx=10)
ctk.CTkLabel(details_frame, text=product["name"], font=("Helvetica", 12, "bold")).pack(anchor="w") ctk.CTkLabel(
ctk.CTkLabel(details_frame, text=f"Price: ${product['price']:.2f}", font=("Helvetica", 12)).pack(anchor="w") details_frame, text=product["name"], font=("Helvetica", 12, "bold")
).pack(anchor="w")
ctk.CTkLabel(
details_frame,
text=f"Price: ${product['price']:.2f}",
font=("Helvetica", 12),
).pack(anchor="w")
# Refresh Data Button # Refresh Data Button
refresh_button = ctk.CTkButton(frame, text="Refresh", command=fetch_shop_data) refresh_button = ctk.CTkButton(frame, text="Refresh", command=fetch_shop_data)
refresh_button.pack(pady=10) refresh_button.pack(pady=10)
# Back Button # Back Button
back_button = ctk.CTkButton(frame, text="Back", command=lambda: switch_func("login")) back_button = ctk.CTkButton(
frame, text="Back", command=lambda: switch_func("login")
)
back_button.pack(pady=10) back_button.pack(pady=10)
# Fetch shop data on load # Fetch shop data on load
......
...@@ -12,6 +12,7 @@ API_URL = "http://127.0.0.1:8000" ...@@ -12,6 +12,7 @@ API_URL = "http://127.0.0.1:8000"
# Global variable to store the access token # Global variable to store the access token
access_token = None access_token = None
# Function to switch between frames # Function to switch between frames
def switch_frame(frame_name, token=None): def switch_frame(frame_name, token=None):
global access_token global access_token
...@@ -20,6 +21,7 @@ def switch_frame(frame_name, token=None): ...@@ -20,6 +21,7 @@ def switch_frame(frame_name, token=None):
frames.get(frame_name, login).tkraise() # Default to login if frame_name is invalid frames.get(frame_name, login).tkraise() # Default to login if frame_name is invalid
# Create main window # Create main window
ctk.set_appearance_mode("dark") # Light, Dark, or System ctk.set_appearance_mode("dark") # Light, Dark, or System
ctk.set_default_color_theme("blue") ctk.set_default_color_theme("blue")
...@@ -43,7 +45,7 @@ frames = { ...@@ -43,7 +45,7 @@ frames = {
"create_shop": create_shop, "create_shop": create_shop,
"create_product": product, "create_product": product,
"category": category, "category": category,
"view_shop": view_shop "view_shop": view_shop,
} }
# Place all frames responsively # Place all frames responsively
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment