summaryrefslogtreecommitdiff
path: root/procurement/migrations/0002_add_representative.py
blob: 86a2f38cde20dcbb54c5a130cbcdfee888b2da0b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
# Generated by Django 2.1.2 on 2018-10-18 02:18

from django.db import migrations, models
import django.db.models.deletion
import warnings

def copy_reps_forward(apps, editor):
    Supplier = apps.get_model("procurement", "Supplier")
    Representative = apps.get_model("procurement", "Representative")
    suppliers = Supplier.objects.all()
    for sup in suppliers:
        name = sup.representative_name
        email = sup.representative_email
        rep = Representative(name=name, email=email, supplier=sup)
        rep.save()

class ReverseMigrationDataLossWarning(UserWarning):
    def __init__(self, supplier, kept_rep, lost_reps):
        self.supplier = supplier
        self.kept_rep = kept_rep
        self.lost_reps = lost_reps

    @staticmethod
    def format_rep(rep):
        return '{} <{}>'.format(rep.name, rep.email)

    def __str__(self):
        return "Supplier {} has multiple representatives, only keeping {}. {} will be lost".format(self.supplier.name, self.format_rep(self.kept_rep), [self.format_rep(rep) for rep in self.lost_reps])

def copy_reps_rev(apps, editor):
    warnings.filterwarnings("always", category=ReverseMigrationDataLossWarning)
    Supplier = apps.get_model("procurement", "Supplier")
    Representative = apps.get_model("procurement", "Representative")
    suppliers = Supplier.objects.all()
    for sup in suppliers:
        reps = sup.representatives.all()
        if reps:
            keep = reps[0]
            if len(reps) > 1:
                lost = reps[1:]
                warnings.warn(ReverseMigrationDataLossWarning(sup, keep, lost))
            sup.representative_name = keep.name
            sup.representative_email = keep.email
            sup.save()

class Migration(migrations.Migration):

    dependencies = [
        ('procurement', '0001_initial'),
    ]

    operations = [
        migrations.CreateModel(
            name='Representative',
            fields=[
                ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
                ('created', models.DateTimeField(auto_now_add=True)),
                ('updated', models.DateTimeField(auto_now=True)),
                ('name', models.CharField(max_length=255)),
                ('email', models.CharField(max_length=255)),
            ],
            options={
                'abstract': False,
            },
        ),
        migrations.AddField(
            model_name='representative',
            name='supplier',
            field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.CASCADE, related_name='representatives', to='procurement.Supplier'),
        ),
        migrations.RunPython(copy_reps_forward, copy_reps_rev),
        migrations.RemoveField(
            model_name='supplier',
            name='representative_email',
        ),
        migrations.RemoveField(
            model_name='supplier',
            name='representative_name',
        ),
    ]