-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
94 lines (66 loc) · 2.54 KB
/
app.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
import os
from flask import Flask, render_template, session, g
from routes.list import list
from routes.auth import auth
from routes.user import user
from flask_debugtoolbar import DebugToolbarExtension
from models import connect_db, db, User, List, likes
# from secret import API_KEY_CONFIG
from sqlalchemy import desc, func
CURR_USER_KEY = "curr_user"
app = Flask(__name__)
with app.app_context():
app.register_blueprint(list)
app.register_blueprint(auth)
app.register_blueprint(user)
# for production, API_KEY_CONFIG as 2nd argument
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY')
# debug = DebugToolbarExtension(app)
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL', 'postgresql:///whichani')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
app.config['SQLALCHEMY_ECHO'] = True
connect_db(app)
######################################################
# Session handler
######################################################
@app.before_request
def add_user_to_g():
"""If logged in, add user to g."""
if CURR_USER_KEY in session:
g.user = User.query.get(session[CURR_USER_KEY])
else:
g.user = None
######################################################
# Home
######################################################
@app.route('/', methods=["GET"])
def home():
"""Render homepage based on login status. Also load top liked lists."""
list_alias = db.aliased(List)
likes_alias = db.aliased(likes)
top_lists = (db.session.query(list_alias)
.join(likes_alias, list_alias.list_id == likes_alias.c.list_id)
.group_by(list_alias.list_id)
.order_by(desc(db.func.count(likes_alias.c.user_id)))
.limit(10)
.all())
top_list_with_likes = []
for list in top_lists:
tmp = []
if len(list.listings) != 0:
like_count = db.session.query(func.count(likes.c.user_id)).filter(likes.c.list_id == list.list_id).scalar()
first_listing = list.listings
tmp.append(list)
tmp.append(like_count)
tmp.append(first_listing)
top_list_with_likes.append(tmp)
return render_template('home.html', top_list_with_likes=top_list_with_likes)
######################################################
# Error page
######################################################
@app.errorhandler(404)
def page_not_found(e):
"""404 page."""
return render_template('404.html'), 404
if __name__ == "__main__":
app.run()