Beispiel #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, 'binder/book_renew_librarian.html', context)
Beispiel #2
0
 def test_renew_form_date_field_help_text(self):
     """Test renewal_date help_text is as expected."""
     form = RenewBookForm()
     self.assertEqual(
         form.fields['renewal_date'].help_text,
         'Enter a date between now and 4 weeks (default 3).')
Beispiel #3
0
 def test_renew_form_date_field_label(self):
     """Test renewal_date label is 'renewal date'."""
     form = RenewBookForm()
     self.assertTrue(
         form.fields['renewal_date'].label is None or
         form.fields['renewal_date'].label == 'renewal date')
Beispiel #4
0
 def test_renew_form_date_max(self):
     """Test form is valid if renewal_date is within 4 weeks."""
     date = datetime.date.today() + datetime.timedelta(weeks=4)
     form = RenewBookForm(data={'renewal_date': date})
     self.assertTrue(form.is_valid())
Beispiel #5
0
 def test_renew_form_date_today(self):
     """Test form is valid if renewal_date is today."""
     date = datetime.date.today()
     form = RenewBookForm(data={'renewal_date': date})
     self.assertTrue(form.is_valid())
Beispiel #6
0
 def test_renew_form_date_too_far_in_future(self):
     """Test form is invalid if renewal_date more than 4 weeks from today."""
     date = datetime.date.today() + datetime.timedelta(weeks=4) + datetime.timedelta(days=1)
     form = RenewBookForm(data={'renewal_date': date})
     self.assertFalse(form.is_valid())
Beispiel #7
0
 def test_renew_form_date_in_past(self):
     """Test form is invalid if renewal_date is before today."""
     date = datetime.date.today() - datetime.timedelta(days=1)
     form = RenewBookForm(data={'renewal_date': date})
     self.assertFalse(form.is_valid())