예제 #1
0
def request(request):
    today = date.today()

    book = Book.objects.get(pk=int(request.GET.get("book")))
    cardowner = Cardowner.objects.get(user=request.user)

    if (
        len(
            LendRequest.objects.filter(
                requester=cardowner, book=book, date__year=today.year, date__month=today.month, date__day=today.day
            )
        )
        >= 10
    ):

        return HttpResponse(status=403)

    lend_request = LendRequest()
    lend_request.requester = cardowner
    lend_request.book = book
    book_requests = LendRequest.objects.filter(book=book, status=LendRequest.WAITING)
    if len(book_requests) == 0:
        lend_request.status = LendRequest.FOR_LEND
        mailer.send_borrow_invite(cardowner, book)
    else:
        lend_request.status = LendRequest.WAITING

    lend_request.save()

    return HttpResponse(status=200)
예제 #2
0
    def save_model(self, request, obj, form, change):
        if obj.handback and 'handback' in form.changed_data:
            book_requests = LendRequest.objects.filter(book=obj.borrowed_item, status=LendRequest.WAITING).order_by('date')
            if len(book_requests) > 0:
                lend_request = book_requests[0]
                lend_request.status = LendRequest.FOR_LEND
                mailer.send_borrow_invite(lend_request.requester, obj.borrowed_item)
                lend_request.save()

        obj.save()