IMPORTANT REFACTOR

master
Davide Bongiovanni 6 years ago
parent d0ff0ceb46
commit b1d8323c30

@ -60,7 +60,7 @@ def getUpcomingMeals():
# meals.append({'id':2, 'kuk':'marek', 'eaters':['davide','sven','david', 'wille'], 'flavor_text':'Grzybowa', 'date':'Sunday 25rd of November', 'selection_deadline':'3 Days 23 hours', 'status':0}) # meals.append({'id':2, 'kuk':'marek', 'eaters':['davide','sven','david', 'wille'], 'flavor_text':'Grzybowa', 'date':'Sunday 25rd of November', 'selection_deadline':'3 Days 23 hours', 'status':0})
# meals.append({'id':2, 'kuk':'marek', 'eaters':['davide','sven','david', 'wille'], 'flavor_text':'Grzybowa', 'date':'Sunday 25rd of November', 'selection_deadline':'3 Days 23 hours', 'status':0}) # meals.append({'id':2, 'kuk':'marek', 'eaters':['davide','sven','david', 'wille'], 'flavor_text':'Grzybowa', 'date':'Sunday 25rd of November', 'selection_deadline':'3 Days 23 hours', 'status':0})
# meals.append({'id':2, 'kuk':'marek', 'eaters':['davide','sven','david', 'wille'], 'flavor_text':'Grzybowa', 'date':'Monday 26rd of November', 'selection_deadline':'3 Days 23 hours', 'status':0}) # meals.append({'id':2, 'kuk':'marek', 'eaters':['davide','sven','david', 'wille'], 'flavor_text':'Grzybowa', 'date':'Monday 26rd of November', 'selection_deadline':'3 Days 23 hours', 'status':0})
query = 'select foo.id, username as kuk, meal_date, array_agg as eaters, selection_deadline, is_lazy from (select is_lazy, meals.id, 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;' query = 'SELECT foo.id, username AS kuk, meal_date, array_agg AS eaters, selection_deadline, is_lazy FROM (SELECT is_lazy, meals.id, 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)) r = db_engine.execute(text(query))
meals = [] meals = []
leaderboard = getLeaderboard() leaderboard = getLeaderboard()
@ -109,7 +109,7 @@ def applyTempMeal(leaderboard, meal):
return sorted(ld, key=lambda i:(i['score'], i['last_meal'] or datetime.datetime(1970, 1, 1, 0, 0))) return sorted(ld, key=lambda i:(i['score'], i['last_meal'] or datetime.datetime(1970, 1, 1, 0, 0)))
def getMeal(meal_id): 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;' 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) r = db_engine.execute(text(query), meal_id=meal_id)
meals = [] meals = []
for row in r: for row in r:
@ -143,7 +143,7 @@ def getMeal(meal_id):
@login_required @login_required
def addme(): def addme():
meal_id = request.args.get('meal') meal_id = request.args.get('meal')
query = 'select :user_id = any (eaters) as is_eater from meals where id =:meal_id;' query = 'SELECT :user_id = any (eaters) AS is_eater FROM meals WHERE id =:meal_id;'
r = db_engine.execute(text(query), user_id=current_user.user_id, meal_id = meal_id) r = db_engine.execute(text(query), user_id=current_user.user_id, meal_id = meal_id)
results = [] results = []
for row in r: for row in r:
@ -158,7 +158,7 @@ def addme():
@login_required @login_required
def removeme(): def removeme():
meal_id = request.args.get('meal') meal_id = request.args.get('meal')
query = 'UPDATE meals SET eaters = array_remove(eaters, :user_id) where id = :meal_id;' query = 'UPDATE meals SET eaters = array_remove(eaters, :user_id) WHERE id = :meal_id;'
r = db_engine.execute(text(query), user_id=current_user.user_id, meal_id = meal_id) r = db_engine.execute(text(query), user_id=current_user.user_id, meal_id = meal_id)
return render_template('appresponse.html', message='You were removed from that meal') return render_template('appresponse.html', message='You were removed from that meal')
@ -186,7 +186,7 @@ def meal_planner():
def search_users(): def search_users():
q = request.args.get('q') q = request.args.get('q')
s = request.args.get('s') s = request.args.get('s')
query = 'select username from users where id>0 and username like :q;' query = 'SELECT username FROM users WHERE id>0 AND username LIKE :q;'
if s == '1': if s == '1':
r = db_engine.execute(text(query), q=(q+'%')) r = db_engine.execute(text(query), q=(q+'%'))
else: else:

Loading…
Cancel
Save