This repository has been archived by the owner on Jan 25, 2023. It is now read-only.
forked from uvapl/celp
-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
91 lines (65 loc) · 2.43 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
from tempfile import mkdtemp
from flask import Flask, render_template, redirect, request, session, flash, send_from_directory
from flask_session import Session
from data import Data
from recommender import Recommender
app = Flask(__name__)
app.config["SESSION_FILE_DIR"] = mkdtemp()
app.config["SESSION_PERMANENT"] = False
app.config["SESSION_TYPE"] = "filesystem"
Session(app)
data = Data()
recommender = Recommender()
@app.route("/")
def index():
"""Landing page, shows 20 recommendations."""
# Get current user if logged in
user = session.get("user")
user_id = user["user_id"] if user else None
# Get recommendations
recommendations = recommender.recommend(user_id)
# Render
return render_template("index.html", recommendations=recommendations,
user=session.get("user"))
@app.route("/login", methods=["POST"])
def login():
"""Login route, redirects to root."""
# Grab username
username = request.form.get("username")
# Try to "log in" (note to self: should implement a password check one day)
try:
session["user"] = data.get_user(username=username)
except IndexError:
flash("Could not log you in: unknown username")
# Goto root
return redirect("/")
@app.route("/logout", methods=["GET"])
def logout():
"""Logout route, redirects to root."""
# Log out
session.pop("user")
# Goto root
return redirect("/")
@app.route("/business/<city>/<id>")
def business(city, id):
"""Business page, shows the business, reviews and 10 recommendations."""
# Get current user if logged in
user = session.get("user")
user_id = user["user_id"] if user else None
# Get business by city and business_id
business_data = data.get_business(city.lower(), id)
# Grab reviews
reviews = data.get_reviews(city=business_data["city"].lower(), business_id=business_data[
"business_id"])
# Get 10 recommendations
recommendations = recommender.recommend(user_id=user_id, business_id=id, city=business_data[
"city"].lower(), n=10)
# Render
return render_template("business.html", business=business_data,
recommendations=recommendations, reviews=reviews, user=user)
@app.route("/static/<path:path>")
def send_static(path):
"""Route to serve anything from the static dir."""
return send_from_directory("static", path)
if __name__ == "__main__":
app.run(debug=True)