Ejemplo n.º 1
0
def feed():
    current_userid = current_user.get_user_id()[0]
    with dbapi2._connect(app.config['dsn']) as connection:
        cursor = connection.cursor()

        query = """SELECT u.USERNAME FROM USERS u
                        INNER JOIN FOLLOWERS f ON (u.ID = f.FOLLOWED_USER_ID)
                        WHERE(FOLLOWING_USER_ID = %s)"""

        cursor.execute(query, (current_userid, ))

        followings = []
        for following in cursor:
            followings.append(following[0])

        posts = []
        for followed in followings:
            query = """SELECT u.ID, u.USERNAME, m.IMDB_URL, m.TITLE, p.COMMENTS FROM
                            USERS u INNER JOIN POSTS p ON (u.ID = p.USER_ID)

                                    INNER JOIN MOVIES m ON (m.MOVIEID = p.MOVIE_ID)

                        WHERE (USERNAME = %s)

                        ORDER BY p.POST_ID DESC"""

            cursor.execute(query, (followed, ))
            for post in cursor:
                posts.append(post[0:5])

    return render_template('feed.html', posts=posts)
Ejemplo n.º 2
0
def RemoveMovieFromList(listname, movieid):
    userid = current_user.get_user_id()
    listToDelete = MovieList(userid, movieid, listname)

    listToDelete.removeMovieFromList()
    flash("Selected movie successfully removed from " + listname + ".")
    return redirect(url_for('page.profile_page'))
Ejemplo n.º 3
0
def DeleteWholeList(listname):

    userid = current_user.get_user_id()
    listToDelete = MovieList(userid, "", listname)

    listToDelete.delete_list()
    flash(listname + " successfully deleted.")
    return redirect(url_for('page.profile_page'))
Ejemplo n.º 4
0
def deleteaccount():
    current_userid = current_user.get_user_id()
    if current_userid is not None:
        with dbapi2.connect(app.config['dsn']) as connection:
            cursor = connection.cursor()
            query = """DELETE FROM USERS WHERE (ID = %s)"""
            cursor.execute(query, current_userid)
            connection.commit()
        return redirect(url_for('page.home_page'))
    else:
        flash("Please log in to MovieShake.")
        return redirect(url_for('page.home_page'))
Ejemplo n.º 5
0
def profile_page():

    if current_user.get_id() is not None:
        movies = []
        lists = []
        userid = current_user.get_user_id()

        with dbapi2._connect(current_app.config['dsn']) as connection:
            cursor = connection.cursor()
            query = """SELECT TITLE, YEAR, m.SCORE, VOTES, IMDB_URL, m.MOVIEID FROM MOVIES m
                                 INNER JOIN WATCHEDLIST w ON (m.MOVIEID = w.MOVIEID)
                                 WHERE (w.USERNAME = %s) """

            cursor.execute(query, (current_user.username, ))

            for movie in cursor:
                movies.append(movie)

            query = """SELECT DISTINCT LIST_NAME FROM MOVIELIST WHERE (USER_ID = %s)"""

            cursor.execute(query, (userid, ))

            for list in cursor:
                lists.append(list[0])

            followingusers = []
            followingusers = current_user.get_following_users_by_userid()

            posts = []
            posts = current_user.get_posts()

            connection.commit()
            usernames = []
            usernames.append(current_user.username)
        return render_template('profile.html',
                               lists=lists,
                               movies=movies,
                               posts=posts,
                               followingusers=followingusers,
                               username=usernames)
    else:
        flash("Please log in to MovieShake")
        return redirect(url_for('page.login_page'))
Ejemplo n.º 6
0
def home_page_1():
    if current_user.get_id() is None:
        return render_template('home2.html')
    else:
        current_userid = current_user.get_user_id()[0]
        lists = []

        with dbapi2._connect(current_app.config['dsn']) as connection:
            cursor = connection.cursor()
            query = """SELECT DISTINCT m.LIST_NAME, m.USER_ID, u.USERNAME FROM MOVIELIST m
                        INNER JOIN FOLLOWERS f ON (m.USER_ID = f.FOLLOWED_USER_ID)
                        INNER JOIN USERS u ON(u.ID = m.USER_ID)
                        WHERE (f.FOLLOWING_USER_ID = %s)"""

            cursor.execute(query, (current_userid, ))

            for list in cursor:
                lists.append(list[0:3])

            query = """SELECT u.USERNAME FROM USERS u
                        INNER JOIN FOLLOWERS f ON (u.ID = f.FOLLOWED_USER_ID)
                        WHERE(FOLLOWING_USER_ID = %s)"""

            cursor.execute(query, (current_userid, ))

            followings = []
            for following in cursor:
                followings.append(following[0])

            watcheds = []
            for followed in followings:
                query = """SELECT w.USERNAME, m.TITLE, m.IMDB_URL, w.SCORE FROM WATCHEDLIST w
                        INNER JOIN USERS u ON (u.USERNAME = w.USERNAME)
                        INNER JOIN MOVIES m ON (m.MOVIEID = w.MOVIEID)
                        WHERE (w.USERNAME = %s)
                        ORDER BY w.MOVIEID DESC"""

                cursor.execute(query, (followed, ))
                for watched in cursor:
                    watcheds.append(watched[0:4])

        return render_template('home.html', lists=lists, watcheds=watcheds)
