Ejemplo n.º 1
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(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
Ejemplo n.º 2
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
Ejemplo n.º 3
0
def movie_list_add(options):
    with Session() as session:
        try:
            movie_list = plugin_movie_list.get_list_by_exact_name(
                options.list_name, session=session)
        except NoResultFound:
            console('Could not find movie list with name {}, creating'.format(
                options.list_name))
            movie_list = plugin_movie_list.MovieListList(
                name=options.list_name)
            session.add(movie_list)
            session.commit()

        title, year = split_title_year(options.movie_title)
        console('Trying to lookup movie title: `{}`'.format(title))
        movie_lookup = lookup_movie(title=title,
                                    session=session,
                                    identifiers=options.identifiers)
        if not movie_lookup:
            console('ERROR: movie lookup failed for movie {}, aborting'.format(
                options.movie_title))
            return

        title = movie_lookup['movie_name']
        movie = plugin_movie_list.get_movie_by_title_and_year(
            list_id=movie_list.id, title=title, year=year, session=session)
        if not movie:
            console("Adding movie with title {} to list {}".format(
                title, movie_list.name))
            movie = plugin_movie_list.MovieListMovie(title=title,
                                                     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 plugin_movie_list.MovieListBase().supported_ids:
                if movie_lookup.get(_id):
                    id_list.append({_id: movie_lookup.get(_id)})
        if id_list:
            console('Setting movie identifiers:')
            for ident in id_list:
                for key in ident:
                    console('{}: {}'.format(key, ident[key]))
            movie.ids = plugin_movie_list.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))