forked from kkschick/judgmental-eye
-
Notifications
You must be signed in to change notification settings - Fork 0
/
judgment.py
152 lines (124 loc) · 4.95 KB
/
judgment.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
from flask import Flask, render_template, redirect, request, flash, session, url_for
import model
from sqlalchemy.orm import joinedload
app = Flask(__name__)
app.secret_key = '23987ETFSDDF345560DFSASF45DFDF567'
@app.route("/")
def index():
return render_template("index.html")
@app.route("/user_list/", defaults={"page":1})
@app.route("/user_list/<int:page>")
def user_list(page):
perpage = 50
pages = (model.session.query(model.User).count()) / perpage
back_one = page - 1
forward_one = page + 1
user_list = model.session.query(model.User).limit(perpage).offset((page*perpage) -perpage).all()
return render_template("user_list.html", users=user_list, pages=pages, back=back_one, forward=forward_one)
@app.route("/view_user/<int:id>")
def show_user_details(id):
user = model.session.query(model.User).filter_by(id=id).join(model.Rating).join(model.Movie).first()
ratings = model.session.query(model.Rating).options(joinedload(model.Rating.movie)).filter_by(user_id=id).all()
return render_template("view_user.html", user=user, ratings=ratings)
@app.route("/view_movie/<int:id>")
def view_movie_details(id):
movie_ratings = model.show_movie_details(id)
movie = movie_ratings[0].movie
num_ratings = len(movie_ratings)
total = 0
for m in movie_ratings:
total += m.rating
average = float(total) / num_ratings
prediction_items = view_prediction(movie)
beratement = view_eye(prediction_items, movie)
return render_template("view_movie.html", movie=movie,
num_ratings=num_ratings, average=average, prediction_items=prediction_items,
beratement=beratement)
def view_prediction(movie):
if 'user' in session:
user_id = session['user']
r = model.session.query(model.Rating).options(joinedload(model.Rating.movie)).filter_by(user_id=user_id, movie_id=movie.id).first()
prediction = None
if r:
user = r.user
rating = r.rating
effective_rating = rating
else:
user = model.get_user_from_id(user_id)
prediction = user.predict_rating(movie)
rating = None
effective_rating = prediction
else:
rating = None
effective_rating = None
prediction = None
prediction_items = [rating, effective_rating, prediction]
return prediction_items
def view_eye(prediction_items, movie):
effective_rating = prediction_items[1]
the_eye = model.get_the_eye()
eye_rating = model.get_eye_rating(the_eye, movie.id)
if not eye_rating:
eye_rating = the_eye.predict_rating(movie)
else:
eye_rating = eye_rating.rating
if eye_rating != None and effective_rating != None:
difference = abs(eye_rating - effective_rating)
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.",
"You have the worst taste in the world."]
beratement = messages[int(difference)]
else:
beratement = None
return beratement
@app.route("/add_rating", methods=["POST"])
def add_rating():
rating = request.form.get("rating")
movie_id = request.form.get("movie")
user_id = session['user']
model.add_rating(movie_id, user_id, rating)
flash ("You've rated this movie")
return redirect(url_for('show_user_details', id=user_id))
@app.route("/update_rating", methods=["POST"])
def update_rating():
rating = request.form.get("rating")
movie_id = request.form.get("movie")
user_id = session['user']
model.update_rating(movie_id, user_id, rating)
flash ("You've changed your rating for this movie")
return redirect(url_for('show_user_details', id=user_id))
@app.route("/login")
def show_login():
return render_template("login.html")
@app.route("/login", methods=["POST"])
def process_login():
email = request.form.get("email")
password = request.form.get("password")
user = model.get_user_from_email(email)
if user == None:
flash ("This user is not registered yet")
return redirect('signup')
else:
session['user'] = user.id
return redirect('/user_list')
@app.route("/signup")
def show_signup():
return render_template("signup.html")
@app.route("/signup", methods=["POST"])
def make_new_account():
email = request.form.get("email")
password = request.form.get("password")
age = request.form.get("age")
gender = request.form.get("gender")
zipcode = request.form.get("zipcode")
model.create_user(email, password, gender, zipcode, age)
flash ("You're registered! Now please log in")
return redirect('/login')
@app.route("/logout")
def process_logout():
session.clear()
return redirect("/")
if __name__ == "__main__":
app.run(debug = True)