Moving away from getpaid for now.
[wolnelektury.git] / src / funding / migrations / 0009_move_from_getpaid.py
1 # Generated by Django 2.2.28 on 2022-10-03 10:55
2 from django.conf import settings
3 from django.db import migrations
4
5
6 def move_from_getpaid(apps, schema_editor):
7     try:
8         G = settings.GETPAID_BACKENDS_SETTINGS
9     except AttributeError:
10         G = {}
11         getpaid_conf = False
12     else:
13         getpaid_conf = True
14
15     Funding = apps.get_model('funding', 'Funding')
16     for f in Funding.objects.filter(status=''):
17         payment = f.payment.first()
18         # TODO: what happens when no payments any more?
19         if payment is None:
20             continue
21         f.created_at = payment.created_on
22         f.order_id = payment.external_id
23         f.pos_id = G.get(payment.backend, {}).get('pos_id', '')
24         assert getpaid_conf, 'Getpaid configuration removed prematurely.'
25         f.status = {
26             'paid': 'COMPLETED',
27             'failed': 'REJECTED',
28             'in_progress': 'CANCELLED',
29         }[payment.status]
30         f.save()
31
32
33 class Migration(migrations.Migration):
34
35     dependencies = [
36         ('funding', '0008_auto_20221003_1235'),
37     ]
38
39     operations = [
40         migrations.RunPython(
41             move_from_getpaid,
42             migrations.RunPython.noop
43         )
44     ]