From 64c363cb50ef45ecd366a6d776b09fad4f7aa884 Mon Sep 17 00:00:00 2001
From: Emmanuel Cazenave <ecazenave@entrouvert.com>
Date: Thu, 9 May 2019 16:57:23 +0200
Subject: [PATCH 1/3] lingo: create PaymentBackend objects (#32441)

---
 .../migrations/0034_auto_20190426_1159.py     | 33 +++++++++++++++++++
 .../migrations/0035_auto_20190426_1124.py     | 28 ++++++++++++++++
 .../migrations/0036_auto_20190426_1202.py     | 29 ++++++++++++++++
 combo/apps/lingo/models.py                    | 20 ++++++++---
 4 files changed, 106 insertions(+), 4 deletions(-)
 create mode 100644 combo/apps/lingo/migrations/0034_auto_20190426_1159.py
 create mode 100644 combo/apps/lingo/migrations/0035_auto_20190426_1124.py
 create mode 100644 combo/apps/lingo/migrations/0036_auto_20190426_1202.py

diff --git a/combo/apps/lingo/migrations/0034_auto_20190426_1159.py b/combo/apps/lingo/migrations/0034_auto_20190426_1159.py
new file mode 100644
index 0000000..a7f7531
--- /dev/null
+++ b/combo/apps/lingo/migrations/0034_auto_20190426_1159.py
@@ -0,0 +1,33 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.18 on 2019-04-26 09:59
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+import jsonfield.fields
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('lingo', '0033_auto_20181204_2241'),
+    ]
+
+    operations = [
+        migrations.CreateModel(
+            name='PaymentBackend',
+            fields=[
+                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
+                ('label', models.CharField(max_length=64, verbose_name='Label')),
+                ('slug', models.SlugField(help_text='The identifier is used in webservice calls.', unique=True, verbose_name='Identifier')),
+                ('description', models.TextField(verbose_name='Description')),
+                ('service', models.CharField(choices=[(b'dummy', 'Dummy (for tests)'), (b'systempayv2', b'systempay (Banque Populaire)'), (b'sips', 'SIPS (Atos, France)'), (b'sips2', 'SIPS (Atos, other countries)'), (b'spplus', "SP+ (Caisse d'epargne)"), (b'ogone', 'Ingenico (formerly Ogone)'), (b'paybox', 'Paybox'), (b'payzen', 'PayZen'), (b'tipi', 'TIPI')], max_length=64, verbose_name='Payment Service')),
+                ('service_options', jsonfield.fields.JSONField(blank=True, default=dict, verbose_name='Payment Service Options')),
+            ],
+        ),
+        migrations.AddField(
+            model_name='regie',
+            name='payment_backend',
+            field=models.ForeignKey(null=True, on_delete=django.db.models.deletion.CASCADE, to='lingo.PaymentBackend'),
+        ),
+    ]
diff --git a/combo/apps/lingo/migrations/0035_auto_20190426_1124.py b/combo/apps/lingo/migrations/0035_auto_20190426_1124.py
new file mode 100644
index 0000000..30fb6ef
--- /dev/null
+++ b/combo/apps/lingo/migrations/0035_auto_20190426_1124.py
@@ -0,0 +1,28 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.18 on 2019-04-26 09:24
+from __future__ import unicode_literals
+
+from django.db import migrations
+
+
+def create_backends(apps, schema_editor):
+    PaymentBackend = apps.get_model('lingo', 'PaymentBackend')
+    Regie = apps.get_model('lingo', 'Regie')
+    for regie in Regie.objects.all():
+        pb = PaymentBackend(
+            label=regie.label, slug=regie.slug, description=regie.description,
+            service=regie.service, service_options=regie.service_options)
+        pb.save()
+        regie.payment_backend = pb
+        regie.save()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('lingo', '0034_auto_20190426_1159'),
+    ]
+
+    operations = [
+        migrations.RunPython(create_backends),
+    ]
diff --git a/combo/apps/lingo/migrations/0036_auto_20190426_1202.py b/combo/apps/lingo/migrations/0036_auto_20190426_1202.py
new file mode 100644
index 0000000..2fdea31
--- /dev/null
+++ b/combo/apps/lingo/migrations/0036_auto_20190426_1202.py
@@ -0,0 +1,29 @@
+# -*- coding: utf-8 -*-
+# Generated by Django 1.11.18 on 2019-04-26 10:02
+from __future__ import unicode_literals
+
+from django.db import migrations, models
+import django.db.models.deletion
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('lingo', '0035_auto_20190426_1124'),
+    ]
+
+    operations = [
+        migrations.RemoveField(
+            model_name='regie',
+            name='service',
+        ),
+        migrations.RemoveField(
+            model_name='regie',
+            name='service_options',
+        ),
+        migrations.AlterField(
+            model_name='regie',
+            name='payment_backend',
+            field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='lingo.PaymentBackend'),
+        ),
+    ]
diff --git a/combo/apps/lingo/models.py b/combo/apps/lingo/models.py
index 7ac1767..ac96de7 100644
--- a/combo/apps/lingo/models.py
+++ b/combo/apps/lingo/models.py
@@ -85,16 +85,27 @@ def build_remote_item(data, regie):
                       no_online_payment_reason=data.get('no_online_payment_reason'))
 
 
+@python_2_unicode_compatible
+class PaymentBackend(models.Model):
+    label = models.CharField(verbose_name=_('Label'), max_length=64)
+    slug = models.SlugField(
+        unique=True, verbose_name=_('Identifier'),
+        help_text=_('The identifier is used in webservice calls.'))
+    description = models.TextField(verbose_name=_('Description'))
+    service = models.CharField(
+        verbose_name=_('Payment Service'), max_length=64, choices=SERVICES)
+    service_options = JSONField(blank=True, verbose_name=_('Payment Service Options'))
+
+    def __str__(self):
+        return self.label
+
+
 @python_2_unicode_compatible
 class Regie(models.Model):
     label = models.CharField(verbose_name=_('Label'), max_length=64)
     slug = models.SlugField(unique=True, verbose_name=_('Identifier'),
             help_text=_('The identifier is used in webservice calls.'))
     description = models.TextField(verbose_name=_('Description'))
-    service = models.CharField(verbose_name=_('Payment Service'),
-            max_length=64, choices=SERVICES)
-    service_options = JSONField(blank=True,
-            verbose_name=_('Payment Service Options'))
     is_default = models.BooleanField(verbose_name=_('Default Regie'), default=False)
     webservice_url = models.URLField(_('Webservice URL to retrieve remote items'),
                         blank=True)
@@ -106,6 +117,7 @@ class Regie(models.Model):
     text_on_success = models.TextField(
             verbose_name=_('Custom text displayed on success'),
             blank=True, null=True)
+    payment_backend = models.ForeignKey(PaymentBackend, on_delete=models.CASCADE)
 
     def is_remote(self):
         return self.webservice_url != ''
-- 
2.20.1

