-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
221 lines (166 loc) · 6.2 KB
/
app.py
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
from enum import IntEnum
from typing import Dict, List, Tuple
from uuid import uuid4
from flask import (
Flask,
current_app,
flash,
jsonify,
redirect,
render_template,
request,
url_for,
)
from flask_sqlalchemy import SQLAlchemy
from sqlalchemy.exc import IntegrityError, NoResultFound
app = Flask("coffee")
app.config.from_pyfile("coffee.cfg")
app.config.from_prefixed_env("COFFEE")
app.template_folder = "templates"
app.static_folder = "static"
sa = SQLAlchemy(app)
if app.config.get("SECRET_KEY", None) is None:
raise RuntimeError("The application config has no SECRET_KEY value")
if app.config.get("ADMIN_TOKEN", None) is None:
token = str(uuid4())
print(f"admin token: {token}")
app.config["ADMIN_TOKEN"] = token
class VoteEnum(IntEnum):
Downvote = 0
Upvote = 1
class CoffeeVariant(sa.Model):
__tablename__ = "coffee_variants"
id: sa.Mapped[int] = sa.mapped_column(primary_key=True)
name: sa.Mapped[str] = sa.mapped_column(sa.String(), unique=True)
votes: sa.Mapped[List["Vote"]] = sa.relationship(back_populates="variant")
class Vote(sa.Model):
__tablename__ = "votes"
id: sa.Mapped[int] = sa.mapped_column(primary_key=True)
variant_id: sa.Mapped[int] = sa.mapped_column(sa.ForeignKey("coffee_variants.id"))
variant: sa.Mapped[CoffeeVariant] = sa.relationship(back_populates="votes")
name: sa.Mapped[str] = sa.mapped_column(sa.String())
vote: sa.Mapped[VoteEnum] = sa.mapped_column(sa.Integer)
def _get_votes() -> Dict[str, List[Vote]]:
votes = sa.session.execute(sa.select(Vote)).scalars()
variants = sa.session.execute(sa.select(CoffeeVariant)).scalars()
votes_dict = {v.name: [] for v in variants}
for vote in votes:
votes_dict[vote.variant.name].append(vote)
return votes_dict
def _add_vote(coffee_variant: str, name: str, vote: VoteEnum) -> Tuple[bool, str]:
try:
votes = _get_votes()
for existing_vote in votes[coffee_variant]:
if existing_vote.name.lower() == name.lower():
sa.session.rollback()
return False, "no double votes!"
variant = sa.session.execute(
sa.select(CoffeeVariant).filter(CoffeeVariant.name == coffee_variant)
).scalar_one()
v = Vote(variant=variant, name=name, vote=vote)
sa.session.add(v)
sa.session.commit()
return True, "thanks for voting!"
except NoResultFound:
return False, "coffee variant not found!"
except Exception as e:
current_app.logger.error(e)
return False, "unexpected error"
@app.template_filter()
def summarize_votes(vote_list: List[Vote]) -> str:
upvotes = [v for v in vote_list if v.vote == VoteEnum.Upvote]
downvotes = [v for v in vote_list if v.vote == VoteEnum.Downvote]
return f"+{len(upvotes)} / -{len(downvotes)}"
@app.template_filter()
def display_vote(vote: VoteEnum) -> str:
value = "unknown"
if vote == VoteEnum.Upvote:
value = "like"
elif vote == VoteEnum.Downvote:
value = "dislike"
return f"<span class='vote {value}'>{value}</span>"
@app.route("/")
def show_overview():
votes = _get_votes()
return render_template("overview.jinja", votes=votes)
@app.route("/vote/<string:coffee_variant>", methods=["GET", "POST"])
def vote_form(coffee_variant: str):
if request.method == "POST":
success = True
try:
values = request.values.to_dict()
vote = VoteEnum(int(values["vote"]))
name = values["name"]
success, message = _add_vote(coffee_variant, name, vote)
if success:
flash(message, "message")
return redirect(url_for("show_overview"))
else:
flash(message, "error")
except KeyError as e:
flash(f"missing the required value for {e}", "error")
except ValueError:
flash("unexpected value for the vote", "error")
except Exception as e:
current_app.logger.error(e)
flash("unexpected error", "error")
return (
render_template("vote_form.jinja", coffee_variant=coffee_variant),
400,
)
else:
return render_template("vote_form.jinja", coffee_variant=coffee_variant)
@app.route("/api/export")
def export():
votes = {
variant: {
vote.name: "+" if vote.vote == VoteEnum.Upvote else "-" for vote in votes
}
for variant, votes in _get_votes().items()
}
return jsonify(votes)
@app.route("/api/variants", methods=["POST"])
def add_coffee_variant():
try:
values = request.values.to_dict()
token = request.values.get("token", None)
if token is None:
return jsonify({"message": "token required!"}), 401
elif token != current_app.config["ADMIN_TOKEN"]:
return jsonify({"message": "unrecognized token!"}), 403
name = values["name"]
variant = CoffeeVariant(name=name)
sa.session.add(variant)
sa.session.commit()
return jsonify({"message": "added variant!"})
except KeyError as e:
app.logger.error(e)
return jsonify({"message": f"missing the required value for {e}"}), 400
except IntegrityError as e:
app.logger.error(e)
return jsonify({"message": "variant already exists!"}), 403
except Exception as e:
app.logger.error(e)
return jsonify({"message": e}), 400
@app.route("/api/vote", methods=["POST"])
def add_vote():
try:
values = request.values.to_dict()
variant = values["variant"]
vote = VoteEnum(int(values["vote"]))
name = values["name"]
success, message = _add_vote(variant, name, vote)
return jsonify({"message": message}), 200 if success else 403
except KeyError as e:
app.logger.error(e)
return jsonify({"message": f"missing the required value for {e}"}), 400
except ValueError as e:
app.logger.error(e)
return jsonify({"message": "unexpected value for the vote"}), 400
except Exception as e:
app.logger.error(e)
return jsonify({"message": e}), 400
with app.app_context():
sa.create_all()
if __name__ == "__main__":
app.run()