diff --git a/client/pizza_palace.py b/client/pizza_palace.py index 951e6ede8e5ad1890561f5f50b75a225e4d68ba5..7727120ed98107d6b29f36126ffa0d5a81f3e45e 100644 --- a/client/pizza_palace.py +++ b/client/pizza_palace.py @@ -17,6 +17,8 @@ pizzas = [] cart_id = "" cart_key = "" +orderpage_items = [] + ## Subroutines def get_endpoint(endpoint): return connect_url.get() + endpoint @@ -45,7 +47,14 @@ def connect_fail(): tab_control.select(connect_frame) messagebox.showerror("Connection failed", "Could not connect to server") -# Connection success fail function +# 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) + messagebox.showerror("Request session failed", "Could not request session") + +# Connection success function # Used when the server is reachable and all is ok # in the initial connection attempt def connect_success(): @@ -65,7 +74,12 @@ def connect(*args): try: res_json = json.loads(res.text) if (res_json["status"] == 200 and res_json["message"] == "ok"): - connect_success() + # Request session + logger.debug("Got OK response from server") + if (new_session()): + connect_success() + return + request_session_fail() return else: logger.debug("Response from server did not contain OK") @@ -87,27 +101,66 @@ def connect(*args): # Create new session def new_session(*arg): global cart_id, cart_key - res = requests.post(get_endpoint("/cart/new")) + res = requests.post(get_endpoint("/cart/open")) if (res.status_code == 200): res_json = res.json() if (res_json != None and res_json["status"] == 200): cart_id, cart_key = res_json["id"], res_json["key"] return True else: - connect_fail() return False else: - connect_fail() return False # Refresh order screen items -def refresh_items(*args): +def refresh_items(): global pizzas res = requests.get(get_endpoint("/pizzas")) if (res.status_code == 200 and res.json() != None): pizzas = res.json() + return True else: connect_fail() + return False + +def order_item(item, people, stuffed_crust): + pass + +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() + self.order_butt = ttk.Button(order_frame, text="Order", command=self.order) + self.order_butt.grid(column=col, row=4, sticky=(W, E)) + def order(self): + order_item(self.id, 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 + del op_item + col = 1 + 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()) + ## Main program code # Window setup @@ -136,7 +189,7 @@ connect_frame.bind("<Return>", connect) # Order tab order_frame = create_tab("Order") -# TODO order list +refresh_order_items() # Cart tab cart_frame = create_tab("Cart")