inventorymgr/borrowstates.py
def checkout() -> Response:
    """API endpoint to checkout a list of items to a user."""
    checkout_request = api.CheckoutRequest.parse_obj(request.json)
    now = _utcnow()

    borrowing_user = User.query.get(checkout_request.borrowing_user_id)
    if borrowing_user is None:
        return make_response({"reason": "no_such_user"}, 400)

    borrowed_items = [
        BorrowableItem.query.get(item_id)
        for item_id in checkout_request.borrowed_item_ids
    ]

    if any(item is None for item in borrowed_items):
        return make_response({"reason": "nonexistent_item"}, 400)

    unqualified_for = [
        item
        for item in borrowed_items
        if not has_required_qualifications(borrowing_user, item)
    ]

    if unqualified_for:
        return make_response({"reason": "missing_qualifications"}, 403)

    if any_item_already_borrowed(borrowed_items):
        return make_response({"reason": "already_borrowed"}, 400)

    borrowstates = [
        BorrowState(borrowing_user=borrowing_user, borrowed_item=item, received_at=now)
        for item in borrowed_items
    ]

    for borrow_state in borrowstates:
        db.session.add(borrow_state)

    db.session.add(
        LogEntry(
            action="checkout",
            timestamp=now,
            subject=borrowing_user,
            items=borrowed_items,
        )
    )
    db.session.commit()

    response = make_response(
        api.BorrowStateCollection(borrowstates=borrowstates).json(), 200
    )
    response.headers["Content-Type"] = "application/json; encoding=utf-8"
    return response
API endpoint to checkout a list of items to a user.
API endpoint to checkout a list of items to a user.
API endpoint to checkout a list of items to a user .
def checkout() -> Response:
    checkout_request = api.CheckoutRequest.parse_obj(request.json)
    now = _utcnow()
    borrowing_user = User.query.get(checkout_request.borrowing_user_id)
    if borrowing_user is None:
        return make_response({"reason": "no_such_user"}, 400)
    borrowed_items = [
        BorrowableItem.query.get(item_id)
        for item_id in checkout_request.borrowed_item_ids
    ]
    if any(item is None for item in borrowed_items):
        return make_response({"reason": "nonexistent_item"}, 400)
    unqualified_for = [
        item
        for item in borrowed_items
        if not has_required_qualifications(borrowing_user, item)
    ]
    if unqualified_for:
        return make_response({"reason": "missing_qualifications"}, 403)
    if any_item_already_borrowed(borrowed_items):
        return make_response({"reason": "already_borrowed"}, 400)
    borrowstates = [
        BorrowState(borrowing_user=borrowing_user, borrowed_item=item, received_at=now)
        for item in borrowed_items
    ]
    for borrow_state in borrowstates:
        db.session.add(borrow_state)
    db.session.add(
        LogEntry(
            action="checkout",
            timestamp=now,
            subject=borrowing_user,
            items=borrowed_items,
        )
    )
    db.session.commit()
    response = make_response(
        api.BorrowStateCollection(borrowstates=borrowstates).json(), 200
    )
    response.headers["Content-Type"] = "application/json; encoding=utf-8"
    return response
def checkout ( ) -> Response : checkout_request = api . CheckoutRequest . parse_obj ( request . json ) now = _utcnow ( ) borrowing_user = User . query . get ( checkout_request . borrowing_user_id ) if borrowing_user is None : return make_response ( { "reason" : "no_such_user" } , 400 ) borrowed_items = [ BorrowableItem . query . get ( item_id ) for item_id in checkout_request . borrowed_item_ids ] if any ( item is None for item in borrowed_items ) : return make_response ( { "reason" : "nonexistent_item" } , 400 ) unqualified_for = [ item for item in borrowed_items if not has_required_qualifications ( borrowing_user , item ) ] if unqualified_for : return make_response ( { "reason" : "missing_qualifications" } , 403 ) if any_item_already_borrowed ( borrowed_items ) : return make_response ( { "reason" : "already_borrowed" } , 400 ) borrowstates = [ BorrowState ( borrowing_user = borrowing_user , borrowed_item = item , received_at = now ) for item in borrowed_items ] for borrow_state in borrowstates : db . session . add ( borrow_state ) db . session . add ( LogEntry ( action = "checkout" , timestamp = now , subject = borrowing_user , items = borrowed_items , ) ) db . session . commit ( ) response = make_response ( api . BorrowStateCollection ( borrowstates = borrowstates ) . json ( ) , 200 ) response . headers [ "Content-Type" ] = "application/json; encoding=utf-8" return response
API endpoint to checkout a list of items to a user.
API endpoint to checkout a list of items to a user .
"""API endpoint to checkout a list of items to a user."""