diff --git a/client/pizza_palace.py b/client/pizza_palace.py index e0d50ea7f93e6d4a72f070fed460cf88165af21e..61c57bf06d7b79ecfa02d62464cabcccf39e4d87 100644 --- a/client/pizza_palace.py +++ b/client/pizza_palace.py @@ -267,10 +267,11 @@ def refresh_cart_items(): row += 1 configure_grid(cart_frame) -def confirm_order(address_vars: dict): - global cart_id, cart_key +def confirm_order(addressline1, addressline2, city, postcode): + global cart_id, cart_key, req_cart_items + required_vars = [addressline1, city, postcode] # Check all address fields are filled - for var in address_vars: + for var in required_vars: if (var.get() == ""): messagebox.showerror("Error", "Please fill in all address fields") return @@ -280,11 +281,13 @@ def confirm_order(address_vars: dict): return # Join address fields address = "" - for var in address_vars: - address += var.get() + ", " - address = address[:-2] + address += addressline1.get() + ", " + address += addressline2.get() + ", " + address += city.get() + ", " + address += postcode.get() # Confirm order reqjson = {"id": cart_id, "key": cart_key, "address": address} + logger.debug(reqjson) res = requests.post(get_endpoint("/cart/confirmorder"), json=reqjson) if (res.status_code == 200): req_cart_items = [] @@ -292,6 +295,8 @@ def confirm_order(address_vars: dict): new_session() tab_control.select(order_frame) messagebox.showinfo("OK!", "Order confirmed") + else: + messagebox.showerror("Error", "Failure to order: " + res.text) def tab_changed_handler(event): selected_tab = event.widget.select() @@ -389,7 +394,8 @@ confirm_label_4.grid(column=1, row=4, sticky=(W, E)) confirm_entry_4 = ttk.Entry(confirm_frame, width=20, textvariable=confirm_postcode) confirm_entry_4.grid(column=2, row=4, sticky=(W, E)) -confirm_button_1 = ttk.Button(confirm_frame, text="Confirm", command=lambda: confirm_order([confirm_addressline1, confirm_addressline2, confirm_city, confirm_postcode])) +confirm_button_1 = ttk.Button(confirm_frame, text="Confirm", command=lambda: confirm_order(confirm_addressline1, confirm_addressline2, confirm_city, confirm_postcode)) +confirm_button_1.grid(column=1, row=5, sticky=(W, E)) # Pack tab_control.pack(expand=1, fill="both") diff --git a/server/app.py b/server/app.py index 25b8922f90053ead31e04cfcb23ca8230ca776d0..1ade7a289e1762e16d1afd9726259eb4a14d6dee 100644 --- a/server/app.py +++ b/server/app.py @@ -211,10 +211,11 @@ def confirm_order_cart(): if (address == None): return gen_missing_message("address") # Test for invalid address and convert to dict - try: - address = json.loads(address) - except: - return gen_invalid_message("address") + #print(address) + #try: + # address = json.loads(address) + #except: + # return gen_invalid_message("address") # Build order order = { "address": address, @@ -224,9 +225,19 @@ def confirm_order_cart(): order_id = db.orders.insert_one(order).inserted_id # Post webhook webhook_data = { - "username": "PizzaBot", - "content": config.webhook_content_prepend + str(order_id) + "username": "PizzaBot" } + if config.webhook_discord: + # Post webhook + embed = config.webhook_dc_embed_json + for i in range(0, len(embed["fields"])): + for field_k, field_v in embed["fields"][i].items(): + embed["fields"][i][field_k] = str(field_v).replace("%ORDER_ID%", str(order_id)) + embed["fields"][i][field_k] = str(field_v).replace("%ADDRESS%", str(address)) + embed["fields"][i][field_k] = str(field_v).replace("%ITEMS%", str(cart["items"])) + webhook_data["embeds"] = [embed] + else: + webhook_data["content"] = config.webhook_content_prepend + str(order_id) try: webhook_response = requests.post(config.webhook_url, json=webhook_data) if (webhook_response.status_code != 200): diff --git a/server/config.py.example b/server/config.py.example index 567fdc4ed523d23764757cd26fcd5865788d4102..d751ba08a44cb8677636ead42ba5ef0beb2a5e7c 100644 --- a/server/config.py.example +++ b/server/config.py.example @@ -1,5 +1,30 @@ webhook = "https://example.com/webhook" webhook_content_prepend = "New order received! Order ID: " +webhook_discord = True # If true, will use discord embeds below instead of prepend +webhook_dc_embed_json = { + "author": { + "name": "PizzaPalace" + }, + "title": "New Order!", + "fields": [ + { + "name": "Order ID", + "value": "%ORDER_ID%" + }, + { + "name": "Address", + "value": "%ADDRESS%" + }, + { + "name": "Items", + "value": "%ITEMS%" + } + ], + "color": "#00b0f4", + "footer": { + "text": "PizzaPalace by TheJoeCoder" + } +} delivery_flatrate = 0.0 mongo_uri = "mongodb://localhost:27017/" mongo_db = "pizza_palace" \ No newline at end of file