Пример #1
0
    def get(self):
        '''
        Gets the info for all books in the database.

        Input: None
        Output HTTP responses:
            200
        '''
        body = LibraryBuilder(items=[])
        books = Book.query.all()
        print(len(books))

        for book in books:
            item = LibraryBuilder(id=book.id,
                                  barcode=book.barcode,
                                  title=book.title,
                                  author=book.author,
                                  pubyear=book.pubyear,
                                  format=book.format,
                                  description=book.description,
                                  loantime=book.loantime,
                                  renewlimit=book.renewlimit)

            item.add_control("self", url_for("api.bookitem", book_id=book.id))
            item.add_control("profile", BOOK_PROFILE)
            body["items"].append(item)

        body.add_namespace("inlibris", LINK_RELATIONS_URL + "#")
        body.add_control("self", url_for("api.bookcollection"))
        body.add_control("profile", BOOK_PROFILE)
        body.add_control_all_patrons()
        body.add_control_add_book()

        return Response(json.dumps(body), 200, mimetype=MASON)
Пример #2
0
    def get(self):
        '''
        Gets the info for all the patrons in the database.

        Input: None
        Output HTTP responses:
            200
        '''

        body = LibraryBuilder(items=[])
        patrons = Patron.query.all()
        print(len(patrons))

        for patron in patrons:
            item = LibraryBuilder(id=patron.id,
                                  barcode=patron.barcode,
                                  firstname=patron.firstname,
                                  lastname=patron.lastname,
                                  email=patron.email,
                                  group=patron.group,
                                  status=patron.status,
                                  regdate=str(patron.regdate.date()))
            item.add_control("self",
                             url_for("api.patronitem", patron_id=patron.id))
            item.add_control("profile", PATRON_PROFILE)
            body["items"].append(item)

        body.add_namespace("inlibris", LINK_RELATIONS_URL)
        body.add_control("self", url_for("api.patroncollection"))
        body.add_control("profile", PATRON_PROFILE)
        body.add_control_add_patron()
        body.add_control_all_books()

        return Response(json.dumps(body), 200, mimetype=MASON)
Пример #3
0
    def get(self, patron_id):
        '''
        Get the info for all the loans by a patron.

        Input: patron_id
        Output HTTP responses:
            200 (patron_id is valid)
            404 (patron_id is invalid)
        '''

        patron = Patron.query.filter_by(id=patron_id).first()

        if patron is None:
            return create_error_response(404, "Patron not found", None)

        loans = Loan.query.filter_by(patron_id=patron_id).all()
        body = LibraryBuilder(items=[])

        for loan in loans:
            book_barcode = Book.query.filter_by(
                id=loan.book_id).first().barcode
            patron_barcode = Patron.query.filter_by(
                id=patron_id).first().barcode

            item = LibraryBuilder(id=loan.id,
                                  book_barcode=book_barcode,
                                  patron_barcode=patron_barcode,
                                  loandate=str(loan.loandate.date()),
                                  renewaldate=None if not loan.renewaldate else
                                  str(loan.renewaldate.date()),
                                  duedate=str(loan.duedate.date()),
                                  renewed=loan.renewed,
                                  status=loan.status)

            item.add_control("self",
                             url_for("api.loanitem", book_id=loan.book_id))
            item.add_control("profile", LOAN_PROFILE)
            body["items"].append(item)

        body.add_namespace("inlibris", LINK_RELATIONS_URL)
        body.add_control("self",
                         url_for("api.loansbypatron", patron_id=patron_id))
        body.add_control("author",
                         url_for("api.patronitem", patron_id=patron_id))
        body.add_control("profile", LOAN_PROFILE)
        body.add_control_all_patrons()
        body.add_control_all_books()
        body.add_control_add_loan(patron_id)

        return Response(json.dumps(body), 200, mimetype=MASON)
