Skip to content
Snippets Groups Projects
Verified Commit fb71d3b5 authored by TheJoeCoder's avatar TheJoeCoder
Browse files

[Frontend] autoconnect and order

parent 6b3b6ec2
Branches
No related tags found
No related merge requests found
......@@ -2,6 +2,9 @@ import json, requests, logging
from tkinter import messagebox, ttk
from tkinter import *
## Server URL
server_url = "http://localhost:5000"
## Init
root = Tk()
......@@ -23,6 +26,14 @@ orderpage_items = []
def get_endpoint(endpoint):
return connect_url.get() + endpoint
def validate_int(input):
try:
unused = int(input)
return True
except:
return False
validate_int_command = (root.register(validate_int), "%P")
# Create frame
def create_tab(title, padding="3 3 12 12", sticky=(N, W, E, S)):
frame = ttk.Frame(tab_control, padding=padding)
......@@ -34,7 +45,7 @@ def create_tab(title, padding="3 3 12 12", sticky=(N, W, E, S)):
# Configure grid for frame
def configure_grid(frame):
for child in connect_frame.winfo_children():
for child in frame.winfo_children():
child.grid_configure(padx=5, pady=5)
# General connect fail function
......@@ -43,15 +54,15 @@ def configure_grid(frame):
# where a server request is made
def connect_fail():
logger.error("Connection Failed")
connect_status_label.configure(text="Connection failed")
tab_control.select(connect_frame)
#connect_status_label.configure(text="Connection failed")
#tab_control.select(connect_frame)
messagebox.showerror("Connection failed", "Could not connect to server")
# Request session fail function
def request_session_fail():
logger.error("Request session failed")
connect_status_label.configure(text="Request session failed")
tab_control.select(connect_frame)
#connect_status_label.configure(text="Request session failed")
#tab_control.select(connect_frame)
messagebox.showerror("Request session failed", "Could not request session")
# Connection success function
......@@ -59,7 +70,7 @@ def request_session_fail():
# in the initial connection attempt
def connect_success():
logger.info("Successfully connected to " + connect_url.get())
connect_status_label.configure(text="Connected")
#connect_status_label.configure(text="Connected")
tab_control.select(order_frame)
messagebox.showinfo("OK!", "Successfully connected")
......@@ -77,6 +88,7 @@ def connect(*args):
# Request session
logger.debug("Got OK response from server")
if (new_session()):
refresh_order_items()
connect_success()
return
request_session_fail()
......@@ -124,43 +136,64 @@ def refresh_items():
return False
def order_item(item, people, stuffed_crust):
pass
item_dict = {}
for pizza in pizzas:
if pizza["id"] == item:
item_dict = pizza
break
if item_dict == {}:
messagebox.showerror("Error", "Could not find pizza")
return
res = requests.post(get_endpoint("/cart/add"), json={"cart_id": cart_id, "cart_key": cart_key, "pizza_id": item, "people": people, "stuffed_crust": stuffed_crust})
if (res.status_code == 200):
res_json = res.json()
if (res_json != None and res_json["status"] == 200):
messagebox.showinfo("OK!", "Successfully added to cart")
else:
connect_fail()
else:
connect_fail()
class OrderItem():
def __init__(self, pizza_dict, col):
self.id = pizza_dict.id
self.col = col
self.label1 = ttk.Label(order_frame, text=pizza_dict.name)
self.label1.grid(column=col, row=1, sticky=(W, E))
self.label2 = ttk.Label(order_frame, text="£%.2f" % pizza_dict.costpp)
self.label2.grid(column=col, row=2, sticky=(W, E))
# TODO number of people
self.quantity = IntVar(value=1)
# self.quant_entry = ttk.Spinbox(order_frame, from=1, to=99)
# TODO stuffed crust
self.stuffed_crust = BooleanVar()
def __init__(self, pizza_dict, row):
global order_frame
print(pizza_dict)
self.id = pizza_dict["id"]
self.row = row
self.label1 = ttk.Label(order_frame, text=pizza_dict["name"])
self.label1.grid(column=1, row=row, sticky=(W, E))
self.label2 = ttk.Label(order_frame, text="£%.2f" % pizza_dict["cost_pp"])
self.label2.grid(column=2, row=row, sticky=(W, E))
self.quantity = StringVar(value=1)
self.quant_entry = ttk.Spinbox(order_frame, from_=1, to=99, textvariable=self.quantity, validate="focusout", validatecommand=validate_int_command)
self.quant_entry.grid(column=3, row=row, sticky=(W, E))
self.stuffed_crust = BooleanVar(value=False)
self.stuffed_entry = ttk.Checkbutton(order_frame, variable=self.stuffed_crust)
self.stuffed_entry.grid(column=4, row=row, sticky=(W, E))
self.order_butt = ttk.Button(order_frame, text="Order", command=self.order)
self.order_butt.grid(column=col, row=4, sticky=(W, E))
self.order_butt.grid(column=5, row=row, sticky=(W, E))
def destroy(self):
self.label1.destroy()
self.label2.destroy()
self.quant_entry.destroy()
self.stuffed_entry.destroy()
self.order_butt.destroy()
def order(self):
order_item(self.id, self.quantity.get(), self.stuffed_crust.get())
if (validate_int(self.quantity.get())):
order_item(self.id, int(self.quantity.get()), self.stuffed_crust.get())
def refresh_order_items(*args):
global order_frame
if(refresh_items()):
# Remove all items
for op_item in orderpage_items:
for op_childitem in op_item:
op_childitem.destroy()
del op_childitem
op_item.destroy()
del op_item
col = 1
row = 2
for pizza in pizzas:
piz_label1 = ttk.Label(order_frame, text=pizza.name)
piz_label1.grid(column=col, row=1, sticky=(W, E))
piz_label2 = ttk.Label(order_frame, text="£%.2f" % pizza.costpp)
piz_label2.grid(column=col, row=2, sticky=(W, E))
piz_orderbutton = ttk.Button(order_frame, text="Order", command=lambda: order_item())
orderpage_items.append(OrderItem(pizza, row))
row += 1
configure_grid(order_frame)
## Main program code
# Window setup
......@@ -168,28 +201,41 @@ root.title("Pizza Palace")
tab_control = ttk.Notebook(root)
# Connect tab
connect_frame = create_tab("Connect")
#connect_frame = create_tab("Connect")
#connect_url_label = ttk.Label(connect_frame, text="Server URL:")
#connect_url_field = ttk.Entry(connect_frame, width=20, textvariable=connect_url)
#connect_url_label.grid(column=1, row=1, sticky=(W, E))
#connect_url_field.grid(column=2, row=1, sticky=(W, E))
#connect_url.set("http://localhost:5000")
connect_url_label = ttk.Label(connect_frame, text="Server URL:")
connect_url_field = ttk.Entry(connect_frame, width=20, textvariable=connect_url)
connect_url_label.grid(column=1, row=1, sticky=(W, E))
connect_url_field.grid(column=2, row=1, sticky=(W, E))
connect_url.set("http://localhost:5000")
connect_url.set(server_url)
connect_status_label = ttk.Label(connect_frame, text="Not connected")
connect_status_label.grid(column=1, row=2, sticky=(W, E))
#connect_status_label = ttk.Label(connect_frame, text="Not connected")
#connect_status_label.grid(column=1, row=2, sticky=(W, E))
connect_submit_button = ttk.Button(connect_frame, text="Go!", command=connect)
connect_submit_button.grid(column=1, row=3, sticky=(W, E))
#connect_submit_button = ttk.Button(connect_frame, text="Go!", command=connect)
#connect_submit_button.grid(column=1, row=3, sticky=(W, E))
configure_grid(connect_frame)
#configure_grid(connect_frame)
connect_submit_button.focus()
connect_frame.bind("<Return>", connect)
#connect_submit_button.focus()
#connect_frame.bind("<Return>", connect)
# Order tab
order_frame = create_tab("Order")
refresh_order_items()
order_label_1 = ttk.Label(order_frame, text="Name")
order_label_1.grid(column=1, row=1, sticky=(W, E))
order_label_2 = ttk.Label(order_frame, text="Cost")
order_label_2.grid(column=2, row=1, sticky=(W, E))
order_label_3 = ttk.Label(order_frame, text="Number of People")
order_label_3.grid(column=3, row=1, sticky=(W, E))
order_label_4 = ttk.Label(order_frame, text="Stuffed Crust?")
order_label_4.grid(column=4, row=1, sticky=(W, E))
order_label_5 = ttk.Label(order_frame, text="Order")
order_label_5.grid(column=5, row=1, sticky=(W, E))
configure_grid(order_frame)
# Cart tab
cart_frame = create_tab("Cart")
......@@ -197,9 +243,11 @@ cart_frame = create_tab("Cart")
# Confirm tab
confirm_frame = create_tab("Confirm")
# Pack
tab_control.pack(expand=1, fill="both")
# Connect
connect()
# Main window loop
root.mainloop()
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please to comment