def DeleteFromWatchedList(username, movieid): movieToDelete = WatchedList(username, movieid, "") score = movieToDelete.existsInWatchedList() movieToDelete = WatchedList(username, movieid, score) movieToDelete.delete_from_watched_list() movie = Movie("", "", "", "", "") oldvotes = movie.getvotes_in_movie_db(movieid) oldscore = movie.getscore_in_movie_db(movieid) if int(oldvotes[0]) > 1: total = int(oldvotes) * int(oldscore) total = total - score newvotes = oldvotes - 1 newscore = total / newvotes movie.update_votes_and_score(movieid, newscore, newvotes) return redirect(url_for('page.home_page'))
def movies_page(): if request.method == "POST": movie = Movie(request.form['title'].title(), "", "", "", "") score = request.form['score'] comments = request.form['comment'] if int(score) < 1 or int(score) > 10: flash("Your rating to the movie should be between 1 and 10.") return redirect(url_for('page.movies_page')) #checks if user is logged in if current_user.get_id() is not None: if (movie.search_movie_in_db() != -1): movieId = movie.search_movie_in_db() userMoviePair = WatchedList(current_user.username, movieId, score) post = Post(current_user.get_user_id(), movieId, comments) oldscore = userMoviePair.existsInWatchedList() if (oldscore != -1): oldscore = oldscore[0] if int(oldscore) == int(score): flash("You have already added " + movie.title + ".") return redirect(url_for('page.home_page')) else: userMoviePair.updateScoreOfWatchedMovie() oldScoreMoviesTable = int( movie.getscore_in_movie_db(movieId)[0]) totalVotes = int( movie.getvotes_in_movie_db(movieId)[0]) newscore = ((oldScoreMoviesTable * totalVotes) - int(oldscore) + int(score)) / (totalVotes) movie.update_votes_and_score(movieId, newscore, totalVotes) flash("You score to " + movie.title + " is updated as " + score + ".") return redirect(url_for('page.home_page')) else: userMoviePair.add_movie_user_pair() #score and vote need to be updated on movies table oldscore = int(movie.getscore_in_movie_db(movieId)[0]) totalVotes = int(movie.getvotes_in_movie_db(movieId)[0]) newscore = ((oldscore * totalVotes) + int(score)) / (totalVotes + 1) totalVotes = totalVotes + 1 movie.update_votes_and_score(movieId, newscore, totalVotes) post.add_post_to_db() flash( movie.title + " is added to your watched list and your post has been saved." ) return redirect(url_for('page.home_page')) else: movieToAdd = movie.verify_movie_from_api() if (movieToAdd == -1): flash("There is no such movie") return redirect(url_for('page.home_page')) else: movieToAdd = movie.verify_movie_from_api() movieToAdd.score = score movieToAdd.add_movie_to_db() flash( movieToAdd.title + " (" + movieToAdd.year + ") is added to your watched list and your post has been saved." ) movieId = movieToAdd.search_movie_in_db() userMoviePair = WatchedList(current_user.username, movieId, score) userMoviePair.add_movie_user_pair() post = Post(current_user.get_user_id(), movieId, comments) post.add_post_to_db() return redirect(url_for('page.home_page')) else: flash("Please log in to MovieShake") return redirect(url_for('page.login_page')) else: if current_user.get_id() is not None: return render_template('movies.html') else: flash("Please log in to MovieShake") return redirect(url_for('page.login_page'))