From 992b7d028db5b4c4ef45e4a33b3dc4ce480b8270 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Fri, 13 Apr 2018 13:31:05 +0200 Subject: Refactoring of migrations --- .../migrations/0042_auto_20180409_1901.py | 51 ++++++++++++++++++++++ 1 file changed, 51 insertions(+) create mode 100644 ishtar_common/migrations/0042_auto_20180409_1901.py (limited to 'ishtar_common/migrations/0042_auto_20180409_1901.py') diff --git a/ishtar_common/migrations/0042_auto_20180409_1901.py b/ishtar_common/migrations/0042_auto_20180409_1901.py new file mode 100644 index 000000000..a2faa24e4 --- /dev/null +++ b/ishtar_common/migrations/0042_auto_20180409_1901.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 current if only one person_type is concerned + current = 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, + current=current + ) + + +class Migration(migrations.Migration): + + dependencies = [ + ('ishtar_common', '0041_auto_20180409_1900'), + ] + + operations = [ + migrations.RunPython(initialize_profiles), + ] -- cgit v1.2.3