mirror of https://gitlab.federez.net/re2o/re2o
13 changed files with 198 additions and 62 deletions
@ -1,20 +0,0 @@ |
|||||
# -*- coding: utf-8 -*- |
|
||||
# Generated by Django 1.10.7 on 2018-06-28 19:57 |
|
||||
from __future__ import unicode_literals |
|
||||
|
|
||||
from django.db import migrations, models |
|
||||
|
|
||||
|
|
||||
class Migration(migrations.Migration): |
|
||||
|
|
||||
dependencies = [ |
|
||||
('cotisations', '0032_chequepayment_comnpaypayment'), |
|
||||
] |
|
||||
|
|
||||
operations = [ |
|
||||
migrations.AlterField( |
|
||||
model_name='comnpaypayment', |
|
||||
name='payment_id', |
|
||||
field=models.CharField(blank=True, default='', max_length=255), |
|
||||
), |
|
||||
] |
|
||||
@ -1,10 +1,12 @@ |
|||||
from django import forms |
from django import forms |
||||
from django.utils.translation import ugettext_lazy as _l |
from django.utils.translation import ugettext_lazy as _l |
||||
|
|
||||
from cotisations.models import Banque as Bank |
from re2o.mixins import FormRevMixin |
||||
|
from cotisations.models import Facture as Invoice |
||||
|
|
||||
|
|
||||
class ChequeForm(forms.Form): |
class InvoiceForm(FormRevMixin, forms.ModelForm): |
||||
"""A simple form to get the bank a the cheque number.""" |
"""A simple form to get the bank a the cheque number.""" |
||||
bank = forms.ModelChoiceField(Bank.objects.all(), label=_l("Bank")) |
class Meta: |
||||
number = forms.CharField(label=_l("Cheque number")) |
model = Invoice |
||||
|
fields = ['banque', 'cheque'] |
||||
|
|||||
@ -0,0 +1,59 @@ |
|||||
|
from django import forms |
||||
|
from django.utils.translation import ugettext as _ |
||||
|
from django.utils.translation import ugettext_lazy as _l |
||||
|
|
||||
|
from . import PAYMENT_METHODS, find_payment_method |
||||
|
|
||||
|
|
||||
|
def payment_method_factory(payment, *args, **kwargs): |
||||
|
payment_method = kwargs.pop('instance', find_payment_method(payment)) |
||||
|
if payment_method is not None: |
||||
|
return forms.modelform_factory(type(payment_method), fields='__all__')( |
||||
|
*args, |
||||
|
instance=payment_method, |
||||
|
**kwargs |
||||
|
) |
||||
|
return PaymentMethodForm(payment_method, *args, **kwargs) |
||||
|
|
||||
|
|
||||
|
class PaymentMethodForm(forms.Form): |
||||
|
"""A special form which allows you to add a payment method to a `Payment` |
||||
|
objects if it hasn't one yet, or to edit the existing payment method. |
||||
|
|
||||
|
To do so it replaces itself with a `modelform_factory`. |
||||
|
""" |
||||
|
|
||||
|
payment_method = forms.ChoiceField( |
||||
|
label=_l("Special payment method"), |
||||
|
required=False |
||||
|
) |
||||
|
|
||||
|
def __init__(self, payment_method, *args, **kwargs): |
||||
|
super(PaymentMethodForm, self).__init__(*args, **kwargs) |
||||
|
if payment_method is None: |
||||
|
prefix = kwargs.get('prefix', None) |
||||
|
self.fields['payment_method'].choices = [(i,p.NAME) for (i,p) in enumerate(PAYMENT_METHODS)] |
||||
|
self.fields['payment_method'].choices.insert(0, ('', _l('no'))) |
||||
|
self.fields['payment_method'].widget.attrs = { |
||||
|
'id': 'paymentMethodSelect' |
||||
|
} |
||||
|
self.templates = [ |
||||
|
forms.modelform_factory(p.PaymentMethod, fields='__all__')(prefix=prefix) |
||||
|
for p in PAYMENT_METHODS |
||||
|
] |
||||
|
else: |
||||
|
self.fields = {} |
||||
|
|
||||
|
def save(self, *args, payment=None, **kwargs): |
||||
|
commit = kwargs.pop('commit', True) |
||||
|
choice = self.cleaned_data['payment_method'] |
||||
|
if choice=='': |
||||
|
return |
||||
|
choice = int(choice) |
||||
|
model = PAYMENT_METHODS[choice].PaymentMethod |
||||
|
form = forms.modelform_factory(model, fields='__all__')(self.data, prefix=self.prefix) |
||||
|
payment_method = form.save(commit=False) |
||||
|
payment_method.payment = payment |
||||
|
if commit: |
||||
|
payment_method.save() |
||||
|
return payment_method |
||||
@ -0,0 +1,21 @@ |
|||||
|
from django.db import models |
||||
|
|
||||
|
from cotisations.models import Paiement |
||||
|
|
||||
|
|
||||
|
class PaymentMethodMixin: |
||||
|
"""The base class for payment models. They should inherit from this.""" |
||||
|
payment = models.OneToOneField( |
||||
|
Paiement, |
||||
|
related_name='payment_method', |
||||
|
editable=False |
||||
|
) |
||||
|
|
||||
|
def end_payment(self, invoice, request): |
||||
|
"""Redefine this method in order to get a different ending to the |
||||
|
payment session if you whish. |
||||
|
|
||||
|
Must return a HttpResponse-like object. |
||||
|
""" |
||||
|
return self.payment.end_payment( |
||||
|
invoice, request, use_payment_method=False) |
||||
Loading…
Reference in new issue