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

(Nearly) finish server

parent 3eea25ce
Branches
No related tags found
No related merge requests found
...@@ -5,8 +5,8 @@ GCSE Computer Science Challenge ...@@ -5,8 +5,8 @@ GCSE Computer Science Challenge
``` ```
git clone https://github.com/TheJoeCoder/PizzaPalace git clone https://github.com/TheJoeCoder/PizzaPalace
cd PizzaPalace cd PizzaPalace
pip install -r requirements.txt
cd server cd server
pip install -r requirements.txt
cp config.py.example config.py cp config.py.example config.py
python app.py python app.py
``` ```
\ No newline at end of file
flask==2.3.2
\ No newline at end of file
import json, uuid, logging import json, uuid, logging, requests
from flask import Flask, request, jsonify, Response from flask import Flask, request, jsonify, Response
from pymongo import MongoClient
logger = logging.Logger(__name__) logger = logging.Logger(__name__)
...@@ -7,6 +8,11 @@ app = Flask(__name__) ...@@ -7,6 +8,11 @@ app = Flask(__name__)
import config import config
# Connect to MongoDB
client = MongoClient(config.mongo_uri)
db = client[config.mongo_db]
with open("pizza_types.json","r") as f: with open("pizza_types.json","r") as f:
pizza_types = json.loads(f.read()) pizza_types = json.loads(f.read())
...@@ -158,7 +164,7 @@ def get_cart_total(): ...@@ -158,7 +164,7 @@ def get_cart_total():
grand_total = subtotal + config.delivery_flatrate grand_total = subtotal + config.delivery_flatrate
return "{\"status\":200,\"message\":\"ok\",\"subtotal\":" + str(subtotal) + ",\"delivery_rate\":" + str(config.delivery_flatrate) + ",\"total\":" + str(grand_total) + ",\"breakdown\":" + json.dumps(breakdown) + "}", 200 return "{\"status\":200,\"message\":\"ok\",\"subtotal\":" + str(subtotal) + ",\"delivery_rate\":" + str(config.delivery_flatrate) + ",\"total\":" + str(grand_total) + ",\"breakdown\":" + json.dumps(breakdown) + "}", 200
@app.route("/cart/confirmorder") @app.route("/cart/confirmorder", methods=["POST"])
def confirm_order_cart(): def confirm_order_cart():
# Get cart id and key from request # Get cart id and key from request
cart_id = request.args.get("id") cart_id = request.args.get("id")
...@@ -175,8 +181,25 @@ def confirm_order_cart(): ...@@ -175,8 +181,25 @@ def confirm_order_cart():
address = json.loads(address) address = json.loads(address)
except: except:
return gen_invalid_message("address") return gen_invalid_message("address")
# TODO place order and clear cart # Build order
return "{\"status\":500,\"message\":\"not implemented\"}" order = {
"address": address,
"items": cart["items"]
}
# Add order to database
order_id = db.orders.insert_one(order).inserted_id
# Post webhook
webhook_data = {
"username": "PizzaBot",
"content": config.webhook_content_prepend + str(order_id)
}
webhook_response = requests.post(config.webhook_url, json=webhook_data)
if (webhook_response.status_code != 200):
logger.error("Webhook failed with status code " + str(webhook_response.status_code))
# Remove cart from existing carts
del open_carts[cart_id]
# Return order ID
return "{\"status\":200,\"message\":\"OK\", \"code\":\"" + str(order_id) + "\"}", 200
if (__name__ == "__main__"): if (__name__ == "__main__"):
app.run(debug=True, host="0.0.0.0") app.run(debug=True, host="0.0.0.0")
\ No newline at end of file
webhook = "https://example.com/webhook" webhook = "https://example.com/webhook"
webhook_content_prepend = "New order received! Order ID: "
delivery_flatrate = 0 delivery_flatrate = 0
mongo_uri = "mongodb://localhost:27017/"
mongo_db = "pizza_palace"
\ No newline at end of file
flask==2.3.2
pymongo[srv]==4.3.3
requests==2.29.0
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment