def renew_loan_librarian(request, pk): """View function for renewing a specific Loan by librarian.""" loan = get_object_or_404(Loan, 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 = RenewLoanForm(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) loan.return_due = form.cleaned_data['renewal_date'] loan.save() # redirect to a new URL: return HttpResponseRedirect(reverse('all_loans') ) # 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 = RenewLoanForm(initial={'renewal_date': proposed_renewal_date}) context = { 'form': form, 'loan': loan, } return render(request, 'library/loan_renew_librarian.html', context)
def test_renew_form_date_max(self): date = timezone.now() + datetime.timedelta(weeks=4) form = RenewLoanForm(data={'renewal_date': date}) self.assertTrue(form.is_valid())
def test_renew_form_date_today(self): date = datetime.date.today() form = RenewLoanForm(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 = RenewLoanForm(data={'renewal_date': date}) self.assertFalse(form.is_valid())
def test_renew_form_date_in_past(self): date = datetime.date.today() - datetime.timedelta(days=1) form = RenewLoanForm(data={'renewal_date': date}) self.assertFalse(form.is_valid())
def test_renew_form_date_field_help_text(self): form = RenewLoanForm() self.assertEqual(form.fields['renewal_date'].help_text, 'Enter a date between now and 4 weeks (default 3).')
def test_renew_form_date_field_label(self): form = RenewLoanForm() self.assertTrue(form.fields['renewal_date'].label == None or form.fields['renewal_date'].label == 'renewal date')