-
Notifications
You must be signed in to change notification settings - Fork 0
/
app.py
91 lines (74 loc) · 2.84 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
import os
import secrets
from flask import Flask, render_template, flash, request, redirect, url_for, session
from database import initialize_mysql_database, populate_movies, get_movies, add_user_to_neo4j, create_connection, start_neo4j, clear_neo4j, user_exists, recommend_movies
from secret import hash_password
from queries import insert_user, check_user
from utils import get_external_ip
from datetime import timedelta
app = Flask(__name__)
app.config['SECRET_KEY'] = secrets.token_hex(16)
app.config['SESSION_PERMANENT'] = True
app.config['PERMANENT_SESSION_LIFETIME'] = timedelta(days=365)
cursor, conn = initialize_mysql_database()
# Routes
@app.route("/")
def index():
return render_template("index.html")
@app.route("/home")
def home():
if 'email' in session:
movies = get_movies()
recommendations = recommend_movies(session['email'])
return render_template("home.html", movies=get_movies(), recommendations=recommendations)
else:
flash('You must signup or login.')
return redirect(url_for('index'))
@app.route("/signup")
def signup():
if 'email' in session:
return render_template("home.html", data=get_movies())
return render_template("signup.html")
@app.route("/signup", methods = ['POST'])
def signup_verify():
email = request.form.get('InputSignUpEmail')
password = hash_password(request.form.get('InputSignUpPassword'))
if not user_exists(cursor, email):
insert_user(cursor, conn, email, password)
add_user_to_neo4j(email)
session['email'] = email
return redirect(url_for('home'))
@app.route("/login")
def login():
if 'email' in session:
return redirect("home.html")
return render_template("login.html")
@app.route("/login", methods = ['POST'])
def login_verify():
email = request.form.get('InputLoginEmail')
password = hash_password(request.form.get('InputPassword'))
# Search for the email and password in the database
if check_user(cursor, email, password):
session['email'] = email
return redirect(url_for('home'))
else:
flash('Wrong credentials')
return render_template("login.html")
@app.route("/logout")
def logout():
session.pop('email', None)
return redirect(url_for('index'))
@app.route('/select_movie/<movie_id>/<movie_file>')
def select_movie(movie_id, movie_file):
# Handle the movie selection
if 'email' in session :
# Call a function to create a connection between the user and the selected movie
create_connection(session['email'], movie_id)
return redirect(f'http://{get_external_ip()}:81?video={movie_file}')
else:
return redirect(url_for('index'))
if __name__ == "__main__":
populate_movies()
# clear_neo4j()
start_neo4j()
app.run(debug=True, host="0.0.0.0", port=int(os.environ.get("PORT", 80)))