Пример #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 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
Пример #4
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()
Пример #5
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
Пример #6
0
    def put(self, idd):

        # 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=lambda s: datetime.datetime.strptime(s, '%Y-%m-%d'),
            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()
        author = AuthorModel.find_by_id(idd)
        if author:
            author.delete_from_db()
            new_author = AuthorModel(data.get('name'), data.get('birth_date'),
                                     data.get('city'), data.get('country'))
            new_author.save_to_db()
            return {'message': "Author modified"}, 201
        return {'message': "Author with id [{}] Not found".format(idd)}, 409
Пример #7
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
Пример #8
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
Пример #9
0
    def post(self, name):
        if _author(name):
            return {"message": "author with that name already exists"}, 409

        data = Author.parser.parse_args()
        author = AuthorModel(name)

        try:
            author.save_to_db()
        except Exception:
            return {'message': 'can not save the note'}, 500

        return author.json(), 201
Пример #10
0
    def post(self, id):
        """POST request that deals with the creation of an a new author"""

        if AuthorModel.find_by_id(id):
            return {
                'message': "An Author with id '{}' already exists.".format(id)
            }, 400

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

        # Call the model
        author = AuthorModel(id, **data)

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

        # Return the json of the author
        return author.json(), 201
Пример #11
0
    def put(self, id):
        """PUT request that deals with the edit or a creation of an author with at a certain id"""

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

        # Call the model
        author = AuthorModel.find_by_id(id)

        # if exists
        if author:
            # Update the fields
            author.name = data['name']
            author.description = data['description']
            author.image_url = data['image_url']
            author.wiki_url = data['wiki_url']
        else:
            # Else we create
            author = AuthorModel(**data)
        # save and commit
        author.save_to_db()

        # Return json when all is done.
        return author.json()