views.py 1.34 KB
Newer Older
1 2 3 4 5 6 7 8 9 10 11 12 13 14
"""Payment

Here are defined some views dedicated to cheque payement.
"""

from django.urls import reverse
from django.shortcuts import redirect, render, get_object_or_404
from django.contrib.auth.decorators import login_required
from django.contrib import messages
from django.utils.translation import ugettext as _

from cotisations.models import Facture as Invoice

from .models import ChequePayment
15
from .forms import InvoiceForm
16 17 18 19 20 21 22 23 24 25 26 27 28 29 30


@login_required
def cheque(request, invoice_pk):
    invoice = get_object_or_404(Invoice, pk=invoice_pk)
    payment_method = getattr(invoice.paiement, 'payment_method', None)
    if invoice.valid or not isinstance(payment_method, ChequePayment):
        messages.error(
            request,
            _("You cannot pay this invoice with a cheque.")
        )
        return redirect(reverse(
            'users:profil',
            kwargs={'userid': request.user.pk}
        ))
31
    form = InvoiceForm(request.POST or None, instance=invoice)
32
    if form.is_valid():
33 34 35 36 37 38 39
        form.instance.valid = True
        form.save()
        return form.instance.paiement.end_payment(
            form.instance,
            request,
            use_payment_method=False
        )
40 41
    return render(
        request,
42 43 44 45 46
        'cotisations/payment.html',
        {
            'form': form,
            'amount': invoice.prix_total()
        }
47
    )