From b805c615e92e1151c591d623eb5c6dd88095b683 Mon Sep 17 00:00:00 2001
From: Tom Teichler <tom.teichler@teckids.org>
Date: Sat, 19 Mar 2022 23:54:52 +0100
Subject: [PATCH] Move payment preferences to model

---
 aleksis/apps/tezor/forms.py       | 14 +++++++++-
 aleksis/apps/tezor/models/base.py | 43 +++++++++++++++++--------------
 aleksis/apps/tezor/preferences.py | 30 ---------------------
 3 files changed, 37 insertions(+), 50 deletions(-)

diff --git a/aleksis/apps/tezor/forms.py b/aleksis/apps/tezor/forms.py
index 64a9821..a395065 100644
--- a/aleksis/apps/tezor/forms.py
+++ b/aleksis/apps/tezor/forms.py
@@ -1,6 +1,6 @@
 from django.utils.translation import gettext as _
 
-from material import Layout, Row
+from material import Layout, Row, Fieldset
 
 from aleksis.core.forms import ActionForm
 from aleksis.core.mixins import ExtensibleForm
@@ -29,14 +29,26 @@ class EditClientForm(ExtensibleForm):
 
     layout = Layout(
         Row("name", "email"),
+        Fieldset(
+            _("Payment pledge"),
+            Row("pledge_enabled"),
+        ),
         Fieldset(
             _("Sofort / Klarna"),
+            "sofort_enabled",
             Row("sofort_api_id", "sofort_api_key", "sofort_project_id"),
         ),
         Fieldset(
             _("PayPal"),
+            "paypal_enabled",
             Row("paypal_client_id", "paypal_secret", "paypal_capture"),
         ),
+        Fieldset(
+            _("Debit"),
+            "sdd_enabled",
+            Row("sdd_creditor", "sdd_creditor_identifier"),
+            Row("sdd_iban", "sdd_bic")
+        ),
     )
 
     class Meta:
diff --git a/aleksis/apps/tezor/models/base.py b/aleksis/apps/tezor/models/base.py
index 398e07f..a57f180 100644
--- a/aleksis/apps/tezor/models/base.py
+++ b/aleksis/apps/tezor/models/base.py
@@ -1,8 +1,11 @@
+from django.core.validators import RegexValidator
 from django.db import models
+from django.db.models import Q
 from django.utils.translation import gettext_lazy as _
 
 from localflavor.generic.models import IBANField, BICField
 
+from aleksis.core.util.core_helpers import get_site_preferences
 from aleksis.core.mixins import ExtensibleModel
 
 
@@ -10,54 +13,56 @@ class Client(ExtensibleModel):
     name = models.CharField(verbose_name=_("Name"), max_length=255)
     email = models.EmailField(verbose_name=_("Email"))
 
-    sofort_api_id = models.CharField(verbose_name=_("Sofort / Klarna API ID"), blank=True)
-    sofort_api_key = models.CharField(verbose_name=_("Sofort / Klarna API key"), blank=True)
-    sofort_project_id = models.CharField(verbose_name=_("Sofort / Klarna Project ID"), blank=True)
+    sofort_enabled = models.BooleanField(verbose_name=_("Sofort / Klarna enabled"), default=False)
+    sofort_api_id = models.CharField(verbose_name=_("Sofort / Klarna API ID"), blank=True, max_length=255)
+    sofort_api_key = models.CharField(verbose_name=_("Sofort / Klarna API key"), blank=True, max_length=255)
+    sofort_project_id = models.CharField(verbose_name=_("Sofort / Klarna Project ID"), blank=True, max_length=255)
 
-    paypal_client_id = models.CharField(verbose_name=_("PayPal client ID"), blank=True)
-    paypal_secret = models.CharField(verbose_name=_("PayPal secret"), blank=True)
+    paypal_enabled = models.BooleanField(verbose_name=_("PayPal enabled"), default=False)
+    paypal_client_id = models.CharField(verbose_name=_("PayPal client ID"), blank=True, max_length=255)
+    paypal_secret = models.CharField(verbose_name=_("PayPal secret"), blank=True, max_length=255)
     paypal_capture = models.BooleanField(
         verbose_name=_("Use PayPal Authorize & Capture"), default=False
     )
 
