From f0ef002eb8dba93b41ebb90ee39904eba9adfb84 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com>
Date: Tue, 18 May 2021 09:54:17 +0200
Subject: [PATCH] czech inspirational: Newsletter subscribe refactoring

---
 czech_inspirational/models.py | 24 +++++-------------------
 majak/settings/base.py        |  8 ++++++++
 shared/utils.py               | 25 +++++++++++++++++++++++++
 3 files changed, 38 insertions(+), 19 deletions(-)

diff --git a/czech_inspirational/models.py b/czech_inspirational/models.py
index 68f90f0a..d23c5679 100644
--- a/czech_inspirational/models.py
+++ b/czech_inspirational/models.py
@@ -1,7 +1,3 @@
-import json
-import logging
-
-import requests
 from django.conf import settings
 from django.core.exceptions import ValidationError
 from django.core.validators import validate_email
@@ -20,11 +16,10 @@ from wagtail.images.edit_handlers import ImageChooserPanel
 from wagtailmetadata.models import MetadataPageMixin
 
 from shared.models import SubpageMixin
+from shared.utils import subscribe_to_newsletter
 from tuning import help
 from uniweb.constants import RICH_TEXT_FEATURES
 
-logger = logging.getLogger()
-
 
 class CzechInspirationalHomePage(Page, MetadataPageMixin):
     ### FIELDS
@@ -285,19 +280,10 @@ class CzechInspirationalDownloadPage(Page, SubpageMixin, MetadataPageMixin):
                 except ValidationError:
                     pass
                 else:
-                    payload = {
-                        "email": email,
-                        "news_id": settings.CZECH_INSPIRATIONAL_NEWSLETTER_ID,
-                    }
-                    response = requests.post(
-                        settings.NALODENI_API_NEWS_SUBSCRIBE_URL,
-                        data=json.dumps(payload),
-                        auth=tuple(settings.NALODENI_API_CREDENTIALS.split(":")),
+                    subscribe_to_newsletter(
+                        email,
+                        settings.CZECH_INSPIRATIONAL_NEWSLETTER_ID,
+                        settings.CZECH_INSPIRATIONAL_NEWSLETTER_SOURCE,
                     )
-                    if response.status_code != 200:
-                        logger.error(
-                            "Failed to subscribe!",
-                            extra={"email": email, "response": response.text},
-                        )
 
         return context
diff --git a/majak/settings/base.py b/majak/settings/base.py
index 080a5df5..13e42d36 100644
--- a/majak/settings/base.py
+++ b/majak/settings/base.py
@@ -254,7 +254,15 @@ DONATE_PORTAL_API_CACHE_TIMEOUT = 60 * 5
 CZECH_INSPIRATIONAL_NEWSLETTER_ID = env.int(
     "CZECH_INSPIRATIONAL_NEWSLETTER_ID", default=0
 )
+CZECH_INSPIRATIONAL_NEWSLETTER_SOURCE = env.str(
+    "CZECH_INSPIRATIONAL_NEWSLETTER_SOURCE", default="ÄŚesko inspirativnĂ­"
+)
+
 NALODENI_API_NEWS_SUBSCRIBE_URL = env.str("NALODENI_API_NEWS_SUBSCRIBE_URL", default="")
 NALODENI_API_CREDENTIALS = env.str("NALODENI_API_CREDENTIALS", default="")
 
 ELECTIONS2021_COOKIE_NAME = "program"
+ELECTIONS2021_NEWSLETTER_ID = env.int("ELECTIONS2021_NEWSLETTER_ID", default=0)
+ELECTIONS2021_NEWSLETTER_SOURCE = env.str(
+    "ELECTIONS2021_NEWSLETTER_SOURCE", "Koalice 2021"
+)
diff --git a/shared/utils.py b/shared/utils.py
index f4e3713c..7f854d99 100644
--- a/shared/utils.py
+++ b/shared/utils.py
@@ -1,8 +1,33 @@
+import json
+import logging
+
+import requests
+from django.conf import settings
 from wagtail.core.models import Page
 
+logger = logging.getLogger()
+
 
 def get_subpage_url(page, dest_page_type):
     try:
         return page.get_descendants().type(dest_page_type).live().first().get_url()
     except (Page.DoesNotExist, AttributeError):
         return "#"
+
+
+def subscribe_to_newsletter(email, news_id, source):
+    payload = {
+        "email": email,
+        "news_id": news_id,
+        "source": source,
+    }
+    response = requests.post(
+        settings.NALODENI_API_NEWS_SUBSCRIBE_URL,
+        data=json.dumps(payload),
+        auth=tuple(settings.NALODENI_API_CREDENTIALS.split(":")),
+    )
+    if response.status_code != 200:
+        logger.error(
+            "Failed to subscribe!",
+            extra={"payload": payload, "response": response.text},
+        )
-- 
GitLab