Added view meal endpoint

master
Davide Bongiovanni 6 years ago
parent cdd6d2f1e3
commit 1e0668ad77

@ -82,6 +82,36 @@ def getUpcomingMeals():
r.close()
return meals
def getMeal(meal_id):
query = 'select foo.id, username as kuk, meal_date, array_agg as eaters, selection_deadline from (select meals.id, kuk, meal_date, selection_deadline, array_agg(username) from meals join users on users.id = any(meals.eaters) where meals.id=:meal_id group by meals.id order by meal_date) as foo join users on kuk=users.id;'
r = db_engine.execute(text(query), meal_id=meal_id)
meals = []
for row in r:
meals.append(dict(row))
r.close()
if len(meals) == 0:
return None
meal = meals[0]
seconds_to_deadline = (meal['selection_deadline'] - datetime.datetime.now()).total_seconds()
if seconds_to_deadline < 0:
meal['friendly_selection_deadline'] = ''
elif seconds_to_deadline > 3600:
hours_td = round(seconds_to_deadline / 3600)
days_td = int(hours_td / 24)
hours_td = hours_td % 24
meal['friendly_selection_deadline'] = str(days_td) + ' days ' + str(hours_td) + ' hours'
else:
meal['friendly_selection_deadline'] = str(round(seconds_to_deadline / 60)) + ' minutes'
meal['meal_time'] = meal['meal_date'].strftime('%H:%M')
meal['meal_friendly_date'] = meal['meal_date'].strftime('%A %d %B')
meal['candidate'] = 'davide'
if (meal['meal_date'] - datetime.datetime.now()).total_seconds() < 0:
meal['needs_confirmation'] = True
else:
meal['needs_confirmation'] = False
return meal
# adding to meal
@app.route("/addme")
@login_required
@ -102,6 +132,15 @@ def addme():
def index():
return render_template('meal_list.html', leaderboard=getLeaderboard(), meals=getUpcomingMeals(), now=datetime.datetime.now())
@app.route('/view_meal')
def view_meal():
meal_id = request.args.get('meal')
meal = getMeal(meal_id)
if meal == None:
return abort(400)
print(meal)
return render_template('index.html', leaderboard=getLeaderboard(), meal=meal)
@app.route('/login', methods=['GET', 'POST'])
def login():
if request.method == 'POST':

@ -39,9 +39,9 @@
<h2>{{meal.meal_friendly_date}}</h2>
{% endif %}
{% if meal.needs_confirmation %}
<div class="flex-card" onclick="location.href='/verify/{{meal.id}}';" style="cursor: pointer; width: 60%; justify-content: space-between;">
<div class="flex-card" onclick="location.href='/verify?meal={{meal.id}}';" style="cursor: pointer; width: 60%; justify-content: space-between;">
{% else %}
<div class="flex-card" onclick="location.href='/meal/{{meal.id}}';" style="cursor: pointer; width: 60%; justify-content: space-between;">
<div class="flex-card" onclick="location.href='/view_meal?meal={{meal.id}}';" style="cursor: pointer; width: 60%; justify-content: space-between;">
{% endif %}
<span{% if meal.needs_confirmation%} style="color: #EE7500;"{% endif %}><b>{{meal.meal_time}}</b></span>
<p style="text-align: left;">{% if meal.needs_confirmation %}Did &nbsp;{% endif %}{% if meal.kuk != "" %}<img class="flex-small-thumb" src="{{url_for('static', filename=meal.kuk + '.png')}}" title="{{meal.kuk}}" align="middle" style="margin: " /> &nbsp; {% if not meal.needs_confirmation %}<b>WILL</b>{% endif %}{% else %}<img class="flex-small-thumb" src="{{url_for('static', filename=meal.candidate + '.png')}}" title="{{meal.kuk}}" align="middle" style="margin: " /> &nbsp; {% if not meal.needs_confirmation %}<b>SHOULD</b>{% endif %}{% endif%} cook for <br>{% for e in meal.eaters %}<img class="flex-small-thumb" src="{{url_for('static', filename=e + '.png')}}" title="{{e}}" align="middle"/>&nbsp; {% endfor %}{% if meal.needs_confirmation %}<b>?</b>{% endif %}</p>

Loading…
Cancel
Save