Example #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, 'catalog/book_renew_librarian.html', context)
Example #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():
            # 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, 'catalog/book_renew_librarian.html', context)
 def test_renew_form_date_field_label(self):
     form = RenewBookForm()
     form_label = form.fields['renewal_date'].label
     self.assertTrue(form_label == None or form_label == 'renewal date')
 def test_renew_form_date_max(self):
     date = timezone.localtime() + datetime.timedelta(weeks=4)
     form = RenewBookForm(data={'renewal_date': date})
     self.assertTrue(form.is_valid())
 def test_renew_form_date_today(self):
     date = datetime.date.today()
     form = RenewBookForm(data={'renewal_date': date})
     self.assertTrue(form.is_valid())
 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())
 def test_renew_form_date_in_pass(self):
     date = datetime.date.today() - datetime.timedelta(days=1)
     form = RenewBookForm(data={'renewal_date': date})
     self.assertFalse(form.is_valid())
 def test_renew_form_date_field_help_help_text(self):
     form = RenewBookForm()
     self.assertEqual(form.fields['renewal_date'].help_text, "Enter a date between now and 4 weeks (default: 3).")