Skip to content

Commit a649619

Browse files
committed
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
1 parent 7619186 commit a649619

3 files changed

Lines changed: 57 additions & 1 deletion

File tree

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
6+
7+
class Migration(migrations.Migration):
8+
9+
dependencies = [
10+
('person', '0001_initial'),
11+
]
12+
13+
operations = [
14+
migrations.AddField(
15+
model_name='email',
16+
name='primary',
17+
field=models.BooleanField(default=False),
18+
preserve_default=True,
19+
),
20+
]
Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,33 @@
1+
# -*- coding: utf-8 -*-
2+
from __future__ import unicode_literals
3+
4+
from django.db import models, migrations
5+
6+
7+
def set_primary_email(apps, schema_editor):
8+
Person = apps.get_model("person", "Person")
9+
for person in Person.objects.all():
10+
email = person.email_set.order_by("-active","-time").first()
11+
if email:
12+
email.primary = True
13+
email.save()
14+
15+
def clear_primary_email(apps, schema_editor):
16+
Person = apps.get_model("person", "Person")
17+
for person in Person.objects.all():
18+
email_list = person.email_set.filter(primary=True)
19+
for email in email_list:
20+
email.primary = False
21+
email.save()
22+
23+
class Migration(migrations.Migration):
24+
25+
dependencies = [
26+
('person', '0002_email_primary'),
27+
]
28+
29+
operations = [
30+
migrations.RunPython(
31+
set_primary_email,
32+
clear_primary_email),
33+
]

ietf/person/models.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -60,7 +60,9 @@ def role_email(self, role_name, group=None):
6060
return e[0]
6161
return None
6262
def email_address(self):
63-
e = self.email_set.filter(active=True).order_by("-time").first()
63+
e = self.email_set.filter(primary=True).first()
64+
if not e:
65+
e = self.email_set.filter(active=True).order_by("-time").first()
6466
if e:
6567
return e.address
6668
else:
@@ -150,6 +152,7 @@ class Email(models.Model):
150152
address = models.CharField(max_length=64, primary_key=True)
151153
person = models.ForeignKey(Person, null=True)
152154
time = models.DateTimeField(auto_now_add=True)
155+
primary = models.BooleanField(default=False)
153156
active = models.BooleanField(default=True) # Old email addresses are *not* purged, as history
154157
# information points to persons through these
155158
def __unicode__(self):

0 commit comments

Comments
 (0)