コード例 #1
0
def renew_book_librarian(request, pk):
    """View function for renewing a specific BookInstance by librarian."""
    book_instance = get_object_or_404(BookInstance, pk=pk)

    # If this is a POST request then process the Form data
    if request.method == 'POST':

        # Create a form instance and populate it with data from the request (binding):
        form = RenewBookForm(request.POST)

        # Check if the form is valid:
        if form.is_valid():
            # process the data in form.cleaned_data as required (here we just write it to the model due_back field)
            book_instance.due_back = form.cleaned_data['renewal_date']
            book_instance.save()

            # redirect to a new URL:
            return HttpResponseRedirect(reverse('all-borrowed'))

    # If this is a GET (or any other method) create the default form.
    else:
        proposed_renewal_date = datetime.date.today() + datetime.timedelta(
            weeks=3)
        form = RenewBookForm(initial={'renewal_date': proposed_renewal_date})

    context = {
        'form': form,
        'book_instance': book_instance,
    }

    return render(request, 'catalogue/book_renew_librarian.html', context)
コード例 #2
0
def renew_book_librarian(request, pk):
    book_instance = get_object_or_404(BookInstance, pk=pk)

    if request.method == 'POST':

        form = RenewBookForm(request.POST)

        if form.is_valid():
            book_instance.due_back = form.cleaned_data['renewal_date']
            book_instance.save()

            return HttpResponseRedirect(reverse('all-borrowed'))

    else:
        proposed_renewal_date = datetime.date.today() + datetime.timedelta(
            weeks=3)
        form = RenewBookForm(initial={'renewal_date': proposed_renewal_date})

    context = {
        'form': form,
        'book_instance': book_instance,
    }

    return render(request, 'catalog/book_renew_librarian.html', context)
コード例 #3
0
 def test_renew_form_date_field_label(self):
     form = RenewBookForm()
     self.assertTrue(form.fields['renewal_date'].label == None or form.fields['renewal_date'].label == 'renewal date')
コード例 #4
0
 def test_renew_form_date_max(self):
     date = timezone.now() + datetime.timedelta(weeks=4)
     form = RenewBookForm(data={'renewal_date': date})
     self.assertTrue(form.is_valid())
コード例 #5
0
 def test_renew_form_date_today(self):
     date = datetime.date.today()
     form = RenewBookForm(data={'renewal_date': date})
     self.assertTrue(form.is_valid())
コード例 #6
0
 def test_renew_form_date_too_far_in_future(self):
     date = datetime.date.today() + datetime.timedelta(weeks=4) + datetime.timedelta(days=1)
     form = RenewBookForm(data={'renewal_date': date})
     self.assertFalse(form.is_valid())
コード例 #7
0
 def test_renew_form_date_in_past(self):
     date = datetime.date.today() - datetime.timedelta(days=1)
     form = RenewBookForm(data={'renewal_date': date})
     self.assertFalse(form.is_valid())
コード例 #8
0
 def test_renew_form_date_field_help_text(self):
     form = RenewBookForm()
     self.assertEqual(form.fields['renewal_date'].help_text, 'Enter a date between now and 4 weeks (default 3).')