From eada3318b5244c9556314f1f68012260e7b95d22 Mon Sep 17 00:00:00 2001 From: Étienne Loks Date: Mon, 9 Apr 2018 19:06:10 +0200 Subject: Profile: default->current --- .../migrations/0040_auto_20180409_1821.py | 55 ---------------------- .../migrations/0040_auto_20180409_1900.py | 55 ++++++++++++++++++++++ .../migrations/0041_auto_20180409_1759.py | 51 -------------------- .../migrations/0041_auto_20180409_1901.py | 51 ++++++++++++++++++++ ishtar_common/models.py | 13 ++--- 5 files changed, 111 insertions(+), 114 deletions(-) delete mode 100644 ishtar_common/migrations/0040_auto_20180409_1821.py create mode 100644 ishtar_common/migrations/0040_auto_20180409_1900.py delete mode 100644 ishtar_common/migrations/0041_auto_20180409_1759.py create mode 100644 ishtar_common/migrations/0041_auto_20180409_1901.py (limited to 'ishtar_common') diff --git a/ishtar_common/migrations/0040_auto_20180409_1821.py b/ishtar_common/migrations/0040_auto_20180409_1821.py deleted file mode 100644 index bdf27cfc2..000000000 --- a/ishtar_common/migrations/0040_auto_20180409_1821.py +++ /dev/null @@ -1,55 +0,0 @@ -# -*- coding: utf-8 -*- -# Generated by Django 1.11.10 on 2018-04-09 18:21 -from __future__ import unicode_literals - -import django.core.validators -from django.db import migrations, models -import django.db.models.deletion -import ishtar_common.models -import re - - -class Migration(migrations.Migration): - - dependencies = [ - ('auth', '0009_merge_20170821_1827'), - ('ishtar_common', '0039_auto_20180405_1923'), - ] - - operations = [ - migrations.CreateModel( - name='ProfileType', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('label', models.TextField(verbose_name='Label')), - ('txt_idx', models.TextField(help_text='The slug is the standardized version of the name. It contains only lowercase letters, numbers and hyphens. Each slug must be unique.', unique=True, validators=[django.core.validators.RegexValidator(re.compile('^[-a-zA-Z0-9_]+\\Z'), "Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens.", 'invalid')], verbose_name='Textual ID')), - ('comment', models.TextField(blank=True, null=True, verbose_name='Comment')), - ('available', models.BooleanField(default=True, verbose_name='Available')), - ('groups', models.ManyToManyField(blank=True, to='auth.Group', verbose_name='Groups')), - ], - options={ - 'ordering': ('label',), - 'verbose_name': 'Profile type', - 'verbose_name_plural': 'Profile types', - }, - bases=(ishtar_common.models.Cached, models.Model), - ), - migrations.CreateModel( - name='UserProfile', - fields=[ - ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), - ('default', models.BooleanField(default=False, verbose_name='Default profile')), - ('person', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='profiles', to='ishtar_common.Person', verbose_name='Person')), - ('profile_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ishtar_common.ProfileType', verbose_name='Profile type')), - ], - options={ - 'verbose_name': 'User profile', - 'verbose_name_plural': 'User profiles', - }, - ), - migrations.AlterField( - model_name='import', - name='state', - field=models.CharField(choices=[(b'C', 'Created'), (b'AP', 'Analyse in progress'), (b'A', 'Analysed'), (b'HQ', 'Check modified in queue'), (b'IQ', 'Import in queue'), (b'HP', 'Check modified in progress'), (b'IP', 'Import in progress'), (b'PI', 'Partially imported'), (b'FE', 'Finished with errors'), (b'F', 'Finished'), (b'AC', 'Archived')], default='C', max_length=2, verbose_name='State'), - ), - ] diff --git a/ishtar_common/migrations/0040_auto_20180409_1900.py b/ishtar_common/migrations/0040_auto_20180409_1900.py new file mode 100644 index 000000000..a8cf51e33 --- /dev/null +++ b/ishtar_common/migrations/0040_auto_20180409_1900.py @@ -0,0 +1,55 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.11.10 on 2018-04-09 19:00 +from __future__ import unicode_literals + +import django.core.validators +from django.db import migrations, models +import django.db.models.deletion +import ishtar_common.models +import re + + +class Migration(migrations.Migration): + + dependencies = [ + ('auth', '0009_merge_20170821_1827'), + ('ishtar_common', '0039_auto_20180405_1923'), + ] + + operations = [ + migrations.CreateModel( + name='ProfileType', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('label', models.TextField(verbose_name='Label')), + ('txt_idx', models.TextField(help_text='The slug is the standardized version of the name. It contains only lowercase letters, numbers and hyphens. Each slug must be unique.', unique=True, validators=[django.core.validators.RegexValidator(re.compile('^[-a-zA-Z0-9_]+\\Z'), "Enter a valid 'slug' consisting of letters, numbers, underscores or hyphens.", 'invalid')], verbose_name='Textual ID')), + ('comment', models.TextField(blank=True, null=True, verbose_name='Comment')), + ('available', models.BooleanField(default=True, verbose_name='Available')), + ('groups', models.ManyToManyField(blank=True, to='auth.Group', verbose_name='Groups')), + ], + options={ + 'ordering': ('label',), + 'verbose_name': 'Profile type', + 'verbose_name_plural': 'Profile types', + }, + bases=(ishtar_common.models.Cached, models.Model), + ), + migrations.CreateModel( + name='UserProfile', + fields=[ + ('id', models.AutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')), + ('current', models.BooleanField(default=False, verbose_name='Current profile')), + ('person', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='profiles', to='ishtar_common.Person', verbose_name='Person')), + ('profile_type', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='ishtar_common.ProfileType', verbose_name='Profile type')), + ], + options={ + 'verbose_name': 'User profile', + 'verbose_name_plural': 'User profiles', + }, + ), + migrations.AlterField( + model_name='import', + name='state', + field=models.CharField(choices=[(b'C', 'Created'), (b'AP', 'Analyse in progress'), (b'A', 'Analysed'), (b'HQ', 'Check modified in queue'), (b'IQ', 'Import in queue'), (b'HP', 'Check modified in progress'), (b'IP', 'Import in progress'), (b'PI', 'Partially imported'), (b'FE', 'Finished with errors'), (b'F', 'Finished'), (b'AC', 'Archived')], default='C', max_length=2, verbose_name='State'), + ), + ] diff --git a/ishtar_common/migrations/0041_auto_20180409_1759.py b/ishtar_common/migrations/0041_auto_20180409_1759.py deleted file mode 100644 index 2ea56823a..000000000 --- a/ishtar_common/migrations/0041_auto_20180409_1759.py +++ /dev/null @@ -1,51 +0,0 @@ -# -*- 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), - ] diff --git a/ishtar_common/migrations/0041_auto_20180409_1901.py b/ishtar_common/migrations/0041_auto_20180409_1901.py new file mode 100644 index 000000000..cc34bb91c --- /dev/null +++ b/ishtar_common/migrations/0041_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', '0040_auto_20180409_1900'), + ] + + operations = [ + migrations.RunPython(initialize_profiles), + ] diff --git a/ishtar_common/models.py b/ishtar_common/models.py index 7c2069309..9aaf7bab1 100644 --- a/ishtar_common/models.py +++ b/ishtar_common/models.py @@ -2471,20 +2471,17 @@ class Person(Address, Merge, OwnPerms, ValueGetter): for attr in ['title', 'salutation'] if getattr(self, attr)]) @property - def default_profile(self): - q = self.profiles.filter(default=True) + def current_profile(self): + q = self.profiles.filter(current=True) if q.count(): return q.all()[0] q = self.profiles nb = q.count() if not nb: return - if nb > 1: - # arbitrary return the first one - return q.all()[0] - # if there is only one it is the default + # arbitrary set the first one as the current profile = q.all()[0] - profile.default = True + profile.current = True profile.save() return profile @@ -2665,7 +2662,7 @@ post_delete.connect(post_save_cache, sender=ProfileType) class UserProfile(models.Model): profile_type = models.ForeignKey( ProfileType, verbose_name=_(u"Profile type")) - default = models.BooleanField(_(u"Default profile"), default=False) + current = models.BooleanField(_(u"Current profile"), default=False) person = models.ForeignKey( Person, verbose_name=_(u"Person"), related_name='profiles') -- cgit v1.2.3