Merge branch 'master' of git.elab.kth.se:davide/KUKapp

# Conflicts:
#	kukv1.py
master
Davide Bongiovanni 6 years ago
commit 50c01cdf9d

@ -4,6 +4,7 @@ import json
import datetime import datetime
from flask import Flask, Response, redirect, url_for, request, session, abort, render_template from flask import Flask, Response, redirect, url_for, request, session, abort, render_template
from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user, current_user from flask_login import LoginManager, UserMixin, login_required, login_user, logout_user, current_user
import sqlalchemy
from sqlalchemy.sql import select, text from sqlalchemy.sql import select, text
from dateutil.parser import parse from dateutil.parser import parse
app = Flask(__name__) app = Flask(__name__)
@ -19,10 +20,20 @@ db_engine = {}
db_metadata = {} db_metadata = {}
meals = {} meals = {}
def connect(user, password, db, host='localhost', port=5432):
url = 'postgresql://{}:{}@{}:{}/{}'
url = url.format(user, password, host, port, db)
con = sqlalchemy.create_engine(url, client_encoding='utf8')
meta = sqlalchemy.MetaData(bind=con, reflect=True)
return con, meta
def getLeaderboard(): def getLeaderboard():
leaders = [] s = 'select u.username as name, u.score as score, max(m.meal_date) as last_meal from users as u inner join meals as m on u.id = m.kuk where m.kuk>0 and m.status>0 group by u.id order by u.score, last_meal;'
leaders.append({'name':'marek', 'score':34}) r = db_engine.execute(text(s))
leaders.append({'name':'sven', 'score':16}) leaders=[]
for row in r:
leaders.append(dict(row))
r.close()
return leaders return leaders
def getUpcomingMeals(): def getUpcomingMeals():
@ -54,9 +65,10 @@ def index():
print (getUpcomingMeals()) print (getUpcomingMeals())
return render_template('meal_list.html', leaderboard=getLeaderboard(), meals=getUpcomingMeals(), current_user_name='Wille') return render_template('meal_list.html', leaderboard=getLeaderboard(), meals=getUpcomingMeals(), current_user_name='Wille')
if __name__ == '__main__': with open('admin.json') as f:
with open('admin.json') as f:
postgres_credentials = json.load(f) postgres_credentials = json.load(f)
db_engine, db_metadata = connect(postgres_credentials['username'], postgres_credentials['password'], 'kuk_app') db_engine, db_metadata = connect(postgres_credentials['username'], postgres_credentials['password'], 'kuk_app')
meals = sqlalchemy.Table('meals', db_metadata) meals = sqlalchemy.Table('meals', db_metadata)
if __name__ == '__main__':
app.run('0.0.0.0') app.run('0.0.0.0')
Loading…
Cancel
Save