Skip to content
Permalink
3d67617616
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
408 lines (346 sloc) 23.8 KB
from flask import Blueprint, jsonify, request
from . import db
from .models import MOVIE_DETAILS, USER_DETAILS, REVIEW_DETAILS #the columns and the data from there are imported
from flask_cors import CORS
import base64 #used for decrypt hash
main = Blueprint('main', __name__)
CORS(main, supports_credentials=True)
@main.route('/addreview', methods=['POST'])
def addReview():
movieID = request.get_json() #gets movieID from the page URL by using userParams function
movieToReview = db.session.query((MOVIE_DETAILS.id),
(MOVIE_DETAILS.title),
(MOVIE_DETAILS.release),
(MOVIE_DETAILS.description),
(MOVIE_DETAILS.category),
(MOVIE_DETAILS.movieLength), #gets the movie details to show on the page filtered down to the right movie
(MOVIE_DETAILS.coverPhoto),
(MOVIE_DETAILS.pagePhoto),
(MOVIE_DETAILS.longDescription),
(REVIEW_DETAILS.review),
(REVIEW_DETAILS.reviewDate),
(REVIEW_DETAILS.userId),db.func.avg(REVIEW_DETAILS.rating).label('averageRating'),).outerjoin(REVIEW_DETAILS).group_by(MOVIE_DETAILS.id).order_by(REVIEW_DETAILS.rating.desc()).filter(MOVIE_DETAILS.id==movieID).all()
thisMovie = [] #empty array created
for movie in movieToReview: #loop through data
thisMovie.append({ 'movieId' : movie.id,
'title': movie.title,
'rating': movie.averageRating,
'category': movie.category,
'coverPhoto': movie.coverPhoto, #appends the empty array with data from the query
'longDescription': movie.longDescription,
'release': movie.release,
'movieLength': movie.movieLength})
return jsonify ({'thisMovie': thisMovie[0]})
@main.route('/addnewreview', methods=['POST'])
def addnewreview():
details = request.get_json() #gets the user input from add review form
print(details)
newReview = REVIEW_DETAILS(rating=details[0],review=details[1],userId=details[2],movieId=details[3],reviewDate=details[4]) #assigns the input to the right columns in the database
db.session.add(newReview) #adds newReview to be commited
db.session.commit() #data is commited to the database
return jsonify ()
@main.route('/removereview', methods=['POST'])
def removereview():
details = request.get_json() #gets the review data from myReviews page
print(details)
REVIEW_DETAILS.query.filter(REVIEW_DETAILS.userId == details["userID"]).filter(REVIEW_DETAILS.movieId == details["movieId"]).delete() #the data is filtered down to get the intersection between the correct user and movieID
db.session.commit() #commit the delete
return 'Done', 201 #successful return
@main.route('/movies')
def movies():
movie_list = db.session.query((MOVIE_DETAILS.id),
(MOVIE_DETAILS.title),
(MOVIE_DETAILS.release),
(MOVIE_DETAILS.description),
(MOVIE_DETAILS.category),
(MOVIE_DETAILS.movieLength), #gets the movie details to show on the page grouped and joined to review datails to show ratings
(MOVIE_DETAILS.coverPhoto),
(MOVIE_DETAILS.pagePhoto),
(MOVIE_DETAILS.longDescription),
(REVIEW_DETAILS.review),
(REVIEW_DETAILS.reviewDate),
(REVIEW_DETAILS.userId),db.func.avg(REVIEW_DETAILS.rating).label('averageRating'),).outerjoin(REVIEW_DETAILS).group_by(MOVIE_DETAILS.id).order_by(REVIEW_DETAILS.rating.desc()).all()
movies = [] #empty array created
for movie in movie_list:
movies.append({ 'id' : movie.id,
'title' : movie.title,
'release' : movie.release,
'description' : movie.description,
'rating' : movie.averageRating,
'movieLength' : movie.movieLength, #movies data added to the array
'category' : movie.category,
'coverPhoto' : movie.coverPhoto,
'pagePhoto' : movie.pagePhoto,
'longDescription' : movie.longDescription})
return jsonify ({'movies': movies}) #movies data returned
@main.route('/users')
def users():
user_list = USER_DETAILS.query.all() #all user data is queried
users = [] #empty array created
for user in user_list:
users.append({ 'id' : user.id,
'userName' : user.userName, #user data is added to the array
'userPassword' : user.userPassword})
return jsonify ({'users': users}) #json object returned with details
@main.route('/register', methods=['POST'])
def addUser():
details = request.get_json() #the details are taken from user input
check = db.session.query((USER_DETAILS.id)).filter(USER_DETAILS.userName==details[0]).all() #a check is ran to see if username already exists
if len(check)==1:
return jsonify ({'userID': None}) #if the username already exists the userID will be set to None so no new user will be added
newUser = USER_DETAILS(userName=details[0],userPassword=details[1],joinDate=details[2]) #new user details are added to the database
db.session.add(newUser)
db.session.commit() #data is commited to the database
userID = db.session.query(USER_DETAILS.id).filter(USER_DETAILS.userName==details[0]).all() #userID is queried from the database to get user logged in
print(userID)
return jsonify ({'userID': userID[0][0]}) #userID returns username and password
@main.route('/login',methods=['GET'])
def login():
userID=None #userID is set to none to initialize it by default
token = request.headers.get('Authorization') #the authorisation headers are taken
print(token)
[type,hash]=token.split() #authorisation token is split to get only the hash
print(hash)
decryptedHash=base64.b64decode(hash.encode('ascii')) #the has is decrypted using base64
print(decryptedHash)
decryptedHashArray=((decryptedHash).decode('utf-8')).split(':') #the decrypted data is decoded to utf-8 and is split by the : symbol
print(decryptedHashArray)
checkUserName = db.session.query((USER_DETAILS.id),(USER_DETAILS.userPassword)).filter(USER_DETAILS.userName==decryptedHashArray[0]).all() #filter the userid and password if the username data equals to what the hash is
print(checkUserName)
if len(checkUserName)==1:
if checkUserName[0][1]==decryptedHashArray[1]: #login details and database match checked
userID=checkUserName[0][0]
return jsonify ({'userID': userID}) #userID is returned either with an existing one or None to either let the login happen or to stop it
@main.route('/reviews')
def reviews():
review_list = REVIEW_DETAILS.query.all() #all of the reviews are taken from the database
reviews = [] #empty array created
for review in review_list:
reviews.append({ 'reviewId' : review.reviewId,
'movieId' : review.movieId,
'userId' : review.userId,
'rating' : review.rating, #array appended with data from review table
'review' : review.review,
'reviewDate' : review.reviewDate})
return jsonify ({'reviews': reviews}) #the reviews are returned
@main.route('/recommended', methods=['POST'])
def recommended():
id = request.get_json() #id is taken to show the correct user's data
print(id)
recommended_list = db.session.query((MOVIE_DETAILS.category)).outerjoin(REVIEW_DETAILS).group_by(MOVIE_DETAILS.category).filter(REVIEW_DETAILS.userId==id).all() #MOVIE_DETAILS and REVIEW_DETAILS joined and get data by the userId
moviesRecommended = [] #empty array created
for movie in recommended_list:
movie_list = db.session.query((MOVIE_DETAILS.id),
(MOVIE_DETAILS.title),
(MOVIE_DETAILS.release),
(MOVIE_DETAILS.description),
(MOVIE_DETAILS.category),
(MOVIE_DETAILS.movieLength),
(MOVIE_DETAILS.coverPhoto), #gets the movie details to show on the page grouped and joined to review datails to show ratings
(MOVIE_DETAILS.pagePhoto),
(MOVIE_DETAILS.longDescription),
(REVIEW_DETAILS.rating),
(REVIEW_DETAILS.review),
(REVIEW_DETAILS.reviewDate),
(REVIEW_DETAILS.userId),db.func.avg(REVIEW_DETAILS.rating).label('averageRating'),).outerjoin(REVIEW_DETAILS).group_by(MOVIE_DETAILS.id).order_by(REVIEW_DETAILS.rating.desc()).filter(MOVIE_DETAILS.category==movie.category).all()
movies = [] #empty array created
for movie2 in movie_list:
movies.append({ 'id' : movie2.id,
'title' : movie2.title,
'release' : movie2.release,
'description' : movie2.description,
'rating' : movie2.averageRating,
'movieLength' : movie2.movieLength,
'category' : movie2.category, #appends data to the movies array
'coverPhoto' : movie2.coverPhoto,
'pagePhoto' : movie2.pagePhoto,
'longDescription' : movie2.longDescription})
moviesRecommended.append({'category' : movie.category, 'movies' : movies}) #the category and all data from movies is appended to moviesRecommended
return jsonify ({'moviesRecommended': moviesRecommended}) #the moviesRecommended array is returned
@main.route('/myReviews')
def myReviews():
userId=None #userID is set to none to initialize it by default
token = request.headers.get('Authorization')
[type,hash]=token.split() #authorisation token is split to get only the hash
print(hash)
decryptedHash=base64.b64decode(hash.encode('ascii')) #the has is decrypted using base64
print(decryptedHash)
decryptedHashArray=((decryptedHash).decode('utf-8')).split(':') #the decrypted data is decoded to utf-8 and is split by the : symbol
checkUserName = db.session.query((USER_DETAILS.id),(USER_DETAILS.userPassword)).filter(USER_DETAILS.userName==decryptedHashArray[0]).all() #filter the userid and password if the username data equals to what the hash is
userId = checkUserName[0][0] #userId equals to the first element in the array which is the id
myReview_list = db.session.query((MOVIE_DETAILS.id),
(MOVIE_DETAILS.category),
(MOVIE_DETAILS.coverPhoto),
(REVIEW_DETAILS.rating).label('reviewRating'), #MOVIE_DETAILS and REVIEW_DETAILS are both used so they are joined and filtered so it only shows the reviews of the logged in user
(REVIEW_DETAILS.review),
(REVIEW_DETAILS.reviewDate),
(REVIEW_DETAILS.userId)).outerjoin(REVIEW_DETAILS).group_by(MOVIE_DETAILS.id).filter(REVIEW_DETAILS.userId==userId).order_by(REVIEW_DETAILS.rating.desc()).all()
myreviews = [] #empty array created
for review in myReview_list:
averageRating = db.session.query((MOVIE_DETAILS.id),db.func.avg(REVIEW_DETAILS.rating).label('averageRating'),).outerjoin(REVIEW_DETAILS).group_by(MOVIE_DETAILS.id).order_by(REVIEW_DETAILS.rating.desc()).filter(REVIEW_DETAILS.movieId==review.id).all()
print(averageRating) #average rating is worked out using the .func.avg and has joined the MOVIE_DETAILS and REVIEW_DETAILS to do so using the REVIEW_DETAILS.movieId and review.id
print(userId,review.userId)
myreviews.append({ 'movieId' : review.id,
'averageRating': averageRating[0][1], #avarage rating is the second element in the array so second element is appended to array
'reviewRating' : review.reviewRating,
'review' : review.review, #rest of the data is appanded to the array
'userId' : review.userId,
'reviewDate' : review.reviewDate,
'coverPhoto' : review.coverPhoto})
return jsonify ({'myReviews': myreviews}) #array is returned
@main.route('/categories', methods=['POST'])
def categories():
category = request.get_json() #catergory is taken from useParams function
print(category)
movie_list = db.session.query((MOVIE_DETAILS.id),
(MOVIE_DETAILS.title),
(MOVIE_DETAILS.release),
(MOVIE_DETAILS.description),
(MOVIE_DETAILS.category),
(MOVIE_DETAILS.movieLength),
(MOVIE_DETAILS.coverPhoto), #data is queried to get MOVIE_DETAILS and REVIEW_DETAILS of specific category
(MOVIE_DETAILS.pagePhoto),
(MOVIE_DETAILS.longDescription),
(REVIEW_DETAILS.rating),
(REVIEW_DETAILS.review),
(REVIEW_DETAILS.reviewDate),
(REVIEW_DETAILS.userId),db.func.avg(REVIEW_DETAILS.rating).label('averageRating'),).outerjoin(REVIEW_DETAILS).group_by(MOVIE_DETAILS.id).order_by(REVIEW_DETAILS.rating.desc()).filter(MOVIE_DETAILS.category==category).all()
movies = [] #empty array craeted
print(movies)
for movie in movie_list:
movies.append({ 'id' : movie.id,
'title' : movie.title,
'release' : movie.release,
'description' : movie.description,
'rating' : movie.averageRating,
'movieLength' : movie.movieLength, #data for a category is appeneded to the array
'category' : movie.category,
'coverPhoto' : movie.coverPhoto,
'pagePhoto' : movie.pagePhoto,
'longDescription' : movie.longDescription})
return jsonify ({'categories': movies}) #movie data is returned
@main.route('/search', methods=['POST'])
def search():
searchTerm = request.get_json() #the term being searched by user is requested through the input on the page
print(searchTerm)
movie_list = db.session.query((MOVIE_DETAILS.id),
(MOVIE_DETAILS.title),
(MOVIE_DETAILS.release),
(MOVIE_DETAILS.description),
(MOVIE_DETAILS.category),
(MOVIE_DETAILS.movieLength),
(MOVIE_DETAILS.coverPhoto),
(MOVIE_DETAILS.pagePhoto), #MOVIE_DETAILS and REVIEW_DETAILS are queried to show on the page using the .contains function to get all movies that contain the search term in the title
(MOVIE_DETAILS.longDescription),
(REVIEW_DETAILS.rating),
(REVIEW_DETAILS.review),
(REVIEW_DETAILS.reviewDate),
(REVIEW_DETAILS.userId),db.func.avg(REVIEW_DETAILS.rating).label('averageRating'),).outerjoin(REVIEW_DETAILS).group_by(MOVIE_DETAILS.id).order_by(REVIEW_DETAILS.rating.desc()).filter(MOVIE_DETAILS.title.contains(searchTerm)).all()
movies = [] #empty array created
print(movies)
for movie in movie_list:
movies.append({ 'id' : movie.id,
'title' : movie.title,
'release' : movie.release,
'description' : movie.description,
'rating' : movie.averageRating,
'movieLength' : movie.movieLength, #movie data appended to array
'category' : movie.category,
'coverPhoto' : movie.coverPhoto,
'pagePhoto' : movie.pagePhoto,
'longDescription' : movie.longDescription})
return jsonify ({'searchResults': movies}) #search results are returned
@main.route('/movieReviews', methods=['POST'])
def movieReviews():
id = request.get_json() #id is requested to know which movie is being shown
print(id)
movie_list = db.session.query((MOVIE_DETAILS.id),
(MOVIE_DETAILS.title),
(MOVIE_DETAILS.release),
(MOVIE_DETAILS.description),
(MOVIE_DETAILS.category),
(MOVIE_DETAILS.movieLength),
(MOVIE_DETAILS.coverPhoto), #MOVIE_DETAILS data is queried for the current movie id joined with REVIEW_DETAILS to work out average rating
(MOVIE_DETAILS.pagePhoto),
(MOVIE_DETAILS.longDescription),
(REVIEW_DETAILS.rating),
(REVIEW_DETAILS.review),
(REVIEW_DETAILS.reviewDate),
(REVIEW_DETAILS.userId),db.func.avg(REVIEW_DETAILS.rating).label('averageRating'),).outerjoin(REVIEW_DETAILS).group_by(MOVIE_DETAILS.id).order_by(REVIEW_DETAILS.rating.desc()).filter(MOVIE_DETAILS.id==id).all()
review_list = db.session.query((REVIEW_DETAILS.rating),(REVIEW_DETAILS.review),(REVIEW_DETAILS.reviewDate),(REVIEW_DETAILS.userId), #REVIEW_DETAILS are needed to show all reviews of current movie
(REVIEW_DETAILS.reviewId),(REVIEW_DETAILS.movieId),(USER_DETAILS.userName)).outerjoin(USER_DETAILS).filter(REVIEW_DETAILS.movieId==id).all()
movies = []
for movie in movie_list:
movies.append({ 'id' : movie.id,
'title' : movie.title,
'release' : movie.release,
'description' : movie.description,
'rating' : movie.averageRating, #movie data is appeneded to array
'movieLength' : movie.movieLength,
'category' : movie.category,
'coverPhoto' : movie.coverPhoto,
'pagePhoto' : movie.pagePhoto,
'longDescription' : movie.longDescription})
reviews = []
for review in review_list:
reviews.append({ 'reviewId' : review.reviewId,
'movieId' : review.movieId,
'userId' : review.userId,
'rating' : review.rating, # review data is appanded to array
'review' : review.review,
'reviewDate' : review.reviewDate,
'userName' : review.userName})
return jsonify ({'movieDetails': movies,'movieReviews': reviews}) #both arrays are returned
@main.route('/myaccount', methods=['POST'])
def myaccount():
userID = request.get_json() #userID is taken to know which user is logged in
print(userID)
user_list = USER_DETAILS.query.filter(USER_DETAILS.id==userID).all() #all user information is queried if userID matches id in the MOVIE_DETAILS table
review_list = db.session.query((REVIEW_DETAILS.rating),
(REVIEW_DETAILS.review),
(REVIEW_DETAILS.reviewDate),
(REVIEW_DETAILS.userId), #reviews of logged in user are queried
(REVIEW_DETAILS.reviewId),
(REVIEW_DETAILS.movieId),
(USER_DETAILS.userName)).outerjoin(USER_DETAILS).filter(REVIEW_DETAILS.userId==userID).all()
reviews = []
for review in review_list:
reviews.append({ 'reviewId' : review.reviewId,
'movieId' : review.movieId,
'userId' : review.userId,
'rating' : review.rating, #review data added to review array
'review' : review.review,
'reviewDate' : review.reviewDate,
'userName' : review.userName})
movieID=(reviews[-1])['movieId'] #movieID needed to be altered as it wasn't showing the right movie
print(movieID)
movie_list = db.session.query((MOVIE_DETAILS.id),
(MOVIE_DETAILS.title),
(MOVIE_DETAILS.release),
(MOVIE_DETAILS.description),
(MOVIE_DETAILS.category),
(MOVIE_DETAILS.movieLength), #movie details are queried where there REVIEW_DETAILS movieId matches the movieID so it shows the right movie details per review
(MOVIE_DETAILS.coverPhoto),
(MOVIE_DETAILS.pagePhoto),
(MOVIE_DETAILS.longDescription),
(REVIEW_DETAILS.review),
(REVIEW_DETAILS.reviewDate),
(REVIEW_DETAILS.userId),db.func.avg(REVIEW_DETAILS.rating).label('averageRating'),).outerjoin(REVIEW_DETAILS).group_by(MOVIE_DETAILS.id).order_by(REVIEW_DETAILS.rating.desc()).filter(REVIEW_DETAILS.movieId==movieID).all()
print(movieID)
users = []
for user in user_list:
users.append({ 'id' : user.id,
'userName' : user.userName,
'userPassword' : user.userPassword, #user data is appended to users array
'joinDate' : user.joinDate})
movies = []
for movie in movie_list:
movies.append({ 'id' : movie.id,
'title' : movie.title,
'release' : movie.release,
'description' : movie.description,
'rating' : movie.averageRating, #movie details are appended to movies array
'movieLength' : movie.movieLength,
'category' : movie.category,
'coverPhoto' : movie.coverPhoto})
return jsonify ({'movieDetails': movies,'userDetails': users,'userReviews': reviews}) #all three arrays are returned