Beispiel #1
0
def search():
    if request.method == 'POST':
        print(request.form.get('input'))
        titles = Movie.query.filter(
            Movie.title.ilike(r'{}%'.format(request.form.get('input')))).all()
        movies_schema = MovieSchema(many=True)
        results = movies_schema.dump(titles)
        return jsonify(results)

    return jsonify({"connection": "failure"})
Beispiel #2
0
def read_all():
    """
    This function responds to a request for /api/movie
    with the complete lists of movie

    :return:        json string of list of movie
    """
    # Create the list of movie from our data
    #return [MOVIE[key] for key in sorted(MOVIE.keys())]
    movie = Movie.query.order_by(Movie.name).all()
    # Serialize the data for the response
    movie_schema = MovieSchema(many=True)
    data = movie_schema.dump(movie)  #.data
    return data
Beispiel #3
0
def update(movie_id, movie):
    """
    This function updates an existing movie in the MOVIE structure

    :param movie_id:    Id of movie to update in the MOVIE structure
    :param movie:       movie to update
    :return:            updated movie structure
    """

    # Get the person requested from the db into session
    update_movie = Movie.query.filter(Movie.movie_id == movie_id).one_or_none()

    # Try to find an existing movie with the same name as the update
    name = movie.get("name")

    existing_movie = (Movie.query.filter(Movie.name == name).one_or_none())

    # Are we trying to find a movie that does not exist?
    if update_movie is None:
        abort(
            404,
            "Movie not found for Id: {movie_id}".format(movie_id=movie_id),
        )

    # Would our update create a duplicate of another person already existing?
    elif (existing_movie is not None and existing_movie.movie_id != movie_id):
        abort(
            409,
            "Movie \"{name}\" exists already".format(name=name),
        )

    # Otherwise go ahead and update!
    else:

        # turn the passed in person into a db object
        schema = MovieSchema()
        update = schema.load(movie, session=db.session)  #.data

        # Set the id to the person we want to update
        update.movie_id = update_movie.movie_id

        # merge the new object into the old and commit it to the db
        db.session.merge(update)
        db.session.commit()

        # return updated person in the response
        data = schema.dump(update_movie)  #.data

        return data, 200
Beispiel #4
0
def create(movie):
    """
    This function creates a new movie in the MOVIE structure
    based on the passed in movie data

    :param movie:   movie to create in MOVIE structure
    :return:        201 on success, 406 on movie exists
    """
    popularity = movie.get("popularity", None)
    director = movie.get("director", None)
    genre = movie.get("genre", None)
    imdb_score = movie.get("imdb_score", None)
    name = movie.get("name", None)

    existing_movie = (Movie.query.filter(Movie.name == name).one_or_none())
    if existing_movie is None:
        # Create a person instance using the schema and the passed in person
        schema = MovieSchema()
        new_movie = schema.load(movie, session=db.session)  #.data
        # Add the person to the database
        db.session.add(new_movie)
        db.session.commit()
        # Serialize and return the newly created person in the response
        data = schema.dump(new_movie)  #.data
        return data, 201

    # Does the movie exist already?
    if name not in MOVIE and name is not None:
        MOVIE[name] = {
            "popularity": popularity,
            "director": director,
            "genre": genre,
            "imdb_score": imdb_score,
            "name": name,
        }
        return MOVIE[name], 201

    # Otherwise, they exist, that's an error
    else:
        abort(
            406,
            "Movie with name {name} already exists".format(name=name),
        )
