-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathapp.py
64 lines (49 loc) · 2.21 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
import os
from flask import Flask, render_template, request, flash, redirect, session, g, jsonify
from flask_debugtoolbar import DebugToolbarExtension
from sqlalchemy.exc import IntegrityError
from utilities.forms import AddUserForm, LoginForm, EditProfileForm, BillSearchForm
from utilities.views_utils import *
from utilities.api_utils import *
from views.auth_views import auth_BP
from views.user_views import user_BP
from views.bills_views import bills_BP
from models import *
app = Flask(__name__)
app.config['SECRET_KEY'] = os.environ.get('SECRET_KEY', 'TisASecret')
app.config['SQLALCHEMY_DATABASE_URI'] = os.environ.get('DATABASE_URL','postgresql:///follow-rep')
app.config['SQLALCHEMY_TRACK_MODIFICATIONS'] = False
debug = DebugToolbarExtension(app)
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
app.register_blueprint(auth_BP)
app.register_blueprint(user_BP)
app.register_blueprint(bills_BP)
connect_db(app);
####################################################################################
@app.before_request
def add_user_to_g():
"""If we're logged in, add curr user to Flask global."""
if CURR_USER_KEY in session:
g.user = User.query.get(session[CURR_USER_KEY])
else:
g.user = None
####################################################################################
@app.route("/", methods=["GET", "POST"])
def show_home():
"""Return home if user is logged in.
If user is not logged in, redirect to anonymous home"""
if g.user:
form = BillSearchForm()
your_bills = g.user.bills_following
bills = 'None'
if form.validate_on_submit():
query = form.search.data
bills = APIUtils.search_bills(query).values()
return render_template('home.html', form = form, bills = bills, your_bills = your_bills)
bill_data = [(APIUtils.get_recent_bills().get(0)),
(APIUtils.get_recent_bills().get(1)),
(APIUtils.get_recent_bills().get(2)),
(APIUtils.get_recent_bills().get(3)),
(APIUtils.get_recent_bills().get(4)),
]
return render_template("home-anon.html", bills = bill_data)