From a9e7c79b460f915ca788a7099859ef82aeefb1f3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexa=20Valentov=C3=A1?= <git@imaniti.org> Date: Wed, 19 Mar 2025 10:38:12 +0100 Subject: [PATCH] wip - ecomail test API --- ...ail_newsletter_list_categories_and_more.py | 28 +++++++++ ...ail_newsletter_list_categories_and_more.py | 22 +++++++ district/models.py | 60 +++++++------------ env.example | 3 + majak/settings/base.py | 3 + shared/models/main.py | 43 ++++++++++--- shared/utils.py | 30 ++++++++++ 7 files changed, 140 insertions(+), 49 deletions(-) create mode 100644 district/migrations/0314_districthomepage_ecomail_newsletter_list_categories_and_more.py create mode 100644 district/migrations/0315_remove_districthomepage_ecomail_newsletter_list_categories_and_more.py diff --git a/district/migrations/0314_districthomepage_ecomail_newsletter_list_categories_and_more.py b/district/migrations/0314_districthomepage_ecomail_newsletter_list_categories_and_more.py new file mode 100644 index 00000000..20970cf5 --- /dev/null +++ b/district/migrations/0314_districthomepage_ecomail_newsletter_list_categories_and_more.py @@ -0,0 +1,28 @@ +# Generated by Django 5.0.7 on 2025-03-19 07:48 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('district', '0313_alter_districtarticlepage_content_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='districthomepage', + name='ecomail_newsletter_list_categories', + field=models.CharField(blank=True, help_text="OddÄ›lte čárkou, napĹ™Ăklad 'Kategorie1,Kategorie2,Kategorie3'.", max_length=128, null=True, verbose_name='Kategorie k pĹ™idánĂ novĂ˝m odbÄ›ratelĹŻm na Ecomailu'), + ), + migrations.AddField( + model_name='districthomepage', + name='ecomail_newsletter_list_id', + field=models.IntegerField(blank=True, null=True, verbose_name='ID Ecomail newsletteru'), + ), + migrations.AlterField( + model_name='districthomepage', + name='newsletter_list_id', + field=models.CharField(blank=True, help_text='ID newsletteru z Mailtrainu. Po vyplnÄ›nĂ se formulář pro odbÄ›r newsletteru zobrazĂ na ĂşvodnĂ stránce a na stránce s kontakty.', max_length=20, null=True, verbose_name='ID Mailtrain newsletteru'), + ), + ] diff --git a/district/migrations/0315_remove_districthomepage_ecomail_newsletter_list_categories_and_more.py b/district/migrations/0315_remove_districthomepage_ecomail_newsletter_list_categories_and_more.py new file mode 100644 index 00000000..eec43904 --- /dev/null +++ b/district/migrations/0315_remove_districthomepage_ecomail_newsletter_list_categories_and_more.py @@ -0,0 +1,22 @@ +# Generated by Django 5.0.7 on 2025-03-19 08:37 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('district', '0314_districthomepage_ecomail_newsletter_list_categories_and_more'), + ] + + operations = [ + migrations.RemoveField( + model_name='districthomepage', + name='ecomail_newsletter_list_categories', + ), + migrations.AddField( + model_name='districthomepage', + name='ecomail_newsletter_list_tags', + field=models.CharField(blank=True, help_text="OddÄ›lte čárkou, napĹ™Ăklad 'Tag1,Tag2,Tag3'. Bez mezer.", max_length=128, null=True, verbose_name='Tagy k pĹ™idánĂ novĂ˝m odbÄ›ratelĹŻm na Ecomailu'), + ), + ] diff --git a/district/models.py b/district/models.py index b9908d0a..5804515e 100644 --- a/district/models.py +++ b/district/models.py @@ -72,6 +72,7 @@ from shared.utils import ( make_promote_panels, strip_all_html_tags, subscribe_to_newsletter, + subscribe_to_ecomail_newsletter, trim_to_length, ) @@ -130,12 +131,27 @@ class DistrictHomePage(CustomLogoMixin, CalendarMixin, MainHomePageMixin): # Settings newsletter_list_id = models.CharField( - "ID newsletteru", + "ID Mailtrain newsletteru", max_length=20, blank=True, null=True, help_text="ID newsletteru z Mailtrainu. Po vyplnÄ›nĂ se formulář pro odbÄ›r newsletteru zobrazĂ na ĂşvodnĂ stránce a na stránce s kontakty.", ) + + ecomail_newsletter_list_id = models.IntegerField( + "ID Ecomail newsletteru", + blank=True, + null=True, + ) + + ecomail_newsletter_list_tags = models.CharField( + "Tagy k pĹ™idánĂ novĂ˝m odbÄ›ratelĹŻm na Ecomailu", + max_length=128, + blank=True, + null=True, + help_text="OddÄ›lte čárkou, napĹ™Ăklad 'Tag1,Tag2,Tag3'. Bez mezer." + ) + newsletter_description = models.CharField( "Popis newsletteru", max_length=250, @@ -201,8 +217,10 @@ class DistrictHomePage(CustomLogoMixin, CalendarMixin, MainHomePageMixin): ), MultiFieldPanel( [ - FieldPanel("newsletter_list_id"), FieldPanel("newsletter_description"), + FieldPanel("newsletter_list_id"), + FieldPanel("ecomail_newsletter_list_id"), + FieldPanel("ecomail_newsletter_list_tags"), ], "Formulář pro odbÄ›r newsletteru", ), @@ -1136,44 +1154,6 @@ class DistrictCustomPage(RoutablePageMixin, MainSimplePageMixin): else newsletter_subscribe ) # preview fix - @route(r"^prihlaseni-k-newsletteru/$") - def newsletter_subscribe(self, request): - if request.method == "POST": - form = SubscribeForm(request.POST) - - if form.is_valid(): - subscribe_to_newsletter( - form.cleaned_data["email"], - ( - self.newsletter_list_id - if hasattr(self, "newsletter_list_id") - and self.newsletter_list_id - else settings.PIRATICZ_NEWSLETTER_CID - ), - ) - - messages.success( - request, - "Zkontroluj si prosĂm schránku, poslali jsme ti potvrzovacĂ email.", - ) - - try: - page = ( - Page.objects.filter(id=form.cleaned_data["return_page_id"]) - .live() - .first() - ) - return HttpResponseRedirect(page.full_url) - except Page.DoesNotExist: - return HttpResponseRedirect(self.url) - - messages.error( - request, - "TvĹŻj prohlĂĹľeÄŤ nám odeslal špatná data. ProsĂm, zkus to znovu.", - ) - - return HttpResponseRedirect(self.url) - @property def newsletter_list_id(self) -> str: for block in self.content: diff --git a/env.example b/env.example index baa1ffc1..a8abfea1 100644 --- a/env.example +++ b/env.example @@ -11,3 +11,6 @@ DJANGO_SECRET_KEY=123456 DJANGO_ALLOWED_HOSTS= CELERY_BROKER_URL=redis://127.0.0.1:6379/0 CELERY_RESULT_BACKEND= + +ECOMAIL_API_URL=https://api2.ecomailapp.cz +ECOMAIL_API_KEY=asdf \ No newline at end of file diff --git a/majak/settings/base.py b/majak/settings/base.py index 13f60a42..b875d229 100644 --- a/majak/settings/base.py +++ b/majak/settings/base.py @@ -309,6 +309,9 @@ DONATE_PORTAL_API_CACHE_TIMEOUT = 60 * 5 MAILTRAIN_API_URL = env.str("MAILTRAIN_API_URL", default="") MAILTRAIN_API_TOKEN = env.str("MAILTRAIN_API_TOKEN", default="") +ECOMAIL_API_URL = env.str("ECOMAIL_API_URL", default="") +ECOMAIL_API_KEY = env.str("ECOMAIL_API_KEY", default="") + CZECH_INSPIRATIONAL_NEWSLETTER_CID = env.str( "CZECH_INSPIRATIONAL_NEWSLETTER_CID", default="" ) diff --git a/shared/models/main.py b/shared/models/main.py index 070a4041..99269760 100644 --- a/shared/models/main.py +++ b/shared/models/main.py @@ -50,7 +50,7 @@ from shared.blocks import ( TwoTextColumnBlock, ) from shared.forms import SubscribeForm -from shared.utils import make_promote_panels, subscribe_to_newsletter +from shared.utils import make_promote_panels, subscribe_to_newsletter, subscribe_to_ecomail_newsletter from tuning import admin_help logger = logging.getLogger(__name__) @@ -1214,15 +1214,40 @@ class MainHomePageMixin( form = SubscribeForm(request.POST) if form.is_valid(): - subscribe_to_newsletter( - form.cleaned_data["email"], - ( + # If there is a mailtrain newsletter list ID, subscribe to it. + # + # Otherwise, if there is no ecomail newsletter list ID, subscribe + # to the global mailtrain list as long as it's defined. + # + # If there is an ecomail newsletter list ID, subscribe to it + # (even if the mailtrain one was also subscribed to. Seems logical here.) + if hasattr(self, "newsletter_list_id") and self.newsletter_list_id: + subscribe_to_newsletter( + form.cleaned_data["email"], self.newsletter_list_id - if hasattr(self, "newsletter_list_id") - and self.newsletter_list_id - else settings.PIRATICZ_NEWSLETTER_CID - ), - ) + ) + elif ( + ( + not hasattr(self, "ecomail_newsletter_list_id") + or not self.ecomail_newsletter_list_id + ) + and settings.PIRATICZ_NEWSLETTER_CID + ): + subscribe_to_newsletter( + form.cleaned_data["email"], + settings.PIRATICZ_NEWSLETTER_CID + ) + + if ( + hasattr(self, "ecomail_newsletter_list_id") + and self.ecomail_newsletter_list_id is not None + and self.ecomail_newsletter_list_id >= 1 + ): + subscribe_to_ecomail_newsletter( + form.cleaned_data["email"], + self.ecomail_newsletter_list_id, + self.ecomail_newsletter_list_tags + ) messages.success( request, diff --git a/shared/utils.py b/shared/utils.py index 23eafdf5..07bd0355 100644 --- a/shared/utils.py +++ b/shared/utils.py @@ -56,14 +56,44 @@ def make_promote_panels( def subscribe_to_newsletter(email, list_id): url = urljoin(settings.MAILTRAIN_API_URL, f"subscribe/{list_id}") + data = {"EMAIL": email, "FORCE_SUBSCRIBE": False, "REQUIRE_CONFIRMATION": True} response = requests.post( url, data=data, headers={"access-token": settings.MAILTRAIN_API_TOKEN} ) + + if response.status_code != 200: + logger.error( + "Failed to subscribe!", extra={"data": data, "response": response.text} + ) + + return response + + +def subscribe_to_ecomail_newsletter(email, list_id: int, categories: str): + split_categories = categories.split(",") + + data = { + "subscriber_data": { + "email": email, + "tags": split_categories, + } + } + + response = requests.post( + urljoin(settings.ECOMAIL_API_URL, f"lists/{list_id}/subscribe"), + data=json.dumps(data), + headers={ + "Content-Type": "application/json", + "key": settings.ECOMAIL_API_KEY + } + ) + if response.status_code != 200: logger.error( "Failed to subscribe!", extra={"data": data, "response": response.text} ) + return response -- GitLab