Beispiel #5
0
def search(movie):
    """
    This function creates a new movie in the MOVIE structure
    based on the passed in movie data

    :param movie:   movie to create in MOVIE structure
    :return:        201 on success, 406 on movie exists
    """
    popularity = movie.get("popularity", None)
    director = movie.get("director", None)
    genre = movie.get("genre", None)
    imdb_score = movie.get("imdb_score", None)
    name = movie.get("name", None)
    movie_id = movie.get("movie_id", None)

    filtered_movie = Movie.query.filter(
        (Movie.movie_id ==
         (movie_id if movie_id is not '' else Movie.movie_id))
        & (Movie.popularity ==
           (popularity if popularity is not '' else Movie.popularity))
        & (Movie.genre.ilike(
            str("%" + genre + "%") if genre is not '' else Movie.genre))
        & (Movie.imdb_score ==
           (imdb_score if imdb_score is not '' else Movie.imdb_score))
        & (Movie.name == (name if name is not '' else Movie.name))
        & (Movie.director ==
           (director if director is not '' else Movie.director))).all()
    print("Here is the Value")
    print(filtered_movie)
    if filtered_movie is not None:
        # Serialize the data for the response
        movie_schema = MovieSchema(many=True)
        data = movie_schema.dump(filtered_movie)  #.data
        return data
    else:
        abort(
            404, "No Movie for the requested conditions Id: {movie_id}".format(
                movie_id=movie_id))
Beispiel #6
0
def read_one(movie_id):
    """
    This function responds to a request for /api/movie/{movie_id}
    with one matching movie from MOVIE

    :param movie_id:    Id of movie to find
    :return:            movie details matching id
    """

    # Get the movie requested
    movie = Movie.query.filter(Movie.movie_id == movie_id).one_or_none()

    # Did we find a person?
    if movie is not None:
        # Serialize the data for the response
        movie_schema = MovieSchema()
        data = movie_schema.dump(movie)  #.data
        return data

    # otherwise, nope, not found
    else:
        abort(404,
              "Movie not found for Id: {movie_id}".format(movie_id=movie_id))
Beispiel #7
0
def add_new_movie(something):
    try:
        data = json.loads(request.data)
        print(data)
    except Exception as e:
        print(e)
        abort(400)
    try:
        entry = MovieSchema.from_dict(data)
        entry_dict = asdict(entry)
        movie = Movie(title=entry_dict['title'],
                      requirements=json.dumps(entry_dict['requirements']),
                      release_date=entry_dict['release_date'])
        print(movie)
        movie.insert()
        return jsonify({"success": True})
    except Exception as e:
        return formatted_json_validation_error(e)
