-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathserver.py
More file actions
260 lines (227 loc) · 9.64 KB
/
server.py
File metadata and controls
260 lines (227 loc) · 9.64 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
import os, io
from datetime import datetime
from flask import Flask, render_template, request, jsonify, send_file, session, redirect, url_for, request
from store import (get_catalog, get_settings, save_settings,
add_ingredient, delete_ingredient, update_ingredient_price,
add_cocktail, delete_cocktail)
from core import calculate, validate_menu, ALCOHOL_LEVELS
from authlib.integrations.flask_client import OAuth
from dotenv import load_dotenv
from functools import wraps
load_dotenv()
app = Flask(__name__)
app.secret_key = os.environ.get("SECRET_KEY", "dev-secret")
oauth = OAuth(app)
google = oauth.register(
name="google",
client_id=os.environ.get("GOOGLE_CLIENT_ID"),
client_secret=os.environ.get("GOOGLE_CLIENT_SECRET"),
server_metadata_url="https://accounts.google.com/.well-known/openid-configuration",
client_kwargs={"scope": "openid email profile"},
)
# ── Auth routes ───────────────────────────────────────────────
@app.route("/login")
def login():
redirect_uri = url_for("auth_callback", _external=True)
return google.authorize_redirect(redirect_uri)
@app.route("/auth/callback")
def auth_callback():
token = google.authorize_access_token()
userinfo = token["userinfo"]
session["user_id"] = userinfo["sub"] # Google's unique user ID
session["email"] = userinfo["email"]
session["name"] = userinfo.get("name", "")
session["picture"] = userinfo.get("picture", "")
return redirect("/")
@app.route("/logout")
def logout():
session.clear()
return redirect("/login")
# ── Auth guard — add this before every protected route ────────
def login_required(f):
"""Redirects to /login for page routes."""
@wraps(f)
def decorated(*args, **kwargs):
if "user_id" not in session:
return redirect(url_for("login"))
return f(*args, **kwargs)
return decorated
def api_login_required(f):
"""Returns JSON error for API/CRUD routes."""
@wraps(f)
def decorated(*args, **kwargs):
if "user_id" not in session:
return jsonify({
"ok": False,
"error": "Authentication required",
"message": "You must be signed in to perform this action.",
"login_url": "/login"
}), 401
return f(*args, **kwargs)
return decorated
# ── PAGES ────────────────────────────────────────────────────
@app.route("/")
def dashboard():
user_id = session.get("user_id")
settings = get_settings(user_id)
catalog = get_catalog(user_id)
errors = validate_menu(settings["menu"])
result = calculate(settings, catalog) if not errors else None
return render_template("dashboard.html",
settings=settings, result=result,
errors=errors, levels=ALCOHOL_LEVELS)
@app.route("/menu")
def menu():
user_id = session.get("user_id")
settings = get_settings(user_id)
catalog = get_catalog(user_id)
errors = validate_menu(settings["menu"])
return render_template("menu.html",
settings=settings, catalog=catalog, errors=errors)
@app.route("/catalog")
def catalog_page():
user_id = session.get("user_id")
catalog = get_catalog(user_id)
return render_template("catalog.html", catalog=catalog)
# ── API: global settings ──────────────────────────────────────
@app.route("/api/settings", methods=["POST"])
@api_login_required
def api_settings():
user_id = session["user_id"]
s = get_settings(user_id)
for key in ["guests", "ticket_price", "venue_cost", "equipment_cost",
"alcohol_ml_per_person", "buffer"]:
if key in request.json:
s[key] = request.json[key]
save_settings(user_id, s)
return jsonify({"ok": True})
# ── API: save 3-level menu ────────────────────────────────────
@app.route("/api/menu", methods=["POST"])
@api_login_required
def api_menu():
user_id = session["user_id"]
s = get_settings(user_id)
s["menu"] = request.json["menu"]
# structural=True -> skip validation (adding/removing spirits or drinks)
# structural=False -> validate sums (explicit Save Menu button only)
if not request.json.get("structural", False):
errors = validate_menu(s["menu"])
if errors:
return jsonify({"ok": False, "errors": errors}), 400
save_settings(user_id, s)
return jsonify({"ok": True})
# ── API: recalculate (AJAX) ───────────────────────────────────
@app.route("/api/calculate", methods=["POST"])
def api_calculate():
user_id = session.get("user_id")
s = get_settings(user_id)
c = get_catalog(user_id)
errors = validate_menu(s["menu"])
if errors:
return jsonify({"ok": False, "errors": errors}), 400
return jsonify({"ok": True, "result": calculate(s, c)})
# ── API: ingredients CRUD ─────────────────────────────────────
@app.route("/api/catalog/ingredient", methods=["POST"])
@api_login_required
def add_ingredient_route():
user_id = session["user_id"]
data = request.json
name = data.get("name", "").strip()
catalog = get_catalog(user_id)
if not name or name in catalog["ingredients"]:
return jsonify({"ok": False, "error": "Name missing or already exists"}), 400
vol = data.get("volume_ml")
add_ingredient(user_id, name, {
"type": data.get("type", "extra"),
"abv": float(data.get("abv", 0)),
"volume_ml": int(vol) if vol else None,
"unit": data.get("unit", "pcs"),
"price_min": float(data.get("price_min", 0)),
"price_max": float(data.get("price_max", 0)),
})
return jsonify({"ok": True})
@app.route("/api/catalog/ingredient/price", methods=["POST"])
@api_login_required
def update_ingredient_price_route():
user_id = session["user_id"]
data = request.json
name = data.get("name", "").strip()
catalog = get_catalog(user_id)
if not name or name not in catalog["ingredients"]:
return jsonify({"ok": False, "error": "Ingredient not found"}), 404
price_min = float(data.get("price_min", 0))
price_max = float(data.get("price_max", 0))
if price_min > price_max:
return jsonify({"ok": False, "error": "Min price cannot exceed max price"}), 400
update_ingredient_price(user_id, name, price_min, price_max)
return jsonify({"ok": True})
@app.route("/api/catalog/ingredient/<name>", methods=["DELETE"])
@api_login_required
def delete_ingredient_route(name):
user_id = session["user_id"]
delete_ingredient(user_id, name)
return jsonify({"ok": True})
# ── API: cocktails CRUD ───────────────────────────────────────
@app.route("/api/catalog/cocktail", methods=["POST"])
@api_login_required
def add_cocktail_route():
user_id = session["user_id"]
data = request.json
name = data.get("name", "").strip()
catalog = get_catalog(user_id)
if not name or name in catalog["cocktails"]:
return jsonify({"ok": False, "error": "Name missing or already exists"}), 400
add_cocktail(user_id, name, {
"main_spirit": data["main_spirit"],
"category": data["category"],
"recipe": data["recipe"],
})
return jsonify({"ok": True})
@app.route("/api/catalog/cocktail/<name>", methods=["DELETE"])
@api_login_required
def delete_cocktail_route(name):
user_id = session["user_id"]
delete_cocktail(user_id, name)
return jsonify({"ok": True})
# ── API: export shopping list as TXT ─────────────────────────
@app.route("/api/export")
def export_txt():
user_id = session.get("user_id")
s = get_settings(user_id)
c = get_catalog(user_id)
r = calculate(s, c)
lines = [
"=" * 62,
f" PARTY BUDGET - {s['guests']} GUESTS",
"=" * 62,
f" Ticket price : E{s['ticket_price']}",
f" Venue cost : E{s['venue_cost']}",
f" Equipment : E{s['equipment_cost']}",
f" Revenue : E{r['revenue']}",
f" Profit : E{r['profit_min']} - E{r['profit_max']}",
f" Break-even : {r['break_even']} guests",
"",
"=" * 62,
" SHOPPING LIST",
"=" * 62,
f" {'Type':<8} {'Item':<26} {'Qty':>5} {'Unit':<6} {'Min E':>7} {'Max E':>7}",
" " + "-" * 60,
]
for item in r["shopping_list"]:
lines.append(
f" {item['type']:<8} {item['name']:<26} {item['quantity']:>5} "
f"{item['unit']:<6} {item['cost_min']:>7.2f} {item['cost_max']:>7.2f}"
)
lines += [
" " + "-" * 60,
f" TOTAL SPEND : E{r['total_min']} - E{r['total_max']}",
f" TOTAL (incl. fixed): E{r['total_min'] + r['fixed_costs']} - E{r['total_max'] + r['fixed_costs']}",
f"\n Generated on {datetime.now().strftime('%d/%m/%Y %H:%M')}",
]
buf = io.BytesIO("\n".join(lines).encode("utf-8"))
buf.seek(0)
filename = f"shopping_list_{datetime.now().strftime('%Y%m%d')}.txt"
return send_file(buf, as_attachment=True, download_name=filename, mimetype="text/plain")
if __name__ == "__main__":
port = int(os.environ.get("PORT", 5000))
app.run(host="0.0.0.0", port=port, debug=False)