diff options
| author | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-04-09 18:28:47 +0200 | 
|---|---|---|
| committer | Étienne Loks <etienne.loks@iggdrasil.net> | 2018-06-12 08:39:42 +0200 | 
| commit | f57577c4d94f9e32395a0eff9a3eebee0b29bf6b (patch) | |
| tree | dd40d336a1858e4d6efaa418bc8e2888cf905498 /ishtar_common/migrations | |
| parent | af940f4d5c0ca165f6b09b8536aa2dfb2a66c662 (diff) | |
| download | Ishtar-f57577c4d94f9e32395a0eff9a3eebee0b29bf6b.tar.bz2 Ishtar-f57577c4d94f9e32395a0eff9a3eebee0b29bf6b.zip  | |
Migration script to convert old person types to new profiles (refs #4046)
Diffstat (limited to 'ishtar_common/migrations')
| -rw-r--r-- | ishtar_common/migrations/0041_auto_20180409_1759.py | 51 | 
1 files changed, 51 insertions, 0 deletions
diff --git a/ishtar_common/migrations/0041_auto_20180409_1759.py b/ishtar_common/migrations/0041_auto_20180409_1759.py new file mode 100644 index 000000000..2ea56823a --- /dev/null +++ b/ishtar_common/migrations/0041_auto_20180409_1759.py @@ -0,0 +1,51 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2018-04-09 17:29 +from __future__ import unicode_literals + +from django.db import migrations + + +def initialize_profiles(apps, schema_editor): +    # Migrate PersonType -> ProfileType +    PersonType = apps.get_model('ishtar_common', 'PersonType') +    ProfileType = apps.get_model('ishtar_common', 'ProfileType') +    UserProfile = apps.get_model('ishtar_common', 'UserProfile') +    Person = apps.get_model('ishtar_common', 'Person') + +    # create profile types from person types +    profile_types = {} +    for person_type in PersonType.objects.all(): +        default = { +            'label': person_type.label, +            'comment': person_type.comment, +            'available': person_type.available, +        } +        pt, created = ProfileType.objects.get_or_create( +            txt_idx=person_type.txt_idx, defaults=default) +        if created: +            for gp in person_type.groups.all(): +                pt.groups.add(gp) +        profile_types[pt.txt_idx] = pt + +    # match profile type with person types +    for person in Person.objects.filter(ishtaruser__isnull=False): +        # is default if only one person_type is concerned +        default = person.person_types.count() == 1 +        # only person with an account are concerned +        for person_type in person.person_types.all(): +            UserProfile.objects.create( +                profile_type=profile_types[person_type.txt_idx], +                person=person, +                default=default +            ) + + +class Migration(migrations.Migration): + +    dependencies = [ +        ('ishtar_common', '0040_auto_20180409_1821'), +    ] + +    operations = [ +        migrations.RunPython(initialize_profiles), +    ]  | 
