0034_registration_payment_obj.py 1.44 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# -*- coding: utf-8 -*-
# Generated by Django 1.11 on 2017-07-21 19:18
from __future__ import unicode_literals

from django.db import migrations


class Migration(migrations.Migration):
    dependencies = [
        ('events', '0033_registration_payment_obj'),
    ]

    def forwards_func(apps, schema_editor):
        Registration = apps.get_model('events', 'registration')
        Payment = apps.get_model('payments', 'payment')
        db_alias = schema_editor.connection.alias
        for reg in Registration.objects.using(db_alias).all():
            if reg.event.price > 0 and reg.payment_str != 'no_payment':
                reg.payment = Payment.objects.create(
                    created_at=reg.date,
                    type=reg.payment_str,
                    amount=reg.event.price,
                    paid_by=reg.member,
                    processing_date=reg.event.start,
25
26
27
                    notes=f'Event registration {reg.event.title_en}'
                          f'{reg.event.start}. '
                          f'Registration: {reg.date}'
28
29
30
31
32
33
34
35
36
37
38
39
40
                )
                reg.save()

    def reverse_func(apps, schema_editor):
        Registration = apps.get_model('events', 'registration')
        db_alias = schema_editor.connection.alias
        for reg in Registration.objects.using(db_alias).all():
            if reg.payment:
                reg.payment.delete()

    operations = [
        migrations.RunPython(forwards_func, reverse_func),
    ]