Skip to content

Commit d16efcc

Browse files
committed
Refactor of MailToken to keep the lists of To and CC tokens with one object
- Legacy-Id: 10036
1 parent 8a74604 commit d16efcc

10 files changed

Lines changed: 580 additions & 778 deletions

File tree

ietf/mailtoken/admin.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,6 @@ def has_code(self, obj):
1212

1313
class MailTokenAdmin(admin.ModelAdmin):
1414
list_display = [ 'slug', 'desc', ]
15-
filter_horizontal = [ 'recipients' ]
15+
filter_horizontal = [ 'to', 'cc', ]
1616
admin.site.register(MailToken, MailTokenAdmin)
1717

ietf/mailtoken/migrations/0001_initial.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -35,8 +35,14 @@ class Migration(migrations.Migration):
3535
),
3636
migrations.AddField(
3737
model_name='mailtoken',
38-
name='recipients',
39-
field=models.ManyToManyField(to='mailtoken.Recipient', null=True, blank=True),
38+
name='cc',
39+
field=models.ManyToManyField(related_name='used_in_cc', null=True, to='mailtoken.Recipient', blank=True),
40+
preserve_default=True,
41+
),
42+
migrations.AddField(
43+
model_name='mailtoken',
44+
name='to',
45+
field=models.ManyToManyField(related_name='used_in_to', null=True, to='mailtoken.Recipient', blank=True),
4046
preserve_default=True,
4147
),
4248
]

ietf/mailtoken/migrations/0002_auto_20150809_1314.py

Lines changed: 334 additions & 395 deletions
Large diffs are not rendered by default.

ietf/mailtoken/models.py

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,8 @@
88
class MailToken(models.Model):
99
slug = models.CharField(max_length=32, primary_key=True)
1010
desc = models.TextField(blank=True)
11-
recipients = models.ManyToManyField('Recipient', null=True, blank=True)
11+
to = models.ManyToManyField('Recipient', null=True, blank=True, related_name='used_in_to')
12+
cc = models.ManyToManyField('Recipient', null=True, blank=True, related_name='used_in_cc')
1213

1314
class Meta:
1415
ordering = ["slug"]

ietf/mailtoken/resources.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -20,14 +20,16 @@ class Meta:
2020
api.mailtoken.register(RecipientResource())
2121

2222
class MailTokenResource(ModelResource):
23-
recipients = ToManyField(RecipientResource, 'recipients', null=True)
23+
to = ToManyField(RecipientResource, 'to', null=True)
24+
cc = ToManyField(RecipientResource, 'cc', null=True)
2425
class Meta:
2526
queryset = MailToken.objects.all()
2627
#resource_name = 'mailtoken'
2728
filtering = {
2829
"slug": ALL,
2930
"desc": ALL,
30-
"recipients": ALL_WITH_RELATIONS,
31+
"to": ALL_WITH_RELATIONS,
32+
"cc": ALL_WITH_RELATIONS,
3133
}
3234
api.mailtoken.register(MailTokenResource())
3335

ietf/mailtoken/tests.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -13,12 +13,12 @@ def test_show_tokens(self):
1313
url = urlreverse('ietf.mailtoken.views.show_tokens')
1414
r = self.client.get(url)
1515
self.assertEqual(r.status_code, 200)
16-
self.assertTrue('ballot_saved_cc' in r.content)
16+
self.assertTrue('ballot_saved' in r.content)
1717

18-
url = urlreverse('ietf.mailtoken.views.show_tokens',kwargs=dict(mailtoken_slug='ballot_saved_cc'))
18+
url = urlreverse('ietf.mailtoken.views.show_tokens',kwargs=dict(mailtoken_slug='ballot_saved'))
1919
r = self.client.get(url)
2020
self.assertEqual(r.status_code, 200)
21-
self.assertTrue('ballot_saved_cc' in r.content)
21+
self.assertTrue('ballot_saved' in r.content)
2222

2323
def test_show_recipients(self):
2424

ietf/mailtoken/utils.py

Lines changed: 8 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,20 +1,17 @@
1-
from django.core.exceptions import ObjectDoesNotExist
2-
31
from ietf.mailtoken.models import MailToken, Recipient
42

53
def gather_address_list(slug,**kwargs):
64

75
addrs = []
86

9-
try:
10-
mailtoken = MailToken.objects.get(slug=slug)
11-
except ObjectDoesNotExist:
12-
# TODO remove the raise here, or find a better way to detect runtime misconfiguration
13-
raise
14-
return addrs
15-
16-
for recipient in mailtoken.recipients.all():
17-
addrs.extend(recipient.gather(**kwargs))
7+
if slug.endswith('_cc'):
8+
mailtoken = MailToken.objects.get(slug=slug[:-3])
9+
for recipient in mailtoken.cc.all():
10+
addrs.extend(recipient.gather(**kwargs))
11+
else:
12+
mailtoken = MailToken.objects.get(slug=slug)
13+
for recipient in mailtoken.to.all():
14+
addrs.extend(recipient.gather(**kwargs))
1815

1916
return list(set([addr for addr in addrs if addr]))
2017

ietf/mailtoken/views.py

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,19 +2,21 @@
22

33
from inspect import getsourcelines
44

5-
from django.shortcuts import render
5+
from django.shortcuts import render, get_object_or_404
66

77
from ietf.mailtoken.models import MailToken, Recipient
88

99
def show_tokens(request, mailtoken_slug=None):
1010
mailtokens = MailToken.objects.all()
1111
if mailtoken_slug:
12-
mailtokens = mailtokens.filter(slug=mailtoken_slug) # TODO better 404 behavior here and below
12+
get_object_or_404(MailToken,slug=mailtoken_slug)
13+
mailtokens = mailtokens.filter(slug=mailtoken_slug)
1314
return render(request,'mailtoken/token.html',{'mailtoken_slug':mailtoken_slug,
1415
'mailtokens':mailtokens})
1516
def show_recipients(request, recipient_slug=None):
1617
recipients = Recipient.objects.all()
1718
if recipient_slug:
19+
get_object_or_404(Recipient,slug=recipient_slug)
1820
recipients = recipients.filter(slug=recipient_slug)
1921
for recipient in recipients:
2022
fname = 'gather_%s'%recipient.slug

0 commit comments

Comments
 (0)