Rietveld Code Review Tool
Help | Bug tracker | Discussion group | Source code

Unified Diff: sitescripts/utils.py

Issue 5177883412660224: Issue 2234 - Add a WSGI controller to collect email addresses for the Adblock Browser iOS launch (Closed)
Patch Set: Renamed files Created April 23, 2015, 2:48 p.m.
Use n/p to move between diff chunks; N/P to move between comments.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: sitescripts/utils.py
===================================================================
--- a/sitescripts/utils.py
+++ b/sitescripts/utils.py
@@ -113,6 +113,25 @@
else:
subprocess.Popen([config.get('DEFAULT', 'mailer'), '-t'], stdin=subprocess.PIPE).communicate(mail.encode('utf-8'))
+def encode_email_address(email):
+ '''
+ Validates and encodes an email address.
+
+ The validation implemented here is very rudamentery and not meant
+ to be complete, as full email validation can get extremly complicated
+ and is rarely needed. This function is primarily making sure that the
+ email address contains no whitespaces and only valid ASCII characters.
+ '''
+ match = re.search(r'^([^@\s]+)@([^@\s]+)$', email)
+ if not match:
+ raise ValueError
+
+ try:
+ return email.encode('ascii')
+ except UnicodeEncodeError:
+ return '%s@%s' % (match.group(1).encode('ascii'),
+ match.group(2).encode('idna'))
+
_template_cache = {}
def get_template(template, autoescape=True):

Powered by Google App Engine
This is Rietveld