From 60056904c8faa33127408aef59b06751313c4289 Mon Sep 17 00:00:00 2001
From: Dominik George <dominik.george@teckids.org>
Date: Sat, 19 Mar 2022 23:55:40 +0100
Subject: [PATCH] Add migration for payment provider preferences

---
 .../0007_client_payment_variants.py           | 134 ++++++++++++++++++
 1 file changed, 134 insertions(+)
 create mode 100644 aleksis/apps/tezor/migrations/0007_client_payment_variants.py

diff --git a/aleksis/apps/tezor/migrations/0007_client_payment_variants.py b/aleksis/apps/tezor/migrations/0007_client_payment_variants.py
new file mode 100644
index 0000000..26e258e
--- /dev/null
+++ b/aleksis/apps/tezor/migrations/0007_client_payment_variants.py
@@ -0,0 +1,134 @@
+# Generated by Django 3.2.12 on 2022-03-19 22:21
+
+import warnings
+
+import django.core.validators
+from django.db import migrations, models
+import localflavor.generic.models
+
+
+def configure_clients(apps, schema_editor):
+    db_alias = schema_editor.connection.alias
+
+    Client = apps.get_model("tezor", "Client")
+    SitePreferenceModel = apps.get_model("core", "SitePreferenceModel")
+
+    fields = ["sofort_api_id", "sofort_api_key", "sofort_project_id", "paypal_client_id", "paypal_secret", "paypal_capture", "pledge_enabled", "sdd_creditor", "sdd_creditor_identifier"]
+    values = {}
+    for field in fields:
+        try:
+            pref = SitePreferenceModel.objects.using(db_alias).get(section="payments", name=field)
+        except SitePreferenceModel.DoesNotExist:
+            continue
+
+        value = pref.raw_value
+        if value == "True":
+            value = True
+        elif value == "False":
+            value = False
+
+        values[field] = value
+        if not "enabled" in field:
+            provider = field.split("_")[0]
+            values[f"{provider}_enabled"] = True
+
+    for variant in ("sdd", "pledge", "paypal", "sofort"):
+        if values.get(f"{variant}_enabled", False):
+            for field in Client._meta.fields:
+                if field.name.startswith(f"{variant}_") and values.get(field.name, "") == "":
+                    values[f"{variant}_enabled"] = False
+                    warnings.warn(f"Payment variant {variant} enabled but {field.name} not configured!")
+
+    Client.objects.update(**values)
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('tezor', '0006_invoice_constraints'),
+    ]
+
+    operations = [
+        migrations.AddField(
+            model_name='client',
+            name='paypal_capture',
+            field=models.BooleanField(default=False, verbose_name='Use PayPal Authorize & Capture'),
+        ),
+        migrations.AddField(
+            model_name='client',
+            name='paypal_client_id',
+            field=models.CharField(blank=True, max_length=255, verbose_name='PayPal client ID'),
+        ),
+        migrations.AddField(
+            model_name='client',
+            name='paypal_enabled',
+            field=models.BooleanField(default=False, verbose_name='PayPal enabled'),
+        ),
+        migrations.AddField(
+            model_name='client',
+            name='paypal_secret',
+            field=models.CharField(blank=True, max_length=255, verbose_name='PayPal secret'),
+        ),
+        migrations.AddField(
+            model_name='client',
+            name='pledge_enabled',
+            field=models.BooleanField(default=False, verbose_name='Pledge enabled'),
+        ),
+        migrations.AddField(
+            model_name='client',
+            name='sdd_bic',
+            field=localflavor.generic.models.BICField(blank=True, max_length=11, verbose_name='BIC/SWIFT code of bank'),
+        ),
+        migrations.AddField(
+            model_name='client',
+            name='sdd_creditor',
+            field=models.CharField(blank=True, max_length=255, verbose_name='SEPA Direct Debit - Creditor name'),
+        ),
+        migrations.AddField(
+            model_name='client',
+            name='sdd_creditor_identifier',
+            field=models.CharField(blank=True, max_length=35, validators=[django.core.validators.RegexValidator('^[A-Z]{2}[0-9]{2}[A-Z0-9]{1,31}$')], verbose_name='SEPA Direct Debit - Creditor identifier'),
+        ),
+        migrations.AddField(
+            model_name='client',
+            name='sdd_enabled',
+            field=models.BooleanField(default=False, verbose_name='Debit enabled'),
+        ),
+        migrations.AddField(
+            model_name='client',
+            name='sdd_iban',
+            field=localflavor.generic.models.IBANField(blank=True, include_countries=None, max_length=34, use_nordea_extensions=False, verbose_name='IBAN of bank account'),
+        ),
+        migrations.AddField(
+            model_name='client',
+            name='sofort_api_id',
+            field=models.CharField(blank=True, max_length=255, verbose_name='Sofort / Klarna API ID'),
+        ),
+        migrations.AddField(
+            model_name='client',
+            name='sofort_api_key',
+            field=models.CharField(blank=True, max_length=255, verbose_name='Sofort / Klarna API key'),
+        ),
+        migrations.AddField(
+            model_name='client',
+            name='sofort_enabled',
+            field=models.BooleanField(default=False, verbose_name='Sofort / Klarna enabled'),
+        ),
+        migrations.AddField(
+            model_name='client',
+            name='sofort_project_id',
+            field=models.CharField(blank=True, max_length=255, verbose_name='Sofort / Klarna Project ID'),
+        ),
+        migrations.AddConstraint(
+            model_name='client',
+            constraint=models.CheckConstraint(check=models.Q(models.Q(('sofort_enabled', True), models.Q(('sofort_api_id', ''), _negated=True), models.Q(('sofort_api_key', ''), _negated=True), models.Q(('sofort_project_id', ''), _negated=True)), ('sofort_enabled', False), _connector='OR'), name='sofort_enabled_configured'),
+        ),
+        migrations.AddConstraint(
+            model_name='client',
+            constraint=models.CheckConstraint(check=models.Q(models.Q(('sdd_enabled', True), models.Q(('sdd_creditor', ''), _negated=True), models.Q(('sdd_creditor_identifier', ''), _negated=True), models.Q(('sdd_iban', ''), _negated=True), models.Q(('sdd_bic', ''), _negated=True)), ('sdd_enabled', False), _connector='OR'), name='sdd_enabled_configured'),
+        ),
+        migrations.AddConstraint(
+            model_name='client',
+            constraint=models.CheckConstraint(check=models.Q(models.Q(('paypal_enabled', True), models.Q(('paypal_client_id', ''), _negated=True), models.Q(('paypal_secret', ''), _negated=True)), ('paypal_enabled', False), _connector='OR'), name='paypal_enabled_configured'),
+        ),
+        migrations.RunPython(configure_clients),
+    ]
-- 
GitLab