def add_movie_tastes(user_id, movie_id, taste): taste = float(taste) user = User.get_by_id(user_id) movie = Movie.get_by_id(movie_id) for actor in movie.actors: artist = Artist.get_by_id(actor.id()) user.add_taste_artist(artist, ACTOR_WEIGHT * taste) for director in movie.directors: artist = Artist.get_by_id(director.id()) user.add_taste_artist(artist, DIRECTOR_WEIGHT * taste) for writer in movie.writers: artist = Artist.get_by_id(writer.id()) user.add_taste_artist(artist, WRITER_WEIGHT * taste) for genre in movie.genres: user.add_taste_genre(genre, GENRE_WEIGHT * taste) user.remove_proposal() user.put() taskqueue.add(url='/api/proposal/' + user.key.id(), method='GET') return 'OK'
def get_tastes_movies_list(user, page=0): """ Get a readable taste movies list. :param user: user :type user: Models.User :return: list of tastes {"code": 0, "data": {"tastes": [{"idIMDB": id,"originalTitle": original_title, "poster": poster_url}], "type": type, "userId": user_id} :rtype: JSON """ tastes_movies_id = user.tastes_movies movies = [] for taste_movie_id in tastes_movies_id: taste_movie = TasteMovie.get_by_id(taste_movie_id.id()) # Get taste if taste_movie.taste >= 1 and taste_movie.added: movie_id = taste_movie.movie.id() # Get movie id from taste movie = Movie.get_by_id(movie_id) # Get movie by id movies.append({"idIMDB": movie_id, "originalTitle": movie.original_title.encode('utf-8') if movie.original_title is not None else None, "title": movie.title.encode('utf-8') if movie.title is not None else None, "tasted": 1, "poster": movie.poster}) return jsonify(code=0, data={"userId": user.key.id(), "type": "movie", "tastes": movies})
def movies_vote(self, request): """Exposes an API endpoint to insert the new votes from the user Args: request: A list of the movies the user currently likes and cast a vote. Returns: A void message if everthing goes well or an error. """ #get jwt and validates if user exists selected_user = self.validate_jwtoken(self.request_state) list_of_voted_movies_query = MovieRankingUser.query(MovieRankingUser.user==selected_user.key).fetch() for user_movie_relation in list_of_voted_movies_query: current_movie = Movie.query(Movie.key==user_movie_relation.movie).get() current_counter = current_movie.number_of_users_who_voted current_movie.number_of_users_who_voted = current_counter - 1 current_movie.put() user_movie_relation.key.delete() for voted_movie in request.voted_movies: current_movie = Movie.get_by_id(voted_movie.movie_identifier) current_counter = current_movie.number_of_users_who_voted current_movie.number_of_users_who_voted = current_counter + 1 new_movie_user_vote = MovieRankingUser(user=selected_user.key,movie=current_movie.key) current_movie.put() new_movie_user_vote.put() return VoteResponse(status_msg='Vote casted with success.')
def untaste_movie(user_id, movie_id): movie = Movie.get_by_id(movie_id) user = User.get_by_id(user_id) for actor in movie.actors: artist = Artist.get_by_id(actor.id()) taste_artist = TasteArtist.get_by_id(actor.id() + user.key.id()) if taste_artist is not None: taste_artist.update_taste(-ACTOR_WEIGHT) else: user.add_taste_artist(artist, -ACTOR_WEIGHT) if taste_artist.taste == 0: user.remove_taste_artist(artist) for director in movie.directors: artist = Artist.get_by_id(director.id()) taste_artist = TasteArtist.get_by_id(director.id() + user.key.id()) if taste_artist is not None: taste_artist.update_taste(-DIRECTOR_WEIGHT) else: user.add_taste_artist(artist, -DIRECTOR_WEIGHT) if taste_artist.taste == 0: user.remove_taste_artist(artist) for writer in movie.writers: artist = Artist.get_by_id(writer.id()) taste_artist = TasteArtist.get_by_id(writer.id() + user.key.id()) if taste_artist is not None: taste_artist.update_taste(-WRITER_WEIGHT) else: user.add_taste_artist(artist, -WRITER_WEIGHT) if taste_artist.taste == 0: user.remove_taste_artist(artist) for genre in movie.genres: taste_genre = TasteGenre.get_by_id(genre + user.key.id()) if taste_genre is not None: taste_genre.update_taste(-GENRE_WEIGHT) else: user.add_taste_genre(genre, -GENRE_WEIGHT) if taste_genre.taste == 0: user.remove_taste_genre(genre) user.remove_proposal() user.put() taskqueue.add(url='/api/proposal/' + user.key.id(), method='GET') return 'OK'
def get_or_retrieve_by_id(id_imdb): """ This function check if the id is a valid IMDb id and in this case get or retrieve the correct entity. :param id_imdb: a valid IMDb id :type id_imdb: string :return: A model instance :rtype Artist or Movie model """ artist = re.compile('nm\d{7}$') movie = re.compile('tt\d{7}$') if artist.match(id_imdb): # It is an artist's id artist = Artist.get_by_id(id_imdb) # Find artist by id if artist is None: try: artist_key = retrieve_artist_from_id(id_imdb) # Retrieve if is not in the datastore except RetrieverError as retriever_error: raise InternalServerError(retriever_error) artist = Artist.get_by_id(artist_key.id()) # Get artist by id return artist elif movie.match(id_imdb): # It is a movie's id movie = Movie.get_by_id(id_imdb) # Find movie by id if movie is None: try: movie_key = retrieve_movie_from_id(id_imdb) # Retrieve if is not in the datastore except RetrieverError as retriever_error: raise InternalServerError(retriever_error) movie = Movie.get_by_id(movie_key.id()) # Get movie by id return movie else: new_movie = Movie().get_by_id(id_imdb) if new_movie != None: return new_movie else: raise InternalServerError(id_imdb + " is not a valid IMDb id or film.TV id")
def get_watched_movies_list(user, page=0): """ Get a readable watched movie list. :param user: user :type user: Models.User :return: list of watched movies {"code": 0, "data": {"movies": [{"idIMDB": id,"originalTitle": original_title, "poster": poster_url, "date": date}],"userId": user_id, "prevPage": prevPage, "nextPage": nextPage} :rtype: JSON """ watched_movies_id = user.watched_movies # Get all taste_artists' keys movies = [] if (page + 1)*10 > len(watched_movies_id): # Finding max element in page last_elem = len(watched_movies_id) else: last_elem = (page + 1)*10 if (page + 1)*10 < len(watched_movies_id): # Preparing url for next page next_page = str(page + 1) next_page_url = '/api/watched/' + user.key.id() + '/' + next_page else: next_page_url = None if page > 0: # Preparing url for prev page prev_page = str(page - 1) prev_page_url = '/api/watched/' + user.key.id() + '/' + prev_page else: prev_page_url = None for i in range(page * 10, last_elem): # Preparing JSON with list of movies watched for current page watched_movie_id = watched_movies_id[i].id() watched_movie = Movie.get_by_id(watched_movie_id) # Get movie date_watched_movie = user.date_watched[i] # Get date taste_movie = TasteMovie.get_by_id(watched_movie_id + user.key.id()) # Get taste movies.append({"idIMDB": watched_movie.key.id(), "originalTitle": watched_movie.original_title.encode('utf-8') if watched_movie.original_title is not None else watched_movie.title.encode('utf-8'), "title": watched_movie.title.encode('utf-8') if watched_movie.title is not None else watched_movie.original_title.encode('utf-8'), "poster": watched_movie.poster, "date": date_watched_movie.strftime('%d-%m-%Y'), "tasted": 1 if taste_movie is not None else 0}) return jsonify(code=0, data={"userId": user.key.id(), "watched": movies, "nextPage": next_page_url, "previousPage": prev_page_url})
def movies_get(self, request): """Exposes an API endpoint to obtain the details of a Movie Args: request: Id of the movie Returns: An Instance containing the Movie Details """ #get jwt and validates if user exists self.validate_jwtoken(self.request_state) selected_movie = Movie.get_by_id(request.id) if selected_movie is None: message = 'No movie with the id "%s" exists.' % request.id raise endpoints.NotFoundException(message) list_of_users_voted_movies_query = MovieRankingUser.query(MovieRankingUser.movie==selected_movie.key).fetch() list_of_users_voted = [RankingUser.query(RankingUser.key==user_movie_relation.user).get().to_simpler_message() for user_movie_relation in list_of_users_voted_movies_query] return selected_movie.to_message(users_who_voted=list_of_users_voted)
def generate_movies(user, page=0): tastes_movies_id = user.tastes_movies movies = [] for i in range(page, len(tastes_movies_id)): taste_movie_id = tastes_movies_id[i] taste_movie = TasteMovie.get_by_id(taste_movie_id.id()) # Get taste movie_id = taste_movie.movie.id() # Get movie id from taste movie = Movie.get_by_id(movie_id) # Get movie by id movies.append({"idIMDB": movie_id, "originalTitle": movie.original_title.encode('utf-8') if movie.original_title is not None else movie.title.encode('utf-8'), "title": movie.title.encode('utf-8') if movie.title is not None else movie.original_title.encode('utf-8'), "tasted": 1, "poster": movie.poster}) return movies
def write_movies_to_db(): movies = get_movies_from_themoviedb() for parsed_movie in movies: movie = Movie( parsed_movie.id, parsed_movie.title, parsed_movie.year, parsed_movie.overview, parsed_movie.poster_path, parsed_movie.backdrop_path ) if movie.exists(): movie_from_db = Movie.get_by_id(movie.id) else: db.session.add(movie) db.session.commit()
def get_tastes_list(user): """ Get a readable taste artists list. :param user: user :type user: Models.User :return: list of tastes {"code": 0, "data": {"tastes": [{"idIMDB": id,"originalTitle": original_title, "poster": poster_url}], "type": type, "userId": user_id} :rtype: JSON """ # TODO: improve it, replace with function... if user.tastesInconsistence is not True: logging.info("tastes in memory") return jsonify(code=0, data=user.tastesJson) logging.info("rebuiling tastes") tastes_artists_id = user.tastes_artists # Get all taste_artists' keys artists = [] for taste_artist_id in tastes_artists_id: taste_artist = TasteArtist.get_by_id(taste_artist_id.id()) # Get taste if taste_artist is None: logging.error("taste_artist is None") continue if taste_artist.taste >= 0.99 and taste_artist.added: artist_id = taste_artist.artist.id() # Get artist id from taste artist = Artist.get_by_id(artist_id) # Get artist by id artists.append({"idIMDB": artist_id, "name": artist.name.encode('utf-8') if artist.name is not None else None, "tasted": 1, "photo": artist.photo}) tastes_movies_id = user.tastes_movies movies = [] for taste_movie_id in tastes_movies_id: taste_movie = TasteMovie.get_by_id(taste_movie_id.id()) # Get taste if taste_movie is None: logging.error("taste_movie is None") continue if taste_movie.taste >=1 and taste_movie.added : movie_id = taste_movie.movie.id() # Get movie id from taste movie = Movie.get_by_id(movie_id) # Get movie by id movies.append({"idIMDB": movie_id, "originalTitle": movie.original_title.encode('utf-8') if movie.original_title is not None else movie.title.encode('utf-8'), "title": movie.title.encode('utf-8') if movie.title is not None else movie.original_title.encode('utf-8'), "tasted": 1, "poster": movie.poster}) tastes_genres_id = user.tastes_genres genres = [] for taste_genre_id in tastes_genres_id: taste_genre = TasteGenre.get_by_id(taste_genre_id.id()) # Get taste if taste_genre is None: logging.error("taste_genre is None") continue # TODO: not use object, use a simple list if taste_genre.taste >= 0.99 and taste_genre.added : genres.append({"name": taste_genre.genre, "tasted": 1}) dataJson = {"userId": user.key.id(), "type": "all", "tastes": {"artists": artists, "movies": movies, "genres": genres}} user.tastesJson = dataJson user.tastesInconsistence = False user.put() return jsonify(code=0, data=dataJson)
def get(self, movie_id): movie = Movie.get_by_id(int(movie_id)) params = {"movie" : movie} return self.render_template("edit_movie.html", params=params)
def get(self, movie_id): movie = Movie.get_by_id(int(movie_id)) movie.done = "Yes" movie.put() return self.redirect_to("list")
def get(self, movie_id): movie = Movie.get_by_id(int(movie_id)) movie.key.delete() return self.redirect_to("trash")
def get(self, movie_id): movie = Movie.get_by_id(int(movie_id)) movie.deleted = False movie.put() return self.redirect_to("trash")