From 0f6b449bf0d7e7355ecd2445f97aaa16739a8e21 Mon Sep 17 00:00:00 2001 From: Davide Bongiovanni Date: Fri, 30 Nov 2018 20:55:33 +0100 Subject: [PATCH] Calculating friendly timedelta string --- kukv1.py | 15 ++++++++++++--- 1 file changed, 12 insertions(+), 3 deletions(-) diff --git a/kukv1.py b/kukv1.py index 6a7e437..88707d1 100644 --- a/kukv1.py +++ b/kukv1.py @@ -36,13 +36,22 @@ def getUpcomingMeals(): query = 'select username as kuk, meal_date, array_agg as eaters, selection_deadline from (select kuk, meal_date, selection_deadline, array_agg(username) from meals join users on users.id = any(meals.eaters) where meals.status=0 group by meals.id order by meal_date) as foo join users on kuk=users.id;' r = db_engine.execute(text(query)) meals = [] - for row in r: - meals.append(dict(row)) - r.close() + for row in r: + meals.append(dict(row)) + seconds_to_deadline = (parse(meals[-1]['selection_deadline']) - datetime.datetime.now()).seconds + if seconds_to_deadline < 0: + meals[-1]['selection_deadline'] = 'Deadline passed' + else: + hours_td = round(seconds_to_deadline / 60 / 60) + days_td = hours_td / 24 + hours_td = hours_td % 24 + meals[-1]['selection_deadline'] = str(days_td) + ' days ' + str(hours_td) + r.close() return meals @app.route('/') def index(): + print (getUpcomingMeals()) return render_template('meal_list.html', leaderboard=getLeaderboard(), meals=getUpcomingMeals(), current_user_name='Wille') if __name__ == '__main__':