2018-07-07 22:01:06 +00:00
|
|
|
# -*- mode: python; coding: utf-8 -*-
|
2020-11-23 16:06:37 +00:00
|
|
|
# Re2o est un logiciel d'administration développé initiallement au Rézo Metz. Il
|
2018-07-07 22:01:06 +00:00
|
|
|
# se veut agnostique au réseau considéré, de manière à être installable en
|
|
|
|
# quelques clics.
|
|
|
|
#
|
|
|
|
# Copyright © 2018 Hugo Levy-Falk
|
|
|
|
#
|
|
|
|
# This program is free software; you can redistribute it and/or modify
|
|
|
|
# it under the terms of the GNU General Public License as published by
|
|
|
|
# the Free Software Foundation; either version 2 of the License, or
|
|
|
|
# (at your option) any later version.
|
|
|
|
#
|
|
|
|
# This program is distributed in the hope that it will be useful,
|
|
|
|
# but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
# GNU General Public License for more details.
|
|
|
|
#
|
|
|
|
# You should have received a copy of the GNU General Public License along
|
|
|
|
# with this program; if not, write to the Free Software Foundation, Inc.,
|
|
|
|
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
|
2018-07-02 19:13:13 +00:00
|
|
|
from django import forms
|
2018-06-23 17:54:20 +00:00
|
|
|
from django.utils.translation import ugettext_lazy as _
|
2018-07-02 19:13:13 +00:00
|
|
|
|
2018-07-02 20:14:51 +00:00
|
|
|
from . import PAYMENT_METHODS
|
|
|
|
from cotisations.utils import find_payment_method
|
2018-07-02 19:13:13 +00:00
|
|
|
|
2019-11-04 16:55:03 +00:00
|
|
|
|
2018-07-03 14:46:29 +00:00
|
|
|
def payment_method_factory(payment, *args, creation=True, **kwargs):
|
2018-07-07 22:01:06 +00:00
|
|
|
"""This function finds the right payment method form for a given payment.
|
|
|
|
|
|
|
|
If the payment has a payment method, returns a ModelForm of it. Else if
|
|
|
|
it is the creation of the payment, a `PaymentMethodForm`.
|
2018-07-16 19:46:13 +00:00
|
|
|
Else `None`.
|
2018-07-07 22:01:06 +00:00
|
|
|
|
2018-07-13 15:14:39 +00:00
|
|
|
Args:
|
|
|
|
payment: The payment
|
|
|
|
*args: arguments passed to the form
|
|
|
|
creation: Should be True if you are creating the payment
|
|
|
|
**kwargs: passed to the form
|
2018-07-07 22:01:06 +00:00
|
|
|
|
2018-07-13 15:14:39 +00:00
|
|
|
Returns:
|
2018-07-16 19:46:13 +00:00
|
|
|
A form or None
|
2018-07-07 22:01:06 +00:00
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
payment_method = kwargs.pop("instance", find_payment_method(payment))
|
2018-07-02 19:13:13 +00:00
|
|
|
if payment_method is not None:
|
2019-11-04 16:55:03 +00:00
|
|
|
return forms.modelform_factory(type(payment_method), fields="__all__")(
|
|
|
|
*args, instance=payment_method, **kwargs
|
2018-07-02 19:13:13 +00:00
|
|
|
)
|
2018-07-03 14:46:29 +00:00
|
|
|
elif creation:
|
2018-07-07 22:01:06 +00:00
|
|
|
return PaymentMethodForm(*args, **kwargs)
|
2018-07-02 19:13:13 +00:00
|
|
|
|
|
|
|
|
|
|
|
class PaymentMethodForm(forms.Form):
|
|
|
|
"""A special form which allows you to add a payment method to a `Payment`
|
2018-07-07 22:01:06 +00:00
|
|
|
object.
|
2018-07-02 19:13:13 +00:00
|
|
|
"""
|
|
|
|
|
|
|
|
payment_method = forms.ChoiceField(
|
2018-06-23 17:54:20 +00:00
|
|
|
label=_("Special payment method"),
|
2019-11-04 16:55:03 +00:00
|
|
|
help_text=_(
|
2019-11-16 14:02:32 +00:00
|
|
|
"Warning: you will not be able to change the payment"
|
|
|
|
" method later. But you will be allowed to edit the other"
|
|
|
|
" options."
|
2018-07-03 14:46:29 +00:00
|
|
|
),
|
2019-11-04 16:55:03 +00:00
|
|
|
required=False,
|
2018-07-02 19:13:13 +00:00
|
|
|
)
|
|
|
|
|
2018-07-07 22:01:06 +00:00
|
|
|
def __init__(self, *args, **kwargs):
|
2018-07-02 19:13:13 +00:00
|
|
|
super(PaymentMethodForm, self).__init__(*args, **kwargs)
|
2019-11-04 16:55:03 +00:00
|
|
|
prefix = kwargs.get("prefix", None)
|
|
|
|
self.fields["payment_method"].choices = [
|
|
|
|
(i, p.NAME) for (i, p) in enumerate(PAYMENT_METHODS)
|
|
|
|
]
|
2019-11-16 14:02:32 +00:00
|
|
|
self.fields["payment_method"].choices.insert(0, ("", _("No")))
|
2019-11-04 16:55:03 +00:00
|
|
|
self.fields["payment_method"].widget.attrs = {"id": "paymentMethodSelect"}
|
2018-07-07 22:01:06 +00:00
|
|
|
self.templates = [
|
2019-11-04 16:55:03 +00:00
|
|
|
forms.modelform_factory(p.PaymentMethod, fields="__all__")(prefix=prefix)
|
2018-07-07 22:01:06 +00:00
|
|
|
for p in PAYMENT_METHODS
|
|
|
|
]
|
2018-07-02 19:13:13 +00:00
|
|
|
|
2018-07-05 13:21:51 +00:00
|
|
|
def clean(self):
|
2018-07-07 22:01:06 +00:00
|
|
|
"""A classic `clean` method, except that it replaces
|
|
|
|
`self.payment_method` by the payment method object if one has been
|
|
|
|
found. Tries to call `payment_method.valid_form` if it exists.
|
|
|
|
"""
|
2018-07-05 13:21:51 +00:00
|
|
|
super(PaymentMethodForm, self).clean()
|
2019-11-04 16:55:03 +00:00
|
|
|
choice = self.cleaned_data["payment_method"]
|
|
|
|
if choice == "":
|
2018-07-02 19:13:13 +00:00
|
|
|
return
|
|
|
|
choice = int(choice)
|
|
|
|
model = PAYMENT_METHODS[choice].PaymentMethod
|
2019-11-04 16:55:03 +00:00
|
|
|
form = forms.modelform_factory(model, fields="__all__")(
|
|
|
|
self.data, prefix=self.prefix
|
|
|
|
)
|
2018-07-05 13:21:51 +00:00
|
|
|
self.payment_method = form.save(commit=False)
|
2019-11-04 16:55:03 +00:00
|
|
|
if hasattr(self.payment_method, "valid_form"):
|
2018-07-05 13:21:51 +00:00
|
|
|
self.payment_method.valid_form(self)
|
|
|
|
return self.cleaned_data
|
|
|
|
|
|
|
|
def save(self, payment, *args, **kwargs):
|
2018-07-07 22:01:06 +00:00
|
|
|
"""Saves the payment method.
|
|
|
|
|
|
|
|
Tries to call `payment_method.alter_payment` if it exists.
|
|
|
|
"""
|
2019-11-04 16:55:03 +00:00
|
|
|
commit = kwargs.pop("commit", True)
|
|
|
|
if not hasattr(self, "payment_method"):
|
2018-07-13 11:16:51 +00:00
|
|
|
return None
|
2018-07-05 13:21:51 +00:00
|
|
|
self.payment_method.payment = payment
|
2019-11-04 16:55:03 +00:00
|
|
|
if hasattr(self.payment_method, "alter_payment"):
|
2018-07-05 13:21:51 +00:00
|
|
|
self.payment_method.alter_payment(payment)
|
2018-07-02 19:13:13 +00:00
|
|
|
if commit:
|
2018-07-05 13:21:51 +00:00
|
|
|
payment.save()
|
|
|
|
self.payment_method.save()
|
|
|
|
return self.payment_method
|