From 01dbd7156c1aae0650f2dd48e5ac22dde09e5c0d Mon Sep 17 00:00:00 2001 From: Benjamin Lellouch Date: Tue, 23 Jun 2020 20:06:00 +0100 Subject: [PATCH 01/10] reformat --- app.py | 34 ++++++++++++++ templates/results.html | 103 +++++++++++++++++++++++++++++++++++++++-- 2 files changed, 133 insertions(+), 4 deletions(-) diff --git a/app.py b/app.py index 9b59c8c..f902410 100644 --- a/app.py +++ b/app.py @@ -24,11 +24,13 @@ from secrets import token_bytes from address import get_addresses from slugify import slugify +from requests.exceptions import HTTPError import json import logging import os import sys +import requests app = Flask(__name__) app.logger.addHandler(logging.StreamHandler(sys.stdout)) @@ -43,6 +45,20 @@ app.secret_key = skey +requests.packages.urllib3.util.ssl_.DEFAULT_CIPHERS += "HIGH:!DH:!aNULL" +try: + requests.packages.urllib3.contrib.pyopenssl.DEFAULT_SSL_CIPHER_LIST += ( + "HIGH:!DH:!aNULL" + ) +except AttributeError: + # no pyopenssl support used / needed / available + pass + +url = "https://us10.api.mailchimp.com/3.0/lists/{}/members/".format( + os.environ["MCLIST_ID"] +) + + @app.before_request def force_https(): criteria = [ @@ -129,3 +145,21 @@ def display_template(template_slug): matching_templates, name, postcode, address ) return render_template("single_email.html", email=email_template[0]) + + +@app.route("/newsletter", methods=["POST"]) +def subscribe_to_newsletter(): + + print(request.form["email"]) + post_params = {"email_address": request.form["email"], "status": "subscribed"} + r = requests.post( + url, auth=("foo", os.environ["MAILCHIMP_SECRET_KEY"]), json=post_params + ) + try: + r.raise_for_status() + except HTTPError: + return jsonify(status="failed") + + results = r.json() + print(results) + return jsonify(status="success") diff --git a/templates/results.html b/templates/results.html index 6333516..da5a9fa 100644 --- a/templates/results.html +++ b/templates/results.html @@ -15,17 +15,112 @@
{{ e.name }} to {{ e.target["name"] }}
{{ e.body }} - Open in default email app + Open in default email app - Open in Gmail - Open in Outlook - Open in Yahoo Mail + Open in Gmail + Open in Outlook + Open in Yahoo Mail
{% endfor %} + + + + + + +