Beispiel #8
0
def get_movies():
    if request.method == 'POST':
        start = int(request.form.get("start") or 0)
        end = int(request.form.get("end") or (start + 99))
        rating = int(request.form.get("rating") or 0)
        action = int(request.form.get("action") or 0)
        adventure = int(request.form.get("adventure") or 0)
        animation = int(request.form.get("animation") or 0)
        comedy = int(request.form.get("comedy") or 0)
        crime = int(request.form.get("crime") or 0)
        documentary = int(request.form.get("documentary") or 0)
        drama = int(request.form.get("drama") or 0)
        family = int(request.form.get("family") or 0)
        fantasy = int(request.form.get("fantasy") or 0)
        history = int(request.form.get("history") or 0)
        horror = int(request.form.get("horror") or 0)
        music = int(request.form.get("music") or 0)
        mystery = int(request.form.get("mystery") or 0)
        romance = int(request.form.get("romance") or 0)
        science_fiction = int(request.form.get("science_fiction") or 0)
        thriller = int(request.form.get("thriller") or 0)
        war = int(request.form.get("war") or 0)
        western = int(request.form.get("western") or 0)

        movies_schema = MovieSchema(many=True)

        if not (action or adventure or animation or comedy or crime
                or documentary or drama or family or fantasy or history
                or horror or music or mystery or romance or science_fiction
                or thriller or war or western):
            if rating == 1:
                titles = Movie.query.order_by(Movie.rating.desc()).slice(
                    start, end).all()
            else:
                titles = Movie.query.slice(start, end)

        else:
            query = Movie.query.join(Genre, Movie.moviegenres)
            title_list = []

            if action:
                title_list.append(query.filter(Genre.genre == "Action"))
            if adventure:
                title_list.append(query.filter(Genre.genre == "Adventure"))
            if animation:
                title_list.append(query.filter(Genre.genre == "Animation"))
            if comedy:
                title_list.append(query.filter(Genre.genre == "Comedy"))
            if crime:
                title_list.append(query.filter(Genre.genre == "Crime"))
            if documentary:
                title_list.append(query.filter(Genre.genre == "Documentary"))
            if drama:
                title_list.append(query.filter(Genre.genre == "Drama"))
            if family:
                title_list.append(query.filter(Genre.genre == "Family"))
            if fantasy:
                title_list.append(query.filter(Genre.genre == "Fantasy"))
            if history:
                title_list.append(query.filter(Genre.genre == "History"))
            if horror:
                title_list.append(query.filter(Genre.genre == "Horror"))
            if music:
                title_list.append(query.filter(Genre.genre == "Music"))
            if mystery:
                title_list.append(query.filter(Genre.genre == "Mystery"))
            if romance:
                title_list.append(query.filter(Genre.genre == "Romance"))
            if science_fiction:
                title_list.append(
                    query.filter(Genre.genre == "Science Fiction"))
            if thriller:
                title_list.append(query.filter(Genre.genre == "Thriller"))
            if war:
                title_list.append(query.filter(Genre.genre == "War"))
            if western:
                title_list.append(query.filter(Genre.genre == "Western"))

            for i in range(len(title_list)):
                if i == 0:
                    continue
                title_list[0] = title_list[0].intersect(title_list[i])

            if rating == 1:
                titles = title_list[0].order_by(Movie.rating.desc()).slice(
                    start, end).all()
            else:
                titles = title_list[0].slice(start, end).all()

        results = movies_schema.dump(titles)
        return jsonify(results)

    return jsonify({"connection": "failure"})
Beispiel #9
0
def vote():
    if request.method == 'PATCH':
        up = bool(request.form.get('up_vote'))
        down = bool(request.form.get('down_vote'))
        movie_id = request.form.get('movie_id')
        movie = Movie.query.filter_by(movie_id=movie_id).all()

        print(request.cookies.get('id'))

        if request.cookies.get('id') is None:
            res = make_response('')
            res.set_cookie('id', str(uuid.uuid1()))

        if len(
                Sessions.query.filter(
                    Sessions.user_id == request.cookies.get('id'),
                    Sessions.movie_id == movie_id).all()) == 0:
            print("this ran")
            sess = Sessions(user_id=request.cookies.get('id'),
                            movie_id=movie_id)
            db.session.add(sess)
            db.session.commit()

        sess_query = Sessions.query.filter(
            Sessions.user_id == request.cookies.get('id'),
            Sessions.movie_id == movie_id).all()[0]

        if up is True:
            if sess_query.vote is 0:
                sess_query.vote = 1  # 1 is thumbs up
                movie[0].up_vote += 1
            elif sess_query.vote is 2:
                movie[0].down_vote -= 1
                sess_query.vote = 1  # 1 is thumbs up
                movie[0].up_vote += 1
            else:
                sess_query.vote = 0
                movie[0].up_vote -= 1
        if down is True:
            if sess_query.vote is 0:
                sess_query.vote = 2  # 2 is thumbs up
                movie[0].down_vote += 1
            elif sess_query.vote is 1:
                movie[0].up_vote -= 1
                sess_query.vote = 2  # 2 is thumbs up
                movie[0].down_vote += 1
            else:
                sess_query.vote = 0
                movie[0].down_vote -= 1

        db.session.commit()

        if movie[0].up_vote or movie[0].down_vote:
            movie[0].rating = (movie[0].up_vote /
                               (movie[0].up_vote + movie[0].down_vote)) * 100
        else:
            movie[0].rating = 0
        db.session.commit()

        movies_schema = MovieSchema(many=True)
        results = movies_schema.dump(movie)
        return jsonify(results)

    return jsonify({"connection": "failure"})