예제 #1
0
def get_feedback(userId: int, movieId: int):
    """
    Return the user’s feedback of the specified movie id
    :param int userId: The user id to retrieve
    :param int movieId: The movie id to retrieve
    :return: JSON object of movie id and rating
    """

    try:
        # Validate user permission level
        if not is_user():
            return Response(json.dumps({}),
                            mimetype='application/json',
                            status=403)

        # Validate input parameters
        if not isinstance(userId, int) and not isinstance(movieId, int):
            return Response(json.dumps({}),
                            mimetype='application/json',
                            status=400)

        # Retrieve feedback
        result = query_get_feedback(userId, movieId)
        if result is None:
            return Response(json.dumps({}),
                            mimetype='application/json',
                            status=404)
        else:
            return Response(json.dumps(result),
                            mimetype='application/json',
                            status=200)
    except Exception:
        return Response(json.dumps({}),
                        mimetype='application/json',
                        status=500)
예제 #2
0
def get_movie(id: int):
    """
    Return a movie, its genres, and its tags
    :param int id: The movie id to retrieve
    :return: JSON object of movie id, name, and genre tags
    """
    try:
        if not is_user():  # checks user
            return Response(json.dumps({}),
                            mimetype='application/json',
                            status=403)
        if not isinstance(id, int):  # checks if id is an integer
            return Response(json.dumps({}),
                            mimetype='application/json',
                            status=400)
        else:  # executes query
            result = query_get_movie(id)

            if result is None:  # make sure there is at least one result returned
                return Response(json.dumps({}),
                                mimetype='application/json',
                                status=404)
            else:
                return Response(json.dumps(result),
                                mimetype='application/json',
                                status=200)

    except Exception:
        return Response(json.dumps({}),
                        mimetype='application/json',
                        status=500)
def get_movie_autocomplete(name: str):
    """
    Get a list of auto-complete suggestions for a partial movie title
    :param str name: The movie title to find suggestions for
    :return: JSON object of movies array containing movie id, title, tags and genres
    """
    con, cursor = db_connection()

    name = unquote(name)

    try:
        if not is_user():
            return Response(json.dumps({}), mimetype='application/json', status=403)
        else:
            cursor.execute(
                f"SELECT movies.id, movies.name, group_concat(DISTINCT tags.name ORDER BY tags.name ASC SEPARATOR ',') as 'Tags',"
                f"group_concat(DISTINCT genre.genre ORDER BY genre.genre ASC SEPARATOR ',') as 'Genres' FROM FlickPick.movies "
                f"LEFT JOIN tags ON tags.movie_id = movies.id "
                f"LEFT JOIN genre ON genre.movie_id = movies.id "
                f"WHERE movies.name LIKE '{name}__%'"
                f"GROUP BY movies.id ORDER BY movies.id ASC")

            result = cursor.fetchall()

            if len(result) == 0:
                return []
            else:
                distances = [(distance(a[1], name), a[0], a[1], a[2], a[3]) for a in result]
                titles = list(
                    map(lambda movie: {'id': movie[1], 'title': movie[2], 'tags': movie[3], 'genres': movie[4]},
                        sorted(distances)))[:10]

                for x in titles:
                    x['tags'] = [] if x['tags'] is None else x['tags'].split(',')
                    x['genres'] = [] if x['genres'] is None else x['genres'].split(',')

            return Response(json.dumps(titles), mimetype='application/json', status=200)
    except Exception:
        return Response(json.dumps({}), mimetype='application/json', status=500)
    finally:
        cursor.close()
        con.close()
def get_tag_autocomplete(name: str, movieId: int):
    """
    Get a list of auto-complete suggestions for a partial tag. The same tag may exist across multiple movies,
    this method does not return every instance of a tag, only unique tags
    :param str name: The tag name to find suggestions for
    :param int movieId: The movie id to retrieve
    :return: JSON object of tags array containing tag name
    """
    con, cursor = db_connection()
    result_set = {}

    name = unquote(name)

    try:
        if not is_user():
            return Response(json.dumps({}), mimetype='application/json', status=403)
        else:
            cursor.execute(f"SELECT id, name FROM tags WHERE movie_id=%s AND name LIKE '{name}__%'", (movieId,))

            result = cursor.fetchall()

            for a in result:        # creates a unique set from result list
                result_set[a[1]] = a

            result_set = list(result_set.values())  # turns set back to a list

            if len(result_set) == 0:
                return []
            else:
                distances = [(distance(a[1], name), a[1], a[0]) for a in result_set]
                tag = list(map(lambda movie: {'id': movie[2], 'tag': movie[1]}, sorted(distances)))[:10]

                return Response({
                    "tags": tag
                }, mimetype='application/json', status=200)
    except Exception:
        return Response(json.dumps({}), mimetype='application/json', status=500)
    finally:
        cursor.close()
        con.close()
예제 #5
0
def get_tag(id: int):
    """
    Return a tag by id
    :param int id: The tag id to retrieve
    :return: JSON object of tag id, name and movie id
    """

    try:
        if not is_user():
            return Response(json.dumps({}), mimetype='application/json', status=403)
        if not isinstance(id, int):
            return Response(json.dumps({}), mimetype='application/json', status=400)
        else:
            result = query_get_tag(id)

            if result is None:
                return Response(json.dumps({}), mimetype='application/json', status=404)
            else:
                return Response(json.dumps(result), mimetype='application/json', status=200)
    except Exception as e:
        print(e)
        return Response(json.dumps({}), mimetype='application/json', status=500)
def create_feedback_tag(userId: int, movieId: int, tagId: int):
    """
    Replace the feedback for a specific tag id
    :param int userId: The user id to retrieve
    :param int movieId: The movie id to retrieve
    :param int tagId: The tag id to retrieve
    :return: JSON object of feedback id
    """

    rating = request.form["rating"]

    try:
        # Validate user permission level
        if not is_user():
            return Response({}, mimetype='application/json', status=403)

        # Validate input parameters
        if not isinstance(userId, int) and not isinstance(
                movieId, int) and not isinstance(tagId, int):
            return Response(json.dumps({}),
                            mimetype='application/json',
                            status=400)

        # Create row in database
        result = query_create_feedback_tag(userId, movieId, tagId, rating)
        if result is None:
            return Response(json.dumps({}),
                            mimetype='application/json',
                            status=404)
        else:
            return Response(json.dumps(result),
                            mimetype='application/json',
                            status=201)
    except Exception:
        return Response(json.dumps({}),
                        mimetype='application/json',
                        status=500)
def update_feedback_tag(feedbackId: int):
    """
    Replace the feedback for a specific tag id
    :param int feedbackId: The feedback tag id to retrieve
    :return: Nothing
    """

    rating = request.form["rating"]

    try:
        # Validate user permission level
        if not is_user():
            return Response(json.dumps({}),
                            mimetype='application/json',
                            status=403)

        # Validate input parameters
        if not isinstance(feedbackId, int):
            return Response(json.dumps({}),
                            mimetype='application/json',
                            status=400)

        # Update row in database
        result = query_update_feedback_tag(feedbackId, rating)
        if not result:
            return Response(json.dumps({}),
                            mimetype='application/json',
                            status=404)
        else:
            return Response(json.dumps({}),
                            mimetype='application/json',
                            status=200)
    except Exception:
        return Response(json.dumps({}),
                        mimetype='application/json',
                        status=500)