summaryrefslogtreecommitdiffstats
path: root/combo/apps/lingo/forms.py
diff options
context:
space:
mode:
Diffstat (limited to 'combo/apps/lingo/forms.py')
-rw-r--r--combo/apps/lingo/forms.py69
1 files changed, 69 insertions, 0 deletions
diff --git a/combo/apps/lingo/forms.py b/combo/apps/lingo/forms.py
new file mode 100644
index 0000000..f19573a
--- /dev/null
+++ b/combo/apps/lingo/forms.py
@@ -0,0 +1,69 @@
+# lingo - basket and payment system
+# Copyright (C) 2019 Entr'ouvert
+#
+# This program is free software: you can redistribute it and/or modify it
+# under the terms of the GNU Affero General Public License as published
+# by the Free Software Foundation, either version 3 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 Affero General Public License for more details.
+#
+# You should have received a copy of the GNU Affero General Public License
+# along with this program. If not, see <http://www.gnu.org/licenses/>.
+
+
+from django import forms
+
+from .models import Regie
+
+
+def create_form_fields(parameters, json_field):
+ fields, initial = {}, {}
+ for param in parameters:
+ field_name = param['name']
+ if param['type'] is bool:
+ fields[field_name] = forms.BooleanField(label=param['caption'], required=False)
+ initial[field_name] = param['default']
+ if field_name in json_field:
+ initial[field_name] = json_field[field_name]
+ else:
+ raise NotImplementedError()
+ return fields, initial
+
+
+def compute_json_field(parameters, cleaned_data):
+ json_field = {}
+ for param in parameters:
+ param_name = param['name']
+ if param_name in cleaned_data:
+ json_field[param_name] = cleaned_data[param_name]
+ return json_field
+
+
+class RegieForm(forms.ModelForm):
+
+ class Meta:
+ model = Regie
+ fields = ['label', 'slug', 'description', 'payment_backend', 'is_default',
+ 'webservice_url', 'extra_fees_ws_url', 'payment_min_amount', 'text_on_success']
+
+ def __init__(self, *args, **kwargs):
+ super(RegieForm, self).__init__(*args, **kwargs)
+ fields, initial = create_form_fields(
+ self.instance.payment_backend.get_payment().get_parameters(scope='transaction'),
+ self.instance.transaction_options
+ )
+ self.fields.update(fields)
+ self.initial.update(initial)
+
+ def save(self):
+ instance = super(RegieForm, self).save()
+ instance.transaction_options = compute_json_field(
+ self.instance.payment_backend.get_payment().get_parameters(scope='transaction'),
+ self.cleaned_data
+ )
+ instance.save()
+ return instance