def submit_rating(): usr = request.args.get('username') res = User.find_by_username(usr) status = 400 # username not found if res is not None: tconst = request.args.get('FilmID') rating = int(request.args.get('Rating')) if 0 <= rating <= 10: status = 350 cur = g.db.cursor() cur.execute( f"SELECT average_ratings FROM ratings WHERE tconst = '{tconst}';" ) old_rating = float(cur.fetchone()[0]) cur.execute( f"SELECT num_votes FROM ratings WHERE tconst = '{tconst}';") num_votes = int(cur.fetchone()[0]) new_rating = (( (old_rating * num_votes) + rating)) / (num_votes + 1) num_votes += 1 cur.execute( f"UPDATE ratings SET average_ratings = '{new_rating}', num_votes = '{num_votes}' WHERE tconst = '{tconst}';" ) return jsonify({'exit_code': status}) else: status = 300 # rating not between 0 and 10 return jsonify({'exit_code': status})
def find_matches(): usr = request.args.get('username') res = User.find_by_username(usr) status = 500 if res is not None: cur = g.db.cursor() cur.execute(f""" CREATE OR REPLACE VIEW view1 AS SELECT user_id, tconst from favouriteFilms WHERE tconst IN (SELECT tconst FROM favouriteFilms WHERE user_id = '{usr}') AND user_id <> '{usr}' ORDER BY user_id LIMIT 1000; SELECT user_id, list FROM ( SELECT user_id, array_to_string(ARRAY_AGG(primary_title), ',') as list, COUNT(primary_title) FROM (SELECT user_id, primary_title from view1 INNER JOIN films ON view1.tconst = films.tconst) as x GROUP BY user_id ORDER BY COUNT(primary_title) DESC LIMIT 10) as y; """) res = [{ "user": i[0], "movies": i[1].split(",") } for i in cur.fetchall()] return json.dumps(res) return jsonify({'exit_code': status})
def remove_favourite(): usr = request.args.get('username') res = User.find_by_username(usr) status = 500 if res is not None: status = 200 tconst = request.args.get('FilmID') cur = g.db.cursor() cur.execute( f"DELETE FROM favouriteFilms WHERE tconst='{tconst}' AND user_id='{usr}';" ) return jsonify({'exit_code': status}) return jsonify({'exit_code': status})
def add_favourite(): usr = request.args.get('username') res = User.find_by_username(usr) status = 500 if res is not None: status = 200 tconst = request.args.get('FilmID') cur = g.db.cursor() cur.execute( f"INSERT INTO favouriteFilms(user_id, tconst) VALUES ('{usr}','{tconst}');" ) return jsonify({'exit_code': status}) return jsonify({'exit_code': status})
def signup(): # Get user input usr = request.args.get('username') pswrd = request.args.get('password') # Check for blanks if usr == "" or pswrd == "": status = -2 # Empty input else: res = User.find_by_username(usr) status = 2 # Username is already exist if res is None: newUser = User(usr, pswrd) newUser.save_to_db() status = 11 # Username does not exist thus can be used return jsonify({'status': status})
def login(): """ Accepts query parameters: - username (the user's username) - password (the user's attempt at their password) """ usr = request.args.get('username') pswrd = request.args.get('password') res = User.find_by_username(usr) userResponse = None status = -1 # initially set to: user does not exist if res is not None: if not User.validate_password(pswrd, res[1]): status = 1 # user exists but password is incorrect else: userResponse = usr status = 10 # user exists and password is correct return jsonify({ 'user': userResponse, # return the username to the frontend to be stored in localStorage 'status': status })