Skip to content

Commit f783847

Browse files
committed
Check if email addresses are valid. See ietf-tools#375
- Legacy-Id: 2508
1 parent 4f37a98 commit f783847

1 file changed

Lines changed: 30 additions & 3 deletions

File tree

ietf/liaisons/forms.py

Lines changed: 30 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,10 @@
11
import datetime
2+
from email.utils import parseaddr
23

34
from django import forms
45
from django.conf import settings
56
from django.forms.util import ErrorList
7+
from django.forms.fields import email_re
68
from django.template.loader import render_to_string
79

810
from ietf.liaisons.accounts import (can_add_outgoing_liaison, can_add_incoming_liaison,
@@ -121,6 +123,27 @@ def has_attachments(self):
121123
return True
122124
return False
123125

126+
def check_email(self, value):
127+
if not value:
128+
return
129+
emails = value.split(',')
130+
for email in emails:
131+
name, addr = parseaddr(email)
132+
if not email_re.search(addr):
133+
raise forms.ValidationError('Invalid email address: %s' % addr)
134+
135+
def clean_response_contact(self):
136+
value = self.cleaned_data.get('response_contact', None)
137+
self.check_email(value)
138+
139+
def clean_technical_contact(self):
140+
value = self.cleaned_data.get('technical_contact', None)
141+
self.check_email(value)
142+
143+
def clean_reply_to(self):
144+
value = self.cleaned_data.get('reply_to', None)
145+
self.check_email(value)
146+
124147
def clean(self):
125148
if not self.cleaned_data.get('body', None) and not self.has_attachments():
126149
self._errors['body'] = ErrorList([u'You must provide a body or attachment files'])
@@ -206,7 +229,7 @@ def get_post_only(self):
206229
return False
207230

208231
def clean(self):
209-
if self.data.has_key('send') and self.get_post_only():
232+
if 'send' in self.data.keys() and self.get_post_only():
210233
self._errors['from_field'] = ErrorList([u'As an IETF Liaison Manager you can not send an incoming liaison statements, you only can post them'])
211234
return super(IncomingLiaisonForm, self).clean()
212235

@@ -267,6 +290,10 @@ def save_extra_fields(self, liaison):
267290
liaison.approval = approval
268291
liaison.save()
269292

293+
def clean_to_poc(self):
294+
value = self.cleaned_data.get('to_poc', None)
295+
self.check_email(value)
296+
270297

271298
class EditLiaisonForm(LiaisonForm):
272299

@@ -278,8 +305,8 @@ class EditLiaisonForm(LiaisonForm):
278305

279306
class Meta:
280307
model = LiaisonDetail
281-
fields = ('from_raw_body', 'to_body', 'to_poc', 'cc1', 'last_modified_date', 'title',
282-
'response_contact', 'technical_contact', 'purpose_text', 'body',
308+
fields = ('from_raw_body', 'to_body', 'to_poc', 'cc1', 'last_modified_date', 'title',
309+
'response_contact', 'technical_contact', 'purpose_text', 'body',
283310
'deadline_date', 'purpose', 'replyto', )
284311

285312
def __init__(self, *args, **kwargs):

0 commit comments

Comments
 (0)