Author publishing + fix.
[wolnelektury.git] / src / club / migrations / 0041_move_amounts.py
1 # Generated by Django 2.2.27 on 2022-08-26 12:39
2
3 from django.db import migrations
4
5
6 def move_amounts(apps, schema_editor):
7     Club = apps.get_model('club', 'Club')
8     for club in Club.objects.all():
9         for amount in [int(x) for x in club.single_amounts.split(',')]:
10             club.singleamount_set.create(amount=amount)
11         for amount in [int(x) for x in club.monthly_amounts.split(',')]:
12             club.monthlyamount_set.create(amount=amount)
13
14
15 def move_amounts_back(apps, schema_editor):
16     Club = apps.get_model('club', 'Club')
17     for club in Club.objects.all():
18         club.single_amounts = ','.join(
19             str(x) for x in
20             club.singleamount_set.order_by('amount')
21         )
22         club.monthly_amounts = ','.join(
23             str(x) for x in
24             club.monthlyamount_set.order_by('amount')
25         )
26         club.save()
27
28
29 class Migration(migrations.Migration):
30
31     dependencies = [
32         ('club', '0040_amount'),
33     ]
34
35     operations = [
36         migrations.RunPython(
37             move_amounts,
38             move_amounts_back
39         )
40     ]