def movie_list_add(options): with Session() as session: try: movie_list = get_list_by_exact_name(options.list_name) except NoResultFound: console('Could not find movie list with name {}, creating'.format(options.list_name)) movie_list = MovieListList(name=options.list_name) session.merge(movie_list) title, year = split_title_year(options.movie_title) console('Trying to lookup movie %s title' % title) entry = lookup_movie(title=title, session=session, identifiers=options.identifiers) if not entry: console('movie lookup failed for movie %s, aborting') return title = entry['movie_name'] movie = get_movie_by_title(list_id=movie_list.id, title=title, session=session) if not movie: console("Adding movie with title {} to list {}".format(title, movie_list.name)) movie = MovieListMovie(title=entry['movie_name'], year=year, list_id=movie_list.id) else: console("Movie with title {} already exist in list {}".format(title, movie_list.name)) id_list = [] if options.identifiers: id_list = options.identifiers else: for _id in MovieListBase().supported_ids: if entry.get(_id): id_list.append({_id: entry.get(_id)}) if id_list: console('Setting movie identifiers:', id_list) movie.ids = get_db_movie_identifiers(identifier_list=id_list, session=session) session.merge(movie) console('Successfully added movie {} to movie list {} '.format(title, movie_list.name))
def movie_list_add(options): with Session() as session: try: movie_list = get_list_by_exact_name(options.list_name) except NoResultFound: console('Could not find movie list with name {}, creating'.format(options.list_name)) movie_list = MovieListList(name=options.list_name) session.add(movie_list) session.merge(movie_list) title, year = split_title_year(options.movie_title) movie_exist = get_movie_by_title(list_id=movie_list.id, title=title, session=session) if movie_exist: console("Movie with the title {} already exist with list {}. Will replace identifiers if given".format( title, movie_list.name)) output = 'Successfully updated movie {} to movie list {} '.format(title, movie_list.name) else: console("Adding movie with title {} to list {}".format(title, movie_list.name)) movie_exist = MovieListMovie(title=title, year=year, list_id=movie_list.id) session.add(movie_exist) output = 'Successfully added movie {} to movie list {} '.format(title, movie_list.name) if options.identifiers: identifiers = [parse_identifier(identifier) for identifier in options.identifiers if options.identifiers] console('Adding identifiers {} to movie {}'.format(identifiers, title)) movie_exist.ids = get_db_movie_identifiers(identifier_list=identifiers, session=session) console(output)
def test_movie_list_sorting(self, api_client): with Session() as session: movie_list = MovieListList(name='test_list') session.add(movie_list) movie_list.movies.append(MovieListMovie(title='movie a', year=2005)) movie_list.movies.append(MovieListMovie(title='movie b', year=2004)) movie_list.movies.append(MovieListMovie(title='movie c', year=2003)) # Sort by title rsp = api_client.get('/movie_list/1/movies/?sort_by=title') assert rsp.status_code == 200, 'Response code is %s' % rsp.status_code data = json.loads(rsp.get_data(as_text=True)) assert data[0]['title'] == 'movie c' rsp = api_client.get('/movie_list/1/movies/?sort_by=title&order=asc') assert rsp.status_code == 200, 'Response code is %s' % rsp.status_code data = json.loads(rsp.get_data(as_text=True)) assert data[0]['title'] == 'movie a' # Sort by year rsp = api_client.get('/movie_list/1/movies/?sort_by=year') assert rsp.status_code == 200, 'Response code is %s' % rsp.status_code data = json.loads(rsp.get_data(as_text=True)) assert data[0]['year'] == 2005 rsp = api_client.get('/movie_list/1/movies/?sort_by=year&order=asc') assert rsp.status_code == 200, 'Response code is %s' % rsp.status_code data = json.loads(rsp.get_data(as_text=True)) assert data[0]['year'] == 2003 # Combine sorting and pagination rsp = api_client.get( '/movie_list/1/movies/?sort_by=year&per_page=2&page=2') assert rsp.status_code == 200, 'Response code is %s' % rsp.status_code data = json.loads(rsp.get_data(as_text=True)) assert data[0]['year'] == 2003
def test_movie_list_pagination(self, api_client, link_headers): base_movie = dict(title='title_', year=1900) number_of_movies = 200 with Session() as session: movie_list = MovieListList(name='test_list') session.add(movie_list) for i in range(number_of_movies): movie = copy.deepcopy(base_movie) movie['title'] += str(i) movie['year'] += i movie_list.movies.append(MovieListMovie(**movie)) # Default values rsp = api_client.get('/movie_list/1/movies/') assert rsp.status_code == 200, 'Response code is %s' % rsp.status_code data = json.loads(rsp.get_data(as_text=True)) assert len(data) == 50 # Default page size assert int(rsp.headers['total-count']) == 200 assert int(rsp.headers['count']) == 50 links = link_headers(rsp) assert links['last']['page'] == 4 assert links['next']['page'] == 2 # Change page size rsp = api_client.get('/movie_list/1/movies/?per_page=100') assert rsp.status_code == 200 data = json.loads(rsp.get_data(as_text=True)) assert len(data) == 100 assert int(rsp.headers['total-count']) == 200 assert int(rsp.headers['count']) == 100 links = link_headers(rsp) assert links['last']['page'] == 2 assert links['next']['page'] == 2 # Get different page rsp = api_client.get('/movie_list/1/movies/?page=2') assert rsp.status_code == 200 data = json.loads(rsp.get_data(as_text=True)) assert len(data) == 50 # Default page size assert int(rsp.headers['total-count']) == 200 assert int(rsp.headers['count']) == 50 links = link_headers(rsp) assert links['last']['page'] == 4 assert links['next']['page'] == 3 assert links['prev']['page'] == 1