in cloud-run-cloud-sql/main.py [0:0]
def save_vote():
# Get the team and time the vote was cast.
team = request.form['team']
time_cast = datetime.datetime.utcnow()
# Verify that the team is one of the allowed options
if team != "TABS" and team != "SPACES":
logger.warning(team)
return Response(
response="Invalid team specified.",
status=400
)
stmt = sqlalchemy.text(
"INSERT INTO votes (time_cast, candidate)"
" VALUES (:time_cast, :candidate)"
)
totals_stmt = sqlalchemy.text(
"UPDATE totals SET num_votes = num_votes + 1 WHERE candidate=:candidate"
)
try:
with db.connect() as conn:
conn.execute(stmt, time_cast=time_cast, candidate=team)
conn.execute(totals_stmt, candidate=team)
except Exception as e:
logger.exception(e)
return Response(
status=500,
response="Unable to successfully cast vote! Please check the "
"application logs for more details."
)
return Response(
status=200,
response="Vote successfully cast for '{}' at time {}!".format(
team, time_cast)
)