Exemplo n.º 1
0
 def get_handler():
     """Return information about lend ``lend_id_``."""
     form_data = request.session.pop('form_data', None)
     if form_data is None:
         form = forms.LendNoteForm()
     else:
         form = forms.LendNoteForm(json.loads(form_data))
     return render(
         request,
         'elts/lend-id.html',
         {'lend': lend_, 'form': form}
     )
Exemplo n.º 2
0
 def get_handler():
     """Return a form for updating lend note ``lend_note_id_``."""
     form_data = request.session.pop('form_data', None)
     if form_data is None:
         form = forms.LendNoteForm(instance = lend_note_)
     else:
         form = forms.LendNoteForm(json.loads(form_data))
     return render(
         request,
         'elts/lend-note-id-update-form.html',
         {'lend_note': lend_note_, 'form': form}
     )
Exemplo n.º 3
0
    def post_handler():
        """Create a new lend note.

        Redirect user to ``lend_id`` view after handling request.

        """
        # For which lend is this note being created?
        try:
            lend_ = models.Lend.objects.get(
                id = request.POST.get('lend_id', None)
            )
        except models.Lend.DoesNotExist:
            return http.HttpResponse(status = 422)

        # Get note text and, if valid, save the note.
        form = forms.LendNoteForm(request.POST)
        if form.is_valid():
            models.LendNote(
                note_text = form.cleaned_data['note_text'],
                author_id = request.user,
                lend_id = lend_,
            ).save()
        else:
            request.session['form_data'] = json.dumps(form.data)

        # Return the user to this page whether or not the note was saved.
        return http.HttpResponseRedirect(
            reverse('elts.views.lend_id', args = [lend_.id])
        )
Exemplo n.º 4
0
 def test_has_is_complaint(self):
     """Create a LendNoteForm and set ``is_complaint``."""
     form = forms.LendNoteForm({
         'note_text': factories.note_note_text(),
         'is_complaint': factories.lend_note_is_complaint()
     })
     self.assertTrue(form.is_valid())
Exemplo n.º 5
0
    def put_handler():
        """Update lend note ``lend_note_id_``.

        If update succeeds, redirect user to ``lend_id`` view. Otherwise,
        redirect user to ``lend_note_id_update_form`` view.

        """
        form = forms.LendNoteForm(request.POST, instance = lend_note_)
        if form.is_valid():
            form.save()
            return http.HttpResponseRedirect(
                reverse('elts.views.lend_id', args = [lend_id_])
            )
        else:
            request.session['form_data'] = json.dumps(form.data)
            return http.HttpResponseRedirect(
                reverse(
                    'elts.views.lend_note_id_update_form',
                    args = [lend_note_id_]
                )
            )
Exemplo n.º 6
0
 def test_invalid_note_text(self):
     """Create an LendNoteForm and set an invalid note_text.``"""
     form = forms.LendNoteForm({
         'note_text': factories.invalid_note_note_text()
     })
     self.assertFalse(form.is_valid())
Exemplo n.º 7
0
 def test_missing_note_text(self):
     """Create an LendNoteForm without setting ``note_text``."""
     form = forms.LendNoteForm({})
     self.assertFalse(form.is_valid())
Exemplo n.º 8
0
 def test_valid(self):
     """Create a valid LendNoteForm."""
     form = forms.LendNoteForm({'note_text': factories.note_note_text()})
     self.assertTrue(form.is_valid())