Added a new field 'primary' to the Email model, added a matching migration, and a data migration to set primary fields to match the way a primary address is chosen today.
- Legacy-Id: 9149
This commit is contained in:
parent
7619186e6a
commit
a64961977b
20
ietf/person/migrations/0002_email_primary.py
Normal file
20
ietf/person/migrations/0002_email_primary.py
Normal file
|
@ -0,0 +1,20 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('person', '0001_initial'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name='email',
|
||||
name='primary',
|
||||
field=models.BooleanField(default=False),
|
||||
preserve_default=True,
|
||||
),
|
||||
]
|
33
ietf/person/migrations/0003_auto_20150304_0829.py
Normal file
33
ietf/person/migrations/0003_auto_20150304_0829.py
Normal file
|
@ -0,0 +1,33 @@
|
|||
# -*- coding: utf-8 -*-
|
||||
from __future__ import unicode_literals
|
||||
|
||||
from django.db import models, migrations
|
||||
|
||||
|
||||
def set_primary_email(apps, schema_editor):
|
||||
Person = apps.get_model("person", "Person")
|
||||
for person in Person.objects.all():
|
||||
email = person.email_set.order_by("-active","-time").first()
|
||||
if email:
|
||||
email.primary = True
|
||||
email.save()
|
||||
|
||||
def clear_primary_email(apps, schema_editor):
|
||||
Person = apps.get_model("person", "Person")
|
||||
for person in Person.objects.all():
|
||||
email_list = person.email_set.filter(primary=True)
|
||||
for email in email_list:
|
||||
email.primary = False
|
||||
email.save()
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('person', '0002_email_primary'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.RunPython(
|
||||
set_primary_email,
|
||||
clear_primary_email),
|
||||
]
|
|
@ -60,7 +60,9 @@ class PersonInfo(models.Model):
|
|||
return e[0]
|
||||
return None
|
||||
def email_address(self):
|
||||
e = self.email_set.filter(active=True).order_by("-time").first()
|
||||
e = self.email_set.filter(primary=True).first()
|
||||
if not e:
|
||||
e = self.email_set.filter(active=True).order_by("-time").first()
|
||||
if e:
|
||||
return e.address
|
||||
else:
|
||||
|
@ -150,6 +152,7 @@ class Email(models.Model):
|
|||
address = models.CharField(max_length=64, primary_key=True)
|
||||
person = models.ForeignKey(Person, null=True)
|
||||
time = models.DateTimeField(auto_now_add=True)
|
||||
primary = models.BooleanField(default=False)
|
||||
active = models.BooleanField(default=True) # Old email addresses are *not* purged, as history
|
||||
# information points to persons through these
|
||||
def __unicode__(self):
|
||||
|
|
Loading…
Reference in a new issue