Пример #1
0
 def post(self):
     data = self.__parse_request__()
     exists = BookModel.find_by_name(data.get('name'))
     if exists:
         return {
             'message':
             "A book with ['name': {}] already exists".format(exists.name)
         }, 409
     authors = []
     a = AuthorModel.find_by_name(data.get('author_name'))
     if a:
         authors.append(a)
     else:
         new_author = AuthorModel(data.get('author_name'),
                                  data.get('author_bd'),
                                  data.get('author_city'),
                                  data.get('author_country'))
         authors.append(new_author)
         new_author.save_to_db()
     new_book = BookModel(data.get('isbn'), data.get('name'), authors,
                          data.get('genre'), data.get('year'),
                          data.get('editorial'), data.get('language'),
                          data.get('price'), data.get('synopsis'),
                          data.get('description'), data.get('num_pages'),
                          data.get('cover_type'), data.get('num_sales'),
                          data.get('total_available'),
                          data.get('cover_image_url'),
                          data.get('back_cover_image_url'))
     new_book.save_to_db()
     return new_book.json(), 200
Пример #2
0
 def put(self, idd):
     data = self.__parse_request__()
     exists = BookModel.find_by_id(idd)
     if not exists:
         return {
             'message': "A book with ['id': {}] not found".format(idd)
         }, 404
     authors = []
     exists.author = []
     exists.save_to_db()
     a = AuthorModel.find_by_name(data.get('author_name'))
     if a:
         authors.append(a)
     else:
         new_author = AuthorModel(data.get('author_name'),
                                  data.get('author_bd'),
                                  data.get('author_city'),
                                  data.get('author_country'))
         authors.append(new_author)
         new_author.save_to_db()
     exists.delete_from_db()
     new_book = BookModel(data.get('isbn'), data.get('name'), authors,
                          data.get('genre'), data.get('year'),
                          data.get('editorial'), data.get('language'),
                          data.get('price'), data.get('synopsis'),
                          data.get('description'), data.get('num_pages'),
                          data.get('cover_type'), data.get('num_sales'),
                          data.get('total_available'),
                          data.get('cover_image_url'),
                          data.get('back_cover_image_url'))
     new_book.id = idd
     new_book.save_to_db()
     return new_book.json(), 200
Пример #3
0
 def delete(self, name):
     authors = AuthorModel.find_by_name(name)
     for author in authors:
         if author.name == name:
             author.delete_from_db()
             return {'message': 'Author deleted.'}
     return {'message': f"Author '{name}' not found."}, 404
Пример #4
0
 def get(self):
     data = self.__parse_request__()
     query = data.get('name')
     books = [(a.json()['book']['name'].lower(), 'book')
              for a in BookModel.query.all()]
     authors = [(a.json()['name'].lower(), 'author')
                for a in AuthorModel.query.all()]
     isbn = [(a.json()['book']['ISBN'], 'isbn')
             for a in BookModel.query.all()]
     repo = books + authors + isbn
     books = []
     for i, j in repo:
         if str(query).lower() in str(i):
             if j == 'book':
                 b = BookModel.query.filter(
                     BookModel.name.like(i)).first().json()
                 if b not in books:
                     books.append(b)
             elif j == 'isbn':
                 b = BookModel.query.filter(
                     BookModel.isbn.like(i)).first().json()
                 if b not in books:
                     books.append(b)
             else:
                 author = AuthorModel.find_by_name(i.title())
                 for book in BookModel.query:
                     for a in book.author:
                         if a == author:
                             b = book.json()
                             if b not in books:
                                 books.append(b)
     return {'books': books}, 200
Пример #5
0
    def put(self, name):
        """PUT request that creates an author, provided a name and description, image_url, wiki_url"""

        # Parse the application/json data
        data = Author.parser.parse_args()

        # Request to the model to find the author
        author = AuthorModel.find_by_name(name)

        # If found
        if author:
            # We update its variables
            author.name = data['name']
            author.description = data['description']
            author.image_url = data['image_url']
            author.wiki_url = data['wiki_url']
        else:
            # Else we create it
            author = AuthorModel(**data)

        # Then we save
        author.save_to_db()

        # We return the updated author in json
        return author.json()
