def post(self, list_id, session=None): """ Add movies to list by ID """ try: ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: raise NotFoundError("list_id %d does not exist" % list_id) data = request.json movie_identifiers = data.get("movie_identifiers", []) # Validates ID type based on allowed ID for id_name in movie_identifiers: if list(id_name)[0] not in MovieListBase().supported_ids: raise BadRequest("movie identifier %s is not allowed" % id_name) title, year = data["movie_name"], data.get("movie_year") movie = ml.get_movie_by_title_and_year(list_id=list_id, title=title, year=year, session=session) if movie: raise Conflict('movie with name "%s" already exist in list %d' % (title, list_id)) movie = ml.MovieListMovie() movie.title = title movie.year = year movie.ids = ml.get_db_movie_identifiers(identifier_list=movie_identifiers, session=session) movie.list_id = list_id session.add(movie) session.commit() response = jsonify(movie.to_dict()) response.status_code = 201 return response
def get(self, list_id, session=None): """ Get movies by list ID """ args = movies_parser.parse_args() page = args.get('page') page_size = args.get('page_size') start = page_size * (page - 1) stop = start + page_size descending = bool(args.get('order') == 'desc') kwargs = { 'start': start, 'stop': stop, 'list_id': list_id, 'order_by': args.get('sort_by'), 'descending': descending, 'session': session } try: ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: return {'status': 'error', 'message': 'list_id %d does not exist' % list_id}, 404 count = ml.get_movies_by_list_id(count=True, **kwargs) movies = [movie.to_dict() for movie in ml.get_movies_by_list_id(**kwargs)] pages = int(ceil(count / float(page_size))) number_of_movies = min(page_size, count) return jsonify({'movies': movies, 'number_of_movies': number_of_movies, 'total_number_of_movies': count, 'page': page, 'total_number_of_pages': pages})
def post(self, list_id, session=None): """ Add movies to list by ID """ try: ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: return {'status': 'error', 'message': 'list_id %d does not exist' % list_id}, 404 data = request.json movie_identifiers = data.get('movie_identifiers', []) # Validates ID type based on allowed ID # TODO pass this to json schema validation for id_name in movie_identifiers: if set(id_name.keys()) & set(MovieListBase().supported_ids) == set([]): return {'status': 'error', 'message': 'movie identifier %s is not allowed' % id_name}, 501 if 'movie_name' in data: title, year = data['movie_name'], data.get('movie_year') else: title, year = split_title_year(data['title']) movie = ml.get_movie_by_title(list_id=list_id, title=title, session=session) if movie: return {'status': 'error', 'message': 'movie with name "%s" already exist in list %d' % (title, list_id)}, 500 movie = ml.MovieListMovie() movie.title = title movie.year = year movie.ids = ml.get_db_movie_identifiers(identifier_list=movie_identifiers, session=session) movie.list_id = list_id session.add(movie) session.commit() response = jsonify({'movie': movie.to_dict()}) response.status_code = 201 return response
def post(self, list_id, session=None): """ Add movies to list by ID """ try: ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: raise NotFoundError('list_id %d does not exist' % list_id) data = request.json movie_identifiers = data.get('movie_identifiers', []) # Validates ID type based on allowed ID for id_name in movie_identifiers: if list(id_name)[0] not in MovieListBase().supported_ids: raise BadRequest('movie identifier %s is not allowed' % id_name) title, year = data['movie_name'], data.get('movie_year') movie = ml.get_movie_by_title_and_year(list_id=list_id, title=title, year=year, session=session) if movie: raise Conflict('movie with name "%s" already exist in list %d' % (title, list_id)) movie = ml.MovieListMovie() movie.title = title movie.year = year movie.ids = ml.get_db_movie_identifiers( identifier_list=movie_identifiers, session=session) movie.list_id = list_id session.add(movie) session.commit() response = jsonify(movie.to_dict()) response.status_code = 201 return response
def get(self, list_id, session=None): """ Get movies by list ID """ args = movies_parser.parse_args() # Pagination and sorting params page = args['page'] per_page = args['per_page'] sort_by = args['sort_by'] sort_order = args['order'] start = per_page * (page - 1) stop = start + per_page descending = bool(sort_order == 'desc') kwargs = { 'start': start, 'stop': stop, 'list_id': list_id, 'order_by': sort_by, 'descending': descending, 'session': session } try: ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: raise NotFoundError('list_id %d does not exist' % list_id) total_items = ml.get_movies_by_list_id(count=True, **kwargs) if not total_items: return jsonify([]) movies = [ movie.to_dict() for movie in ml.get_movies_by_list_id(**kwargs) ] total_pages = int(ceil(total_items / float(per_page))) if page > total_pages: raise NotFoundError('page %s does not exist' % page) # Actual results in page actual_size = min(len(movies), per_page) # Get pagination headers pagination = pagination_headers(total_pages, total_items, actual_size, request) # Create response rsp = jsonify(movies) # Add link header to response rsp.headers.extend(pagination) return rsp
def get(self, list_id, session=None): """ Get movies by list ID """ args = movies_parser.parse_args() # Pagination and sorting params page = args['page'] per_page = args['per_page'] sort_by = args['sort_by'] sort_order = args['order'] start = per_page * (page - 1) stop = start + per_page descending = bool(sort_order == 'desc') kwargs = { 'start': start, 'stop': stop, 'list_id': list_id, 'order_by': sort_by, 'descending': descending, 'session': session } try: ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: raise NotFoundError('list_id %d does not exist' % list_id) total_items = ml.get_movies_by_list_id(count=True, **kwargs) if not total_items: return jsonify([]) movies = [movie.to_dict() for movie in ml.get_movies_by_list_id(**kwargs)] total_pages = int(ceil(total_items / float(per_page))) if page > total_pages: raise NotFoundError('page %s does not exist' % page) # Actual results in page actual_size = min(len(movies), per_page) # Get pagination headers pagination = pagination_headers(total_pages, total_items, actual_size, request) # Create response rsp = jsonify(movies) # Add link header to response rsp.headers.extend(pagination) return rsp
def get(self, list_id, session=None): """ Get movies by list ID """ args = movies_parser.parse_args() page = args.get('page') page_size = args.get('page_size') start = page_size * (page - 1) stop = start + page_size if args.get('order') == 'desc': descending = True else: descending = False kwargs = { 'start': start, 'stop': stop, 'list_id': list_id, 'order_by': args.get('sort_by'), 'descending': descending, 'session': session } try: movie_list = ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: return { 'status': 'error', 'message': 'list_id %d does not exist' % list_id }, 404 movies = [ movie.to_dict() for movie in ml.get_movies_by_list_id(**kwargs) ] return jsonify({'movies': movies})
def post(self, list_id, session=None): """ Add movies to list by ID """ try: movie_list = ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: return {'status': 'error', 'message': 'list_id %d does not exist' % list_id}, 404 data = request.json # Validates ID type based on allowed ID # TODO pass this to json schema validation for id_name in data.get('movie_identifiers'): if set(id_name.keys()) & set(allowed_ids) == set([]): return {'status': 'error', 'message': 'movie identifier %s is not allowed' % id_name}, 501 if 'movie_name' in data: title, year = data['movie_name'], data.get('movie_year') else: title, year = split_title_year(data['title']) movie = ml.get_movie_by_title(list_id=list_id, title=title, session=session) if movie: return {'status': 'error', 'message': 'movie with name "%s" already exist in list %d' % (title, list_id)}, 500 movie = ml.MovieListMovie() movie.title = title movie.year = year movie.ids = ml.get_db_movie_identifiers(identifier_list=data.get('movie_identifiers'), session=session) movie.list_id = list_id session.add(movie) session.commit() response = jsonify({'movie': movie.to_dict()}) response.status_code = 201 return response
def get(self, list_id, session=None): """ Get movies by list ID """ args = movies_parser.parse_args() page = args.get('page') page_size = args.get('page_size') start = page_size * (page - 1) stop = start + page_size if args.get('order') == 'desc': descending = True else: descending = False kwargs = { 'start': start, 'stop': stop, 'list_id': list_id, 'order_by': args.get('sort_by'), 'descending': descending, 'session': session } try: movie_list = ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: return {'status': 'error', 'message': 'list_id %d does not exist' % list_id}, 404 movies = [movie.to_dict() for movie in ml.get_movies_by_list_id(**kwargs)] return jsonify({'movies': movies})
def get(self, list_id, session=None): """ Get list by ID """ try: movie_list = ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: raise NotFoundError("list_id %d does not exist" % list_id) return jsonify(movie_list.to_dict())
def get(self, list_id, session=None): """ Get list by ID """ try: movie_list = ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: raise NotFoundError('list_id %d does not exist' % list_id) return jsonify(movie_list.to_dict())
def delete(self, list_id, session=None): """ Delete list by ID """ try: movie_list = ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: raise NotFoundError('list_id %d does not exist' % list_id) session.delete(movie_list) return success_response('successfully deleted list')
def get(self, list_id, session=None): """ Get list by ID """ try: movie_list = ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: return {'status': 'error', 'message': 'list_id %d does not exist' % list_id}, 404 return jsonify(movie_list.to_dict())
def delete(self, list_id, session=None): """ Delete list by ID """ try: movie_list = ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: raise NotFoundError("list_id %d does not exist" % list_id) session.delete(movie_list) return success_response("successfully deleted list")
def delete(self, list_id, session=None): """ Delete list by ID """ try: movie_list = ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: return {'status': 'error', 'message': 'list_id %d does not exist' % list_id}, 404 session.delete(movie_list) return {}
def get(self, list_id, session=None): """ Get movies by list ID """ args = movies_parser.parse_args() page = args.get('page') page_size = args.get('page_size') start = page_size * (page - 1) stop = start + page_size descending = bool(args.get('order') == 'desc') kwargs = { 'start': start, 'stop': stop, 'list_id': list_id, 'order_by': args.get('sort_by'), 'descending': descending, 'session': session } try: ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: return { 'status': 'error', 'message': 'list_id %d does not exist' % list_id }, 404 count = ml.get_movies_by_list_id(count=True, **kwargs) movies = [ movie.to_dict() for movie in ml.get_movies_by_list_id(**kwargs) ] pages = int(ceil(count / float(page_size))) number_of_movies = min(page_size, count) return jsonify({ 'movies': movies, 'number_of_movies': number_of_movies, 'total_number_of_movies': count, 'page': page, 'total_number_of_pages': pages })
def get(self, list_id, session=None): """ Get movies by list ID """ args = movies_parser.parse_args() # Pagination and sorting params page = args["page"] per_page = args["per_page"] sort_by = args["sort_by"] sort_order = args["order"] start = per_page * (page - 1) stop = start + per_page descending = sort_order == "desc" kwargs = { "start": start, "stop": stop, "list_id": list_id, "order_by": sort_by, "descending": descending, "session": session, } try: list = ml.get_list_by_id(list_id=list_id, session=session) except NoResultFound: raise NotFoundError("list_id %d does not exist" % list_id) total_items = list.movies.count() if not total_items: return jsonify([]) movies = [movie.to_dict() for movie in ml.get_movies_by_list_id(**kwargs)] total_pages = int(ceil(total_items / float(per_page))) if page > total_pages: raise NotFoundError("page %s does not exist" % page) # Actual results in page actual_size = min(len(movies), per_page) # Get pagination headers pagination = pagination_headers(total_pages, total_items, actual_size, request) # Create response rsp = jsonify(movies) # Add link header to response rsp.headers.extend(pagination) return rsp