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

Unified Diff: sitescripts/submit_email/web/submit_email.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/submit_email/web/submit_email.py
===================================================================
new file mode 100644
--- /dev/null
+++ b/sitescripts/submit_email/web/submit_email.py
@@ -0,0 +1,75 @@
+# coding: utf-8
+
+# This file is part of the Adblock Plus web scripts,
+# Copyright (C) 2006-2015 Eyeo GmbH
+#
+# Adblock Plus is free software: you can redistribute it and/or modify
+# it under the terms of the GNU General Public License version 3 as
+# published by the Free Software Foundation.
+#
+# Adblock Plus is distributed in the hope that it will be useful,
+# but WITHOUT ANY WARRANTY; without even the implied warranty of
+# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
+# GNU General Public License for more details.
+#
+# You should have received a copy of the GNU General Public License
+# along with Adblock Plus. If not, see <http://www.gnu.org/licenses/>.
+
+import fcntl
+import wsgiref.util
+from urlparse import parse_qs, urljoin
+from urllib import urlencode
+
+from sitescripts.utils import get_config, sendMail, encode_email_address
+from sitescripts.web import url_handler, form_handler
+from sitescripts.signing import sign, verify
+
+VERIFICATION_PATH = '/verifyEmail'
+
+@url_handler('/submitEmail')
+@form_handler
+def submit_email(environ, start_response, data):
+ email = data.get('email', '').strip()
+ try:
+ email = encode_email_address(email)
+ except ValueError:
+ start_response('400 Bad Request', [('Content-Type', 'text/plain')])
+ return ['No valid email address given.']
+
+ sendMail(
+ get_config().get('submit_email', 'verification_email_template'),
+ {
+ 'recipient': email,
+ 'verification_url': '%s?%s' % (
+ urljoin(wsgiref.util.application_uri(environ), VERIFICATION_PATH),
+ urlencode([('email', email), ('signature', sign(email))])
+ )
+ }
+ )
+
+ start_response('200 OK', [('Content-Type', 'text/plain')])
+ return ["Thanks for your submission! You'll receive a verification email shortly."]
+
+@url_handler(VERIFICATION_PATH)
+def verify_email(environ, start_response):
+ config = get_config()
+
+ params = parse_qs(environ.get('QUERY_STRING', ''))
+ email = params.get('email', [''])[0]
+ signature = params.get('signature', [''])[0]
+
+ if verify(email, signature):
+ filename = config.get('submit_email', 'filename')
+ with open(filename, 'a', 0) as file:
+ fcntl.lockf(file, fcntl.LOCK_EX)
+ try:
+ print >>file, email
+ finally:
+ fcntl.lockf(file, fcntl.LOCK_UN)
+
+ option = 'successful_verification_redirect_location'
+ else:
+ option = 'failed_verification_redirect_location'
+
+ start_response('303 See Other', [('Location', config.get('submit_email', option))])
+ return []

Powered by Google App Engine
This is Rietveld