Пример #6
0
    def post(self):
        """POST request that creates an author, provided a name and description, image_url, wiki_url"""

        # Parse the application/json data
        data = Author.parser.parse_args()

        # Look if we find the author by its name
        if AuthorModel.find_by_name(data['name']):
            return {
                'message':
                "An author with name '{}' already exists.".format(data['name'])
            }, 400

        # If user doesn't exists then we create it
        author = AuthorModel(**data)

        # we try push and commit
        try:
            author.save_to_db()

        except:
            #if error
            return {"message": "An error occurred inserting the author."}, 500

        # We return a json of the author
        return author.json(), 201
Пример #7
0
    def post(self, name):
        """POST request that creates an author, provided a name and description, image_url, wiki_url"""

        # Request to the model and if found
        if AuthorModel.find_by_name(name):

            # Return meessage that author exists
            return {
                'message':
                "An Author with name '{}' already exists.".format(name)
            }, 400

        # Parse the application/json data
        data = Author.parser.parse_args()

        # We pass the arguments to the model
        author = AuthorModel(name, **data)

        # Try to save
        try:
            author.save_to_db()
        except:
            # if error
            return {"message": "An error occurred inserting the Author."}, 500

        # Return a json of the created author
        return author.json(), 201
Пример #8
0
    def post(self):
        # Create a new author with the data passed to us.
        parser = reqparse.RequestParser(
        )  # create parameters parser from request
        # define all input parameters need and its type
        parser.add_argument('name',
                            type=str,
                            required=True,
                            help="This field cannot be left blanck")
        parser.add_argument('birth_date',
                            type=str,
                            required=True,
                            help="This field cannot be left blanck")
        parser.add_argument('city',
                            type=str,
                            required=True,
                            help="This field cannot be left blanck")
        parser.add_argument('country',
                            type=str,
                            required=True,
                            help="This field cannot be left blanck")

        data = parser.parse_args()

        exists = AuthorModel.find_by_name(data.name)
        if exists:
            return {'message': "Author already exists"}, 409

        # The ID is the following to the last one
        new_author = AuthorModel(data.get('name'), data.get('birth_date'),
                                 data.get('city'), data.get('country'))
        new_author.save_to_db()
        return {'message': "OK"}, 201
Пример #9
0
    def post(self, name):
        for author in AuthorModel.find_by_name(name):
            if author.name == name:
                return {'message': f'User: {name} already exists.'}

        author = AuthorModel(name)
        author.save_to_db()

        return author.json(), 201
Пример #10
0
    def get(self, author_name):
        '''Get books by author id'''
        author = AuthorModel.find_by_name(author_name)
        if not author:
            return [], 404
        try:
            books = AuthorModel.get_all_books_from_author(author.id)
        except Exception as e:
            return [], 404
        book_list = [book.json() for book in books]

        return book_list, 200
Пример #11
0
    def get(self, name):
        """GET request that deals with requests that look for a author provided its name"""

        # Request to the model
        Author = AuthorModel.find_by_name(name)

        # if found
        if Author:

            # Return a json
            return Author.json()

        # if not
        return {'message': 'Author not found'}, 404
Пример #12
0
    def get(self):
        reqdata = NewsList.parser.parse_args()

        if reqdata['search_key'] is None:
            return [n.json() for n in NewsModel.find_all()]

        found_news = NewsModel.find_any(reqdata['search_key'])
        authors_news = []
        for author in AuthorModel.find_by_name(reqdata['search_key']):
            authors_news += NewsModel.find_by_author(author.id)

        for news in authors_news:
            if news in found_news:
                found_news.remove(found_news.index(news))

        return [n.json() for n in found_news + authors_news]
Пример #13
0
 def get(self, name):
     for author in AuthorModel.find_by_name(name):
         if author.name == name:
             return author.json()
     return {'message': f"Author '{name}' not found."}, 404