From 572e373f95dd628d2391e7336d386337f3bd6fe3 Mon Sep 17 00:00:00 2001 From: Robert Sparks Date: Tue, 28 Feb 2017 19:39:01 +0000 Subject: [PATCH] Move primary but inactive email to the most recently touched active email for a Person. If a person has more than one primary email, make all but the first be not primary. Fixes #2214. Commit ready for merge. - Legacy-Id: 12932 --- ietf/person/migrations/0015_clean_primary.py | 41 ++++++++++++++++++++ 1 file changed, 41 insertions(+) create mode 100644 ietf/person/migrations/0015_clean_primary.py diff --git a/ietf/person/migrations/0015_clean_primary.py b/ietf/person/migrations/0015_clean_primary.py new file mode 100644 index 000000000..ee340dca6 --- /dev/null +++ b/ietf/person/migrations/0015_clean_primary.py @@ -0,0 +1,41 @@ +# -*- coding: utf-8 -*- +# Generated by Django 1.10.5 on 2017-02-28 10:46 +from __future__ import unicode_literals + +from django.db import migrations +from collections import Counter + +import debug # pyflakes:ignore + +def repair_primary(apps, schema_editor): + Person = apps.get_model('person','Person') + Email = apps.get_model('person','Email') + for p in Person.objects.all(): + primary = p.email_set.filter(primary=True).first() + if primary and not primary.active: + #debug.show("['removing primary',primary.address,p.name]") + primary.primary = False + primary.save() + new_primary = p.email_set.filter(active=True).order_by("-time").first() + if new_primary: + #debug.show("['adding primary',new_primary.address,p.name]") + new_primary.primary = True + new_primary.save() + for p_id in [x for x in Counter(Email.objects.filter(primary=True).values_list('person',flat=True)).items() if x[1]>1]: + for e in Email.objects.filter(person_id=p_id,primary=True)[1:]: + #debug.show("['removing extra primary', e.address, e.person.name]") + e.primary = False + e.save() + +def do_nothing(apps, schema_editor): + pass + +class Migration(migrations.Migration): + + dependencies = [ + ('person', '0014_auto_20160613_0751'), + ] + + operations = [ + migrations.RunPython(repair_primary,do_nothing) + ]