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)
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, 'book_renew_librarian.html', context)
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())
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).')
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')
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())
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())
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())
def test_renew_form_date_max(self): date = datetime.date.today() + datetime.timedelta(weeks=4) form_data = {'renewal_date': date} form = RenewBookForm(data=form_data) self.assertTrue(form.is_valid())
def test_renew_form_date_today(self): date = datetime.date.today() form_data = {'renewal_date': date} form = RenewBookForm(data=form_data) 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_data = {'renewal_date': date} form = RenewBookForm(data=form_data) self.assertFalse(form.is_valid())
def test_renew_form_date_in_past(self): date = datetime.date.today() - datetime.timedelta(days=1) form_data = {'renewal_date': date} form = RenewBookForm(data=form_data) self.assertFalse(form.is_valid())
def test_renwer_form_date_field_help_text(self): form = RenewBookForm() self.assertEqual( form.fields['renewal_date'].help_text, 'Seleccione una fecha entra hoy y 4 semanas (por defecto 3 semanas)' )