-    sdd_creditor = models.CharField(verbose_name=_("SEPA Direct Debit - Creditor name"), blank=True)
+
+    sdd_enabled = models.BooleanField(verbose_name=_("Debit enabled"), default=False)
+    sdd_creditor = models.CharField(verbose_name=_("SEPA Direct Debit - Creditor name"), blank=True, max_length=255)
     sdd_creditor_identifier = models.CharField(
-        verbose_name=_("SEPA Direct Debit - Creditor identifier"), blank=True
+        verbose_name=_("SEPA Direct Debit - Creditor identifier"), blank=True, max_length=35, validators=[RegexValidator("^[A-Z]{2}[0-9]{2}[A-Z0-9]{1,31}$")]
     )
     sdd_iban = IBANField(verbose_name=_("IBAN of bank account"), blank=True)
     sdd_bic = BICField(verbose_name=_("BIC/SWIFT code of bank"), blank=True)
 
-    class Meta:
-        sofort_enabled = get_site_preferences("payments__sofort_enabled")
-        paypal_enabled = get_site_preferences("payments__paypal_enabled")
-        debit_enabled = get_site_preferences("payments__debit_enabled")
+    pledge_enabled = models.BooleanField(verbose_name=_("Pledge enabled"), default=False)
 
+    class Meta:
         constraints = [
             models.UniqueConstraint(fields=["name", "site"], name="uniq_client_per_site"),
             models.CheckConstraint(
-                check=(
+                check=((
                     Q(sofort_enabled=True)
                     & ~Q(sofort_api_id="")
                     & ~Q(sofort_api_key="")
                     & ~Q(sofort_project_id="")
                 )
-                | Q(sofort_enabled=False),
+                | Q(sofort_enabled=False)),
                 name="sofort_enabled_configured",
             ),
             models.CheckConstraint(
-                check=(
-                    Q(debit_enabled=True)
+                check=((
+                    Q(sdd_enabled=True)
                     & ~Q(sdd_creditor="")
                     & ~Q(sdd_creditor_identifier="")
                     & ~Q(sdd_iban="")
                     & ~Q(sdd_bic="")
                 )
-                | Q(debit_enabled=False),
-                name="debit_enabled_configured",
+                | Q(sdd_enabled=False)),
+                name="sdd_enabled_configured",
             ),
             models.CheckConstraint(
-                check=(Q(paypal_enabled=True) & ~Q(paypal_client_id="") & ~Q(paypal_secret=""))
-                | Q(paypal_enabled=False),
+                check=((Q(paypal_enabled=True) & ~Q(paypal_client_id="") & ~Q(paypal_secret=""))
+                | Q(paypal_enabled=False)),
                 name="paypal_enabled_configured",
             ),
         ]
diff --git a/aleksis/apps/tezor/preferences.py b/aleksis/apps/tezor/preferences.py
index 820854b..d22df09 100644
--- a/aleksis/apps/tezor/preferences.py
+++ b/aleksis/apps/tezor/preferences.py
@@ -32,33 +32,3 @@ class EnablePledge(BooleanPreference):
     verbose_name = _("Enable pledged payments")
     default = False
     required = False
-
-@site_preferences_registry.register
-class EnablePayPal(BooleanPreference):
-    """Payment paypal payment backend - enable or not."""
-
-    section = payments
-    name = "paypal_enabled"
-    verbose_name = _("Enable PayPal payments")
-    default = False
-    required = False
-
-@site_preferences_registry.register
-class EnableSofort(BooleanPreference):
-    """Payment sofort payment backend - enable or not."""
-
-    section = payments
-    name = "sofort_enabled"
-    verbose_name = _("Enable Sofort payments")
-    default = False
-    required = False
-
-@site_preferences_registry.register
-class EnableDebit(BooleanPreference):
-    """Payment debit payment backend - enable or not."""
-
-    section = payments
-    name = "sdd_enabled"
-    verbose_name = _("Enable debit payments")
-    default = False
-    required = False
-- 
GitLab