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 ...@@ -2,6 +2,9 @@ import json, requests, logging
from tkinter import messagebox, ttk from tkinter import messagebox, ttk
from tkinter import * from tkinter import *
## Server URL
server_url = "http://localhost:5000"
## Init ## Init
root = Tk() root = Tk()
...@@ -23,6 +26,14 @@ orderpage_items = [] ...@@ -23,6 +26,14 @@ orderpage_items = []
def get_endpoint(endpoint): def get_endpoint(endpoint):
return connect_url.get() + 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 # Create frame
def create_tab(title, padding="3 3 12 12", sticky=(N, W, E, S)): def create_tab(title, padding="3 3 12 12", sticky=(N, W, E, S)):
frame = ttk.Frame(tab_control, padding=padding) 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)): ...@@ -34,7 +45,7 @@ def create_tab(title, padding="3 3 12 12", sticky=(N, W, E, S)):
# Configure grid for frame # Configure grid for frame
def configure_grid(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) child.grid_configure(padx=5, pady=5)
# General connect fail function # General connect fail function
...@@ -43,15 +54,15 @@ def configure_grid(frame): ...@@ -43,15 +54,15 @@ def configure_grid(frame):
# where a server request is made # where a server request is made
def connect_fail(): def connect_fail():
logger.error("Connection Failed") logger.error("Connection Failed")
connect_status_label.configure(text="Connection failed") #connect_status_label.configure(text="Connection failed")
tab_control.select(connect_frame) #tab_control.select(connect_frame)
messagebox.showerror("Connection failed", "Could not connect to server") messagebox.showerror("Connection failed", "Could not connect to server")
# Request session fail function # Request session fail function
def request_session_fail(): def request_session_fail():
logger.error("Request session failed") logger.error("Request session failed")
connect_status_label.configure(text="Request session failed") #connect_status_label.configure(text="Request session failed")
tab_control.select(connect_frame) #tab_control.select(connect_frame)
messagebox.showerror("Request session failed", "Could not request session") messagebox.showerror("Request session failed", "Could not request session")
# Connection success function # Connection success function
...@@ -59,7 +70,7 @@ def request_session_fail(): ...@@ -59,7 +70,7 @@ def request_session_fail():
# in the initial connection attempt # in the initial connection attempt
def connect_success(): def connect_success():
logger.info("Successfully connected to " + connect_url.get()) 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) tab_control.select(order_frame)
messagebox.showinfo("OK!", "Successfully connected") messagebox.showinfo("OK!", "Successfully connected")
...@@ -77,6 +88,7 @@ def connect(*args): ...@@ -77,6 +88,7 @@ def connect(*args):
# Request session # Request session
logger.debug("Got OK response from server") logger.debug("Got OK response from server")
if (new_session()): if (new_session()):
refresh_order_items()
connect_success() connect_success()
return return
request_session_fail() request_session_fail()
...@@ -124,43 +136,64 @@ def refresh_items(): ...@@ -124,43 +136,64 @@ def refresh_items():
return False return False
def order_item(item, people, stuffed_crust): 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(): class OrderItem():
def __init__(self, pizza_dict, col): def __init__(self, pizza_dict, row):
self.id = pizza_dict.id global order_frame
self.col = col print(pizza_dict)
self.label1 = ttk.Label(order_frame, text=pizza_dict.name) self.id = pizza_dict["id"]
self.label1.grid(column=col, row=1, sticky=(W, E)) self.row = row
self.label2 = ttk.Label(order_frame, text="£%.2f" % pizza_dict.costpp) self.label1 = ttk.Label(order_frame, text=pizza_dict["name"])
self.label2.grid(column=col, row=2, sticky=(W, E)) self.label1.grid(column=1, row=row, sticky=(W, E))
# TODO number of people self.label2 = ttk.Label(order_frame, text="£%.2f" % pizza_dict["cost_pp"])
self.quantity = IntVar(value=1) self.label2.grid(column=2, row=row, sticky=(W, E))
# self.quant_entry = ttk.Spinbox(order_frame, from=1, to=99) self.quantity = StringVar(value=1)
# TODO stuffed crust self.quant_entry = ttk.Spinbox(order_frame, from_=1, to=99, textvariable=self.quantity, validate="focusout", validatecommand=validate_int_command)
self.stuffed_crust = BooleanVar() 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 = 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): 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): def refresh_order_items(*args):
global order_frame global order_frame
if(refresh_items()): if(refresh_items()):
# Remove all items # Remove all items
for op_item in orderpage_items: for op_item in orderpage_items:
for op_childitem in op_item: op_item.destroy()
op_childitem.destroy()
del op_childitem
del op_item del op_item
col = 1 row = 2
for pizza in pizzas: for pizza in pizzas:
piz_label1 = ttk.Label(order_frame, text=pizza.name) orderpage_items.append(OrderItem(pizza, row))
piz_label1.grid(column=col, row=1, sticky=(W, E)) row += 1
piz_label2 = ttk.Label(order_frame, text="£%.2f" % pizza.costpp) configure_grid(order_frame)
piz_label2.grid(column=col, row=2, sticky=(W, E))
piz_orderbutton = ttk.Button(order_frame, text="Order", command=lambda: order_item())
## Main program code ## Main program code
# Window setup # Window setup
...@@ -168,28 +201,41 @@ root.title("Pizza Palace") ...@@ -168,28 +201,41 @@ root.title("Pizza Palace")
tab_control = ttk.Notebook(root) tab_control = ttk.Notebook(root)
# Connect tab # 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.set(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_status_label = ttk.Label(connect_frame, text="Not connected") #connect_status_label = ttk.Label(connect_frame, text="Not connected")
connect_status_label.grid(column=1, row=2, sticky=(W, E)) #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 = ttk.Button(connect_frame, text="Go!", command=connect)
connect_submit_button.grid(column=1, row=3, sticky=(W, E)) #connect_submit_button.grid(column=1, row=3, sticky=(W, E))
configure_grid(connect_frame) #configure_grid(connect_frame)
connect_submit_button.focus() #connect_submit_button.focus()
connect_frame.bind("<Return>", connect) #connect_frame.bind("<Return>", connect)
# Order tab # Order tab
order_frame = create_tab("Order") 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 tab
cart_frame = create_tab("Cart") cart_frame = create_tab("Cart")
...@@ -197,9 +243,11 @@ cart_frame = create_tab("Cart") ...@@ -197,9 +243,11 @@ cart_frame = create_tab("Cart")
# Confirm tab # Confirm tab
confirm_frame = create_tab("Confirm") confirm_frame = create_tab("Confirm")
# Pack # Pack
tab_control.pack(expand=1, fill="both") tab_control.pack(expand=1, fill="both")
# Connect
connect()
# Main window loop # Main window loop
root.mainloop() 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 register or to comment