-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathserver.py
223 lines (156 loc) · 6.47 KB
/
server.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
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
"""Movie Ratings."""
from jinja2 import StrictUndefined
from flask import (Flask, render_template, redirect, request, flash,
session, jsonify)
from flask_debugtoolbar import DebugToolbarExtension
from model import User, Rating, Movie, connect_to_db, db
app = Flask(__name__)
# Required to use Flask sessions and the debug toolbar
app.secret_key = "ABC"
app.config['DEBUG_TB_INTERCEPT_REDIRECTS'] = False
# Normally, if you use an undefined variable in Jinja2, it fails
# silently. This is horrible. Fix this so that, instead, it raises an
# error.
app.jinja_env.undefined = StrictUndefined
@app.route('/')
def index():
"""Homepage."""
return render_template("homepage.html")
@app.route("/users")
def user_list():
"""Show list of users; returns list of user objects."""
users = User.query.all()
return render_template("user_list.html", users=users)
@app.route("/movies")
def movie_list():
"""Show list of movies; returns list of movie titles."""
movies = Movie.query.order_by(Movie.title).all()
return render_template("movie_list.html", movies=movies)
@app.route("/register")
def register_form():
"""Renders register template form."""
return render_template("register_form.html")
@app.route("/register", methods=["POST"])
def register_process():
"""Takes information from register form and checks if a user with the
email address exists, and if not, creates a new user in the database."""
email = request.form.get("email")
pwd = request.form.get("pwd")
if db.session.query(User).filter(User.email == email).first() is None:
new_user = User(email=email, password=pwd)
db.session.add(new_user)
db.session.commit()
return redirect("/login-page")
@app.route("/login") # This is a get request.
def login_page():
return render_template("login.html")
@app.route("/login", methods=["POST"]) # Post request; can have same route name.
def login_process():
"""Takes information from register form and checks if a user with the
email address/pwd matches, and if so, logs them in."""
email = request.form.get("email")
pwd = request.form.get("pwd")
if db.session.query(User).filter(User.email == email, User.password == pwd
).first() is None:
flash("Email/password combination do not match.")
return redirect("/login-page")
else:
flash("Logged in! as %s" % email)
user_id = db.session.query(User.user_id).filter(User.email == email).one()
print user_id[0]
session['user_id'] = user_id[0]
return redirect("/users/%s" % (user_id[0]))
@app.route("/logout")
def logout_process():
"""Logs out current user."""
del session['user_id']
flash("You are now logged out. Goodbye!")
return redirect("/")
@app.route("/users/<user_id>")
def show_user_details(user_id):
"""Shows user details: age, zipcode, list of movies they rated and score."""
user = db.session.query(User).get(user_id)
return render_template("user_info.html", user=user)
@app.route("/movies/<movie_id>")
def show_movie_details(movie_id):
"""Shows movie details: title, release date, IMDB url, and list of ratings."""
movie = Movie.query.get(movie_id)
user_id = session.get('user_id')
if user_id:
user_rating = Rating.query.filter_by(
movie_id=movie_id, user_id=user_id).first()
else:
user_rating = None
# Get average rating of movie
prediction = None
rating_scores = [r.score for r in movie.ratings]
avg_rating = float(sum(rating_scores)) / len(rating_scores)
# Prediction: only predict if the user hasn't rated it.
if (user_rating is None) and user_id:
user = User.query.get(user_id)
if user:
prediction = user.predict_rating(movie)
if prediction:
effective_rating = prediction
elif user_rating:
effective_rating = user_rating.score
else:
effective_rating = 1
the_eye = (User.query.filter_by(email="the-eye@of-judgment.com").one())
eye_rating = Rating.query.filter_by(user_id=the_eye.user_id,
movie_id=movie.movie_id).first()
if eye_rating is None:
eye_rating = the_eye.predict_rating(movie)
else:
eye_rating = eye_rating.score
difference = abs(eye_rating - effective_rating)
BERATEMENT_MESSAGES = [
"I suppose you don't have such bad taste after all.",
"I regret every decision that I've ever made that has brought me to "
+ "listen to your opinion.",
"Words fail me, as your taste in movies has clearly failed you.",
"That movie is great. For a clown to watch. Idiot.",
"Words cannot express the awfulness of your taste."
]
beratement = BERATEMENT_MESSAGES[int(difference)]
return render_template(
"movie_info.html",
movie=movie,
user_rating=user_rating,
average=avg_rating,
prediction=prediction,
beratement=beratement
)
@app.route("/rate_movie", methods=["POST"])
def rate_movie():
"""Rates movie from the movie_info page if user is logged in."""
if 'user_id' in session:
movie_id = request.form.get("movie_id")
user_id = session['user_id']
score = request.form.get("new_rating")
if db.session.query(Rating).filter(Rating.movie_id == movie_id,
Rating.user_id == user_id).count() == 0:
new_rating = Rating(movie_id=movie_id,
user_id=user_id,
score=score)
db.session.add(new_rating)
else:
update = db.session.query(Rating).filter(Rating.movie_id == movie_id,
Rating.user_id == user_id).first()
update.score = score
db.session.commit()
flash("Success! Your rating has been added!")
return redirect("/")
else:
flash("Please log in!")
return redirect("/login")
# Auto redirects to a GET page, can't redirect to a POST page.
if __name__ == "__main__":
# We have to set debug=True here, since it has to be True at the
# point that we invoke the DebugToolbarExtension
app.debug = True
app.jinja_env.auto_reload = app.debug # make sure templates, etc. are not cached in debug mode
connect_to_db(app)
# Use the DebugToolbar
DebugToolbarExtension(app)
app.run(port=5000, host='0.0.0.0')