Ejemplo n.º 7
0
def Show_list(listname):
    movies = []
    listnames = []
    with dbapi2._connect(current_app.config['dsn']) as connection:
        cursor = connection.cursor()
        query = """SELECT MOVIEID, TITLE, YEAR, SCORE, VOTES, IMDB_URL FROM MOVIES m
                                 INNER JOIN MOVIELIST l ON (m.MOVIEID = l.MOVIE_ID)
                                 WHERE ((l.LIST_NAME = %s) AND (l.USER_ID = %s))"""

        user_id = current_user.get_user_id()
        cursor.execute(query, (
            listname,
            user_id,
        ))

        for movie in cursor:
            movies.append(movie)

        connection.commit()
        listnames.append(listname)
    return render_template('movielist.html', movies=movies, listname=listnames)
Ejemplo n.º 8
0
def UserList():
    userid = current_user.get_user_id()
    users = []
    with dbapi2._connect(current_app.config['dsn']) as connection:
        cursor = connection.cursor()
        query = """SELECT ID, USERNAME, EMAIL FROM USERS

                        EXCEPT

                        SELECT ID, USERNAME, EMAIL FROM USERS u
                        INNER JOIN FOLLOWERS f ON (u.ID = f.FOLLOWED_USER_ID)
                        WHERE (f.FOLLOWING_USER_ID = %s) """

        cursor.execute(query, (userid, ))

        for user in cursor:
            if current_user.username == user[1]:
                continue
            else:
                users.append(user)

        connection.commit()
    return render_template('userlist.html', users=users)
Ejemplo n.º 9
0
def list_page():
    if request.method == "POST":

        flag = False

        if current_user.username is None:
            flash('Please log in.')
            return redirect(url_for('page.login_page'))

        else:

            list_name = request.form['name']
            movie1 = request.form['moviename1']
            movie2 = request.form['moviename2']
            movie3 = request.form['moviename3']
            movie4 = request.form['moviename4']

            list_array = [
                movie1.title(),
                movie2.title(),
                movie3.title(),
                movie4.title()
            ]

            smovie = Movie(list_array[0], "", "", "", "")
            movie = smovie.search_movie_in_db()
            newlist = MovieList(current_user.get_user_id(), movie, list_name)

            for movie in list_array:
                if movie == "":
                    continue
                else:
                    smovie = Movie(movie, "", "", "", "")
                    movie = smovie.search_movie_in_db()

                    if movie == -1:
                        movieToAdd = smovie.verify_movie_from_api()
                        if (movieToAdd == -1):
                            flash("There is no such movie")
                            return redirect(url_for('page.home_page'))
                        else:
                            movieToAdd.score = 7

                            movieToAdd.add_movie_to_db()
                            movieid = movieToAdd.search_movie_in_db()
                            newlist = MovieList(current_user.get_user_id(),
                                                movieid[0], list_name)
                            newlist.add_movie()

                    else:
                        newlist = MovieList(current_user.get_user_id(), movie,
                                            list_name)
                        if newlist.exists() == 1:
                            flash('You have already added that movie.')
                            return redirect(url_for('page.list_page'))
                        else:
                            newlist.add_movie()

            return redirect(url_for('page.home_page'))

    else:
        return render_template('list.html')
Ejemplo n.º 10
0
def user_profiles(user_id):
    if current_user.get_id() is not None:
        if current_user.get_user_id()[0] == int(user_id):
            return redirect(url_for('page.profile_page'))
        else:
            with dbapi2._connect(app.config['dsn']) as connection:

                cursor = connection.cursor()

                query = "SELECT USERNAME FROM USERS WHERE (ID = %s)"

                cursor.execute(query, (user_id, ))

                usr = cursor.fetchone()

                user = User(usr[0], "", "")

                if user is not None:
                    movies = []
                    lists = []
                    userid = user_id
                    with dbapi2._connect(
                            current_app.config['dsn']) as connection:
                        cursor = connection.cursor()
                        query = """SELECT TITLE, YEAR, m.SCORE, VOTES, IMDB_URL FROM MOVIES m
                                     INNER JOIN WATCHEDLIST w ON (m.MOVIEID = w.MOVIEID)
                                     WHERE (w.USERNAME = %s) """

                        cursor.execute(query, (user.username, ))

                    for movie in cursor:
                        movies.append(movie)

                    query = """SELECT DISTINCT LIST_NAME FROM MOVIELIST WHERE (USER_ID = %s)"""

                    cursor.execute(query, (userid, ))

                    for list in cursor:
                        lists.append(list[0])

                    followingusers = []
                    followingusers = user.get_following_users_by_userid()

                    followedusers = []
                    followedusers = user.get_followed_users_by_userid()

                    posts = []
                    posts = user.get_posts()
                    connection.commit()

                    currentuserid = current_user.get_user_id()
                    return render_template('userprofiles.html',
                                           userid=user_id,
                                           username=user.username,
                                           lists=lists,
                                           movies=movies,
                                           posts=posts,
                                           followingusers=followingusers,
                                           currentuserid=currentuserid,
                                           followedusers=followedusers)
                else:
                    flash("There is no such user.")
                    return redirect(url_for('page.home_page'))

    else:
        flash("Please log in to MovieShake")
        return redirect(url_for('page.login_page'))
Ejemplo n.º 11
0
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'))