Added view meal endpoint

master
Davide Bongiovanni 6 years ago
parent cdd6d2f1e3
commit 1e0668ad77

@ -82,6 +82,36 @@ def getUpcomingMeals():
r.close() r.close()
return meals 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 # adding to meal
@app.route("/addme") @app.route("/addme")
@login_required @login_required
@ -102,31 +132,40 @@ def addme():
def index(): def index():
return render_template('meal_list.html', leaderboard=getLeaderboard(), meals=getUpcomingMeals(), now=datetime.datetime.now()) 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']) @app.route('/login', methods=['GET', 'POST'])
def login(): def login():
if request.method == 'POST': if request.method == 'POST':
username = request.form['username'] username = request.form['username']
password = request.form['password'] password = request.form['password']
r = db_engine.execute(text('select id from users where username=:username and password=:password'), username=username, password=password) r = db_engine.execute(text('select id from users where username=:username and password=:password'), username=username, password=password)
result = [] result = []
for row in r: for row in r:
result.append(dict(row)) result.append(dict(row))
r.close() r.close()
print(len(users)) print(len(users))
if len(result) > 0: if len(result) > 0:
u = User.get(username) u = User.get(username)
u.user_id = result[0]['id'] u.user_id = result[0]['id']
login_user(u) login_user(u)
return redirect(request.args.get('next') or url_for('index')) return redirect(request.args.get('next') or url_for('index'))
return abort(401) return abort(401)
else: else:
return Response(''' return Response('''
<form action="" method="post"> <form action="" method="post">
<p><input type=text name=username> <p><input type=text name=username>
<p><input type=password name=password> <p><input type=password name=password>
<p><input type=submit value=Login> <p><input type=submit value=Login>
</form> </form>
''') ''')
with open('admin.json') as f: with open('admin.json') as f:
postgres_credentials = json.load(f) postgres_credentials = json.load(f)
@ -141,18 +180,18 @@ login_manager.login_view = "login"
@app.route("/logout") @app.route("/logout")
@login_required @login_required
def logout(): def logout():
logout_user() logout_user()
return Response('<p>Logged out</p>') return Response('<p>Logged out</p>')
# handle login failed # handle login failed
@app.errorhandler(401) @app.errorhandler(401)
def page_not_found(e): def page_not_found(e):
return Response('<p>Login failed</p>') return Response('<p>Login failed</p>')
# callback to reload the user object # callback to reload the user object
@login_manager.user_loader @login_manager.user_loader
def load_user(userid): def load_user(userid):
return User.get(userid) return User.get(userid)
if __name__ == '__main__': if __name__ == '__main__':
app.run('0.0.0.0') app.run('0.0.0.0')

@ -39,9 +39,9 @@
<h2>{{meal.meal_friendly_date}}</h2> <h2>{{meal.meal_friendly_date}}</h2>
{% endif %} {% endif %}
{% if meal.needs_confirmation %} {% 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 %} {% 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 %} {% endif %}
<span{% if meal.needs_confirmation%} style="color: #EE7500;"{% endif %}><b>{{meal.meal_time}}</b></span> <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> <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