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 users_get(self, request): """Exposes an API endpoint to obtain the details of the User Args: request: Void message request, because the info comes from the JWT Returns: An Instance containing the User Details. """ #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() list_of_voted_movies = [] list_of_voted_movie_keys_to_exclude = [] for user_movie_relation in list_of_voted_movies_query: current_movie = Movie.query(Movie.key==user_movie_relation.movie).get() list_of_voted_movies.append(current_movie.to_message()) #puts the voted movie keys in a list to exclude list_of_voted_movie_keys_to_exclude.append(current_movie.key) #all movies in the system total_list_of_movies = Movie.query().order(Movie.title) #removes the voted movies from the total list_of_not_voted_movies_query = [res for res in total_list_of_movies.fetch() if res.key not in list_of_voted_movie_keys_to_exclude] #transforms the movies to messages list_of_not_voted_movies = [system_movie.to_message() for system_movie in list_of_not_voted_movies_query] return selected_user.to_message(votes_movies=list_of_voted_movies, not_votes_movies=list_of_not_voted_movies)
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)