示例#1
0
class Author(Resource):

    def __init__(self, args):
        super().__init__(args)
        self.book_service = BookService()
        self.author_service = AuthorService()

    @api_app.doc(responses={200: "OK", 400: "Invalid argument"},
                 params={"id": "Specify author Id"})
    def get(self, id):
        try:
            author = self.author_service.get_author_by_id(id)
            result = {
                "message": "Found author by id: {0}".format(id),
                "name": author.name,
                "surname": author.surname
            }
            return result

        except Exception as e:
            book_namespace.abort(400, e.__doc__, status="Could not find author by id", statusCode="400")

    @api_app.doc(responses={200: "OK", 400: "Invalid argument"},
                 params={"id": "Specify author Id to remove"})
    def delete(self, id):
        try:
            self.author_service.delete_author_by_id(str(id))
            self.book_service.clear_author_books(str(id))
            return {
                "message": "Removed author by id: {0}".format(id)
            }

        except Exception as e:
            book_namespace.abort(400, e.__doc__, status="Could not remove author by id", statusCode="400")
示例#2
0
class BookList(Resource):

    def __init__(self, args):
        super().__init__(args)
        self.book_service = BookService()
        self.author_service = AuthorService()

    new_book_model = api_app.model("Book model",
                                   {
                                       "title": fields.String(required=True, description="Book title",
                                                              help="Title cannot be null", example="Bieguni"),
                                       "year": fields.Integer(required=True, description="Year of publication",
                                                              help="Year cannot be null", example="2007"),
                                       "author_id": fields.Integer(required=True, description="Author's Id ",
                                                                   help="Author's Id cannot be null")
                                   })

    @api_app.param(START, "The data will be returned from this position.")
    @api_app.param(LIMIT, "The max size of returned data.")
    @api_app.doc(responses={200: "OK"})
    def get(self):
        start = self.parse_request_arg_or_zero(request, START, "0")
        start = max(1, start)
        limit = self.parse_request_arg_or_zero(request, LIMIT, "50")

        paginated_book_response = self.book_service.get_paginated_books_response(start, limit)

        return paginated_book_response.get_json(request.base_url)

    def parse_request_arg_or_zero(self, request, param, default_value):
        val = request.args.get(param, default_value)
        val = int(val) if val.isdigit() else 0
        return val

    @api_app.expect(new_book_model)
    def post(self):
        try:
            book_req = BookRequest(request)
            app.logger.debug("id author: {0}".format(book_req.author_id))
            app.logger.debug("title: {0}".format(book_req.title))
            app.logger.debug("year: {0}".format(book_req.year))
            author = self.author_service.get_author_by_id(book_req.author_id)
            saved_book_id = self.book_service.add_book(book_req)

            result = {"message": "Added new book", "saved_book_id": saved_book_id}

            return result

        except KeyError as e:
            book_namespace.abort(400, e.__doc__, status="Could not save new book", statusCode="400")

        except BookAlreadyExistsException as e:
            book_namespace.abort(409, e.__doc__, status="Could not save new book. Already exists", statusCode="409")

        except AuthorNotFoundByIdException as e:
            book_namespace.abort(404, e.__doc__, status="Could not save new book. Author (by id) does not exist.",
                                 statusCode="404")
class AuthorID(Resource):
    def __init__(self, args):
        super().__init__(args)
        self.author_service = AuthorService()

    @api_app.doc(responses={
        200: "OK",
        400: "Invalid argument"
    },
                 params={"id": "Specify author Id"})
    @jwt_required
    def get(self, id):
        try:
            author = self.author_service.get_author_by_id(id)
            return author.__dict__

        except Exception as e:
            author_namespace.abort(400,
                                   e.__doc__,
                                   status="Could not find author by id",
                                   statusCode="400")

    @api_app.doc(responses={
        200: "OK",
        400: "Invalid argument"
    },
                 params={"id": "Specify author Id"})
    @jwt_required
    def delete(self, id):
        try:

            author_id = self.author_service.del_author_by_id(id)

            return {"message": "Removed author by id: {0}".format(author_id)}

        except Exception as e:
            author_namespace.abort(400,
                                   e.__doc__,
                                   status="Could not remove author by id",
                                   statusCode="400")
示例#4
0
class AuthorList(Resource):

    def __init__(self, args):
        super().__init__(args)
        self.author_service = AuthorService()
        self.book_service = BookService()

    new_author_model = api_app.model("Author model",
                                     {
                                         "name": fields.String(required=True, description="Author name",
                                                               help="Name cannot be blank"),
                                         "surname": fields.String(required=True, description="Author surname",
                                                                  help="Surname cannot be null")
                                     })

    @api_app.expect(new_author_model)
    def post(self):
        try:
            author_req = AuthorRequest(request)
            saved_author_id = self.author_service.add_author(author_req)

            result = {"message": "Added new author", "save_author_id": saved_author_id ,
                      "DELETE":"http://*****:*****@api_app.doc(responses={200: "OK"})
    def get(self):
        paginated_autor_response = self.author_service.get_paginated_authors_response()
        return paginated_autor_response

    def parse_request_arg_or_zero(self, request, param, default_value):
        val = request.args.get(param, default_value)
        val = int(val) if val.isdigit() else 0
        return val
class Author(Resource):
    def __init__(self, args):
        super().__init__(args)
        self.author_service = AuthorService()

    new_author_model = api_app.model(
        "Author model", {
            "name":
            fields.String(required=True,
                          description="Author name",
                          help="Name cannot be blank"),
            "surname":
            fields.String(required=True,
                          description="Author surname",
                          help="Surname cannot be null")
        })

    @api_app.expect(new_author_model)
    def post(self):
        try:
            author_req = AuthorRequest(request)
            saved_author_id = self.author_service.add_author(author_req)

            result = {
                "message": "Added new author",
                "save_author_id": saved_author_id
            }

            return result

        except AuthorAlreadyExistsException as e:
            author_namespace.abort(
                409,
                e.__doc__,
                status="Could not save author. Already exists",
                statusCode=409)
示例#6
0
 def __init__(self, args):
     super().__init__(args)
     self.book_service = BookService()
     self.author_service = AuthorService()
class Author(Resource):
    def __init__(self, args):
        super().__init__(args)
        self.author_service = AuthorService()

    new_author_model = api_app.model(
        "Author model", {
            "name":
            fields.String(required=True,
                          description="Author name",
                          help="Name cannot be blank"),
            "surname":
            fields.String(required=True,
                          description="Author surname",
                          help="Surname cannot be null")
        })

    @api_app.expect(new_author_model)
    @jwt_required
    def post(self):
        try:
            author_req = AuthorRequest(request)
            saved_author_id = self.author_service.add_author(author_req)

            result = {
                "message": "Added new author",
                "save_author_id": saved_author_id
            }

            return result

        except AuthorAlreadyExistsException as e:
            author_namespace.abort(
                409,
                e.__doc__,
                status="Could not save author. Already exists",
                statusCode=409)

    @api_app.doc(responses={
        200: "OK",
        400: "Invalid argument"
    },
                 params={
                     "name": "Specify author name",
                     "surname": "Specify author surname"
                 })
    @api_app.param("name", "Specify author name.")
    @api_app.param("surname", "Specify author surname.")
    @jwt_required
    def get(self):
        try:
            name = request.args.get("name")
            surname = request.args.get("surname")

            author = self.author_service.get_author_by_names(name, surname)
            return author.__dict__

        except Exception as e:
            author_namespace.abort(400,
                                   e.__doc__,
                                   status="Could not find author by names",
                                   statusCode="400")
 def __init__(self, args):
     super().__init__(args)
     self.bib_service = BibliographyService()
     self.author_service = AuthorService()
class BibliographyList(Resource):
    def __init__(self, args):
        super().__init__(args)
        self.bib_service = BibliographyService()
        self.author_service = AuthorService()

    new_bib_model = api_app.model(
        "Bibliography model", {
            "title":
            fields.String(required=True,
                          description="bibliography position title",
                          help="Title cannot be null",
                          example="Bieguni"),
            "year":
            fields.Integer(required=True,
                           description="Year of publication",
                           help="Year cannot be null",
                           example="2007"),
            "author_id":
            fields.Integer(required=True,
                           description="Author's Id ",
                           help="Author's Id cannot be null")
        })

    @api_app.param(START, "The data will be returned from this position.")
    @api_app.param(LIMIT, "The max size of returned data.")
    @api_app.doc(responses={200: "OK"})
    @jwt_required
    def get(self):
        username = get_raw_jwt()['name']

        start = self.parse_request_arg_or_zero(request, START, "0")
        start = max(1, start)
        all_user_bibs = self.bib_service.get_all_bibs(username)
        default_limit = len(all_user_bibs)
        limit = self.parse_request_arg_or_zero(request, LIMIT,
                                               str(default_limit))

        paginated_bib_response = self.bib_service.get_paginated_bibs_response(
            start, limit, username)

        return paginated_bib_response.get_json(request.base_url)

    def parse_request_arg_or_zero(self, request, param, default_value):
        val = request.args.get(param, default_value)
        val = int(val) if val.isdigit() else 0
        return val

    @api_app.expect(new_bib_model)
    @jwt_required
    def post(self):
        try:
            bib_req = BibliographyRequest(request)
            author = self.author_service.get_author_by_id(bib_req.author_id)
            username = get_raw_jwt()['name']
            saved_bib_id = self.bib_service.add_bib(bib_req, username)

            result = {
                "message": "Added new bibliography postion",
                "saved_bib_id": saved_bib_id
            }

            return result

        except KeyError as e:
            bibliography_namespace.abort(
                400,
                e.__doc__,
                status="Could not save new bibliography position",
                statusCode="400")

        except BibliographyAlreadyExistsException as e:
            bibliography_namespace.abort(
                409,
                e.__doc__,
                status=
                "Could not save new bibliography position. Already exists",
                statusCode="409")

        except AuthorNotFoundByIdException as e:
            bibliography_namespace.abort(
                404,
                e.__doc__,
                status=
                "Could not save new bibliography position. Author (by id) does not exist.",
                statusCode="404")