Esempio n. 1
0
def movie_list_list(options):
    """List movie list"""
    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 {}'.format(
                options.list_name))
            return
    header = ['Movie Name', 'Movie year']
    header += MovieListBase().supported_ids
    table_data = [header]
    movies = get_movies_by_list_id(movie_list.id,
                                   order_by='added',
                                   descending=True,
                                   session=session)
    for movie in movies:
        movie_row = [movie.title, movie.year or '']
        for identifier in MovieListBase().supported_ids:
            movie_row.append(movie.identifiers.get(identifier, ''))
        table_data.append(movie_row)
    title = '{} Movies in movie list: `{}`'.format(len(movies),
                                                   options.list_name)
    try:
        table = TerminalTable(options.table_type,
                              table_data,
                              title,
                              drop_columns=[5, 2, 4])
        console(table.output)
    except TerminalTableError as e:
        console('ERROR: %s' % str(e))
Esempio n. 2
0
def movie_list_keyword_type(identifier):
    if identifier.count('=') != 1:
        raise ArgumentTypeError('Received identifier in wrong format: %s, '
                                ' should be in keyword format like `imdb_id=tt1234567`' % identifier)
    name, value = identifier.split('=', 2)
    if name not in MovieListBase().supported_ids:
        raise ArgumentTypeError(
            'Received unsupported identifier ID %s. Should be one of %s' % (
                identifier, ' ,'.join(MovieListBase().supported_ids)))
    return {name: value}
Esempio n. 3
0
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))
Esempio n. 4
0
 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
Esempio n. 5
0
    def put(self, list_id, movie_id, session=None):
        """ Sets movie identifiers """
        try:
            movie = ml.get_movie_by_id(list_id=list_id,
                                       movie_id=movie_id,
                                       session=session)
        except NoResultFound:
            return {
                'status':
                'error',
                'message':
                'could not find movie with id %d in list %d' %
                (movie_id, 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:
            if set(id_name.keys()) & set(MovieListBase().supported_ids) == set(
                []):
                return {
                    'status': 'error',
                    'message': 'movie identifier %s is not allowed' % id_name
                }, 501
        movie.ids[:] = ml.get_db_movie_identifiers(identifier_list=data,
                                                   movie_id=movie_id,
                                                   session=session)
        session.commit()
        return jsonify(movie.to_dict())
Esempio n. 6
0
    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(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=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
Esempio n. 7
0
    def test_identifiers(self, api_client, schema_match):
        rsp = api_client.get('/movie_list/identifiers/')
        assert rsp.status_code == 200, 'Response code is %s' % rsp.status_code
        data = json.loads(rsp.get_data(as_text=True))
        errors = schema_match(OC.return_identifiers, data)
        assert not errors

        identifiers = MovieListBase().supported_ids
        assert data == identifiers
Esempio n. 8
0
    def put(self, list_id, movie_id, session=None):
        """ Sets movie identifiers """
        try:
            movie = ml.get_movie_by_id(list_id=list_id,
                                       movie_id=movie_id,
                                       session=session)
        except NoResultFound:
            raise NotFoundError('could not find movie with id %d in list %d' %
                                (movie_id, list_id))
        data = request.json

        # Validates ID type based on allowed ID
        for id_name in data:
            if list(id_name)[0] not in MovieListBase().supported_ids:
                raise BadRequest('movie identifier %s is not allowed' %
                                 id_name)
        movie.ids[:] = ml.get_db_movie_identifiers(identifier_list=data,
                                                   movie_id=movie_id,
                                                   session=session)
        session.commit()
        return jsonify(movie.to_dict())
Esempio n. 9
0
 def get(self, session=None):
     """ Return a list of supported movie list identifiers """
     return jsonify(MovieListBase().supported_ids)
Esempio n. 10
0
    @api.response(404, model=default_error_schema)
    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 {}


movie_identifiers_doc = "Use movie identifier using the following format:\n[{'ID_NAME: 'ID_VALUE'}]." \
                        " Has to be one of %s" % " ,".join(MovieListBase().supported_ids)

movies_parser = api.parser()
movies_parser.add_argument('sort_by',
                           choices=('id', 'added', 'title', 'year'),
                           default='title',
                           help='Sort by attribute')
movies_parser.add_argument('order',
                           choices=('desc', 'asc'),
                           default='desc',
                           help='Sorting order')
movies_parser.add_argument('page', type=int, default=1, help='Page number')
movies_parser.add_argument('page_size',
                           type=int,
                           default=10,
                           help='Number of movies per page')