Пример #4
0
    def get(self, book_id):
        '''
        Gets the information for a single book.

        Input: book_id
        Output HTTP responses:
            200 OK (when book_id is valid)
            404 Not Found (when book_id is invalid)
        '''
        book = Book.query.filter_by(id=book_id).first()
        if book is None:
            return create_error_response(
                404, "Not found",
                "No book was found with the id {}".format(book_id))

        body = LibraryBuilder(id=book.id,
                              barcode=book.barcode,
                              title=book.title,
                              author=book.author,
                              pubyear=book.pubyear,
                              format=book.format,
                              description=book.description,
                              loantime=book.loantime,
                              renewlimit=book.renewlimit)

        body.add_namespace("inlibris", LINK_RELATIONS_URL)
        body.add_control("self", url_for("api.bookitem", book_id=book.id))
        body.add_control("profile", BOOK_PROFILE)
        body.add_control("collection", url_for("api.bookcollection"))
        body.add_control_holds_on(book_id)
        body.add_control_loan_of(book_id)
        body.add_control_edit_book(book_id)
        body.add_control_delete_book(book_id)

        return Response(json.dumps(body), 200, mimetype=MASON)
Пример #5
0
    def get(self, book_id):
        '''
        Gets the information for a single loan.

        Input: book_id
        Output HTTP responses:
            200 (when book_id is valid)
            400 (when book_id is valid but book is not loaned)
            404 (when book_id is invalid)
        '''

        book = Book.query.filter_by(id=book_id).first()
        if book is None:
            return create_error_response(404, "Book not found", None)

        loan = Loan.query.filter_by(book_id=book_id).first()
        if loan is None:
            return create_error_response(400, "Book not loaned", None)

        book_barcode = Book.query.filter_by(id=book_id).first().barcode
        patron_barcode = Patron.query.filter_by(
            id=loan.patron_id).first().barcode

        body = LibraryBuilder(id=loan.id,
                              book_barcode=book_barcode,
                              patron_barcode=patron_barcode,
                              loandate=str(loan.loandate.date()),
                              renewaldate=None if not loan.renewaldate else
                              str(loan.renewaldate.date()),
                              duedate=str(loan.duedate.date()),
                              renewed=loan.renewed,
                              status=loan.status)

        body.add_namespace("inlibris", LINK_RELATIONS_URL)
        body.add_control("self", url_for("api.loanitem", book_id=book_id))
        body.add_control("profile", LOAN_PROFILE)
        body.add_control("author",
                         url_for("api.patronitem", patron_id=loan.patron_id))
        body.add_control_loans_by(loan.patron_id)
        body.add_control_target_book(book_id)
        body.add_control_all_books()
        body.add_control_all_patrons()
        body.add_control_edit_loan(book_id)
        body.add_control_delete_loan(book_id)

        return Response(json.dumps(body), 200, mimetype=MASON)
Пример #6
0
    def get(self, patron_id):
        '''
        Gets the information for a single patron.

        Input: patron_id
        Output HTTP responses:
            200 OK (when patron_id is valid)
            404 Not Found (when patron_id is invalid)
        '''

        patron = Patron.query.filter_by(id=patron_id).first()
        if patron is None:
            return create_error_response(
                404, "Not found",
                "No patron was found with the id {}".format(patron_id))

        body = LibraryBuilder(id=patron.id,
                              barcode=patron.barcode,
                              firstname=patron.firstname,
                              lastname=patron.lastname,
                              email=patron.email,
                              group=patron.group,
                              status=patron.status,
                              regdate=str(patron.regdate.date()))

        body.add_namespace("inlibris", LINK_RELATIONS_URL)
        body.add_control("self", url_for("api.patronitem",
                                         patron_id=patron.id))
        body.add_control("profile", PATRON_PROFILE)
        body.add_control_loans_by(patron_id)
        body.add_control_holds_by(patron_id)
        body.add_control("collection", url_for("api.patroncollection"))
        body.add_control_edit_patron(patron_id)
        body.add_control_delete_patron(patron_id)

        return Response(json.dumps(body), 200, mimetype=MASON)