diff --git a/district/migrations/0114_alter_districtcustompage_content_and_more.py b/district/migrations/0115_alter_districtcustompage_content_and_more.py similarity index 95% rename from district/migrations/0114_alter_districtcustompage_content_and_more.py rename to district/migrations/0115_alter_districtcustompage_content_and_more.py index 6bf24af5d2d32c3ee690b58b389b02fd3abaab00..4b99c6750fe21fae1670048dffa9b132276ac104 100644 --- a/district/migrations/0114_alter_districtcustompage_content_and_more.py +++ b/district/migrations/0115_alter_districtcustompage_content_and_more.py @@ -1,23 +1,17 @@ -# Generated by Django 4.1.8 on 2023-05-15 10:36 +# Generated by Django 4.1.8 on 2023-05-21 15:26 -import django.db.models.deletion import wagtail.blocks -import wagtail.contrib.routable_page.models import wagtail.contrib.table_block.blocks import wagtail.fields import wagtail.images.blocks -import wagtailmetadata.models -from django.db import migrations, models +from django.db import migrations import shared.blocks -import shared.models class Migration(migrations.Migration): dependencies = [ - ("wagtailcore", "0083_workflowcontenttype"), - ("wagtailimages", "0025_alter_image_file_alter_rendition_file"), - ("district", "0113_merge_20230502_1854"), + ("district", "0114_merge_20230502_2140"), ] operations = [ @@ -6139,146 +6133,11 @@ class Migration(migrations.Migration): wagtail.blocks.StructBlock( [ ( - "submit_page", - wagtail.blocks.PageChooserBlock( - label="Zvolte si formulářovou stránku, která bude zpracovávat newsletter poĹľadavky", - page_type=["district.DistrictNewsletterForm"], - ), - ), - ( - "design", - wagtail.blocks.StructBlock( - [ - ( - "icon", - wagtail.images.blocks.ImageChooserBlock( - help_text="Pokud žádnou nevyberete, zobrazĂ se ikonka pro lahev v moĹ™i", - label="Vyberte si ikonku k formuláři", - required=False, - ), - ), - ( - "title", - wagtail.blocks.CharBlock( - default="OdebĂrej náš newsletter", - label="Nadpis formuláře", - ), - ), - ( - "subtitle", - wagtail.blocks.CharBlock( - default="Fake news tam nenajdeš, ale dozvĂš se, co chystáme doopravdy!", - label="Podnadpis formuláře", - ), - ), - ( - "consent", - wagtail.blocks.RichTextBlock( - default='SouhlasĂm se <a href="https://www.pirati.cz/ochrana-osobnich-udaju/">zpracovánĂm osobnĂch ĂşdajĹŻ</a>', - features=[ - "h2", - "h3", - "h4", - "h5", - "bold", - "italic", - "ol", - "ul", - "hr", - "link", - "document-link", - "image", - "superscript", - "subscript", - "strikethrough", - "blockquote", - "embed", - ], - label="Souhlas se zpracovánĂm osobnĂch ĂşdajĹŻ", - ), - ), - ( - "button", - wagtail.blocks.CharBlock( - default="OdebĂrat", - label="TlaÄŤĂtko pro pĹ™ihlášenĂ k newsletteru", - ), - ), - ( - "confirmation", - wagtail.blocks.CharBlock( - default="Zkontroluj si prosĂm schránku, poslali jsme ti potvrzovacĂ e-mail.", - label="Zpráva o ĂşspěšnĂ©m odeslánĂ formuláře", - ), - ), - ( - "labels", - wagtail.blocks.StructBlock( - [ - ( - "email", - wagtail.blocks.CharBlock( - default="TvĹŻj e-mail", - label="Popisek emailu", - ), - ), - ( - "first_name", - wagtail.blocks.CharBlock( - default="Tvoje kĹ™estnĂ jmĂ©no", - help_text="ZobrazĂ se, pouze pokud umoĹľnĂte zadat jmĂ©no v konfiguraci", - label="Popisek kĹ™estnĂho jmĂ©na", - ), - ), - ( - "last_name", - wagtail.blocks.CharBlock( - default="Tvoje pĹ™ĂjmenĂ", - help_text="ZobrazĂ se, pouze pokud umoĹľnĂte zadat jmĂ©no v konfiguraci", - label="Popisek pĹ™ĂjmenĂ", - ), - ), - ], - label="popisky vstupĹŻ uĹľivatele", - required=True, - ), - ), - ], - label="Design formuláře", - required=True, - ), - ), - ( - "config", - wagtail.blocks.StructBlock( - [ - ( - "show_name_input", - wagtail.blocks.BooleanBlock( - label="UmoĹľnit uĹľivateli pĹ™idat jmĂ©no a pĹ™ĂjmenĂ?", - required=False, - ), - ), - ( - "force_subscribe", - wagtail.blocks.BooleanBlock( - default=True, - label="PĹ™ihlásit uĹľivatele, i kdyĹľ se v minulosti odhlásil?", - required=False, - ), - ), - ( - "send_timezone", - wagtail.blocks.BooleanBlock( - label="ZasĂlat timezone podle uĹľivatelova prohlĂĹľeÄŤe?", - required=False, - ), - ), - ], - label="Konfigurace", - required=True, + "list_id", + wagtail.blocks.CharBlock( + label="ID newsletteru", required=True ), - ), + ) ] ), ), @@ -6396,146 +6255,11 @@ class Migration(migrations.Migration): wagtail.blocks.StructBlock( [ ( - "submit_page", - wagtail.blocks.PageChooserBlock( - label="Zvolte si formulářovou stránku, která bude zpracovávat newsletter poĹľadavky", - page_type=["district.DistrictNewsletterForm"], - ), - ), - ( - "design", - wagtail.blocks.StructBlock( - [ - ( - "icon", - wagtail.images.blocks.ImageChooserBlock( - help_text="Pokud žádnou nevyberete, zobrazĂ se ikonka pro lahev v moĹ™i", - label="Vyberte si ikonku k formuláři", - required=False, - ), - ), - ( - "title", - wagtail.blocks.CharBlock( - default="OdebĂrej náš newsletter", - label="Nadpis formuláře", - ), - ), - ( - "subtitle", - wagtail.blocks.CharBlock( - default="Fake news tam nenajdeš, ale dozvĂš se, co chystáme doopravdy!", - label="Podnadpis formuláře", - ), - ), - ( - "consent", - wagtail.blocks.RichTextBlock( - default='SouhlasĂm se <a href="https://www.pirati.cz/ochrana-osobnich-udaju/">zpracovánĂm osobnĂch ĂşdajĹŻ</a>', - features=[ - "h2", - "h3", - "h4", - "h5", - "bold", - "italic", - "ol", - "ul", - "hr", - "link", - "document-link", - "image", - "superscript", - "subscript", - "strikethrough", - "blockquote", - "embed", - ], - label="Souhlas se zpracovánĂm osobnĂch ĂşdajĹŻ", - ), - ), - ( - "button", - wagtail.blocks.CharBlock( - default="OdebĂrat", - label="TlaÄŤĂtko pro pĹ™ihlášenĂ k newsletteru", - ), - ), - ( - "confirmation", - wagtail.blocks.CharBlock( - default="Zkontroluj si prosĂm schránku, poslali jsme ti potvrzovacĂ e-mail.", - label="Zpráva o ĂşspěšnĂ©m odeslánĂ formuláře", - ), - ), - ( - "labels", - wagtail.blocks.StructBlock( - [ - ( - "email", - wagtail.blocks.CharBlock( - default="TvĹŻj e-mail", - label="Popisek emailu", - ), - ), - ( - "first_name", - wagtail.blocks.CharBlock( - default="Tvoje kĹ™estnĂ jmĂ©no", - help_text="ZobrazĂ se, pouze pokud umoĹľnĂte zadat jmĂ©no v konfiguraci", - label="Popisek kĹ™estnĂho jmĂ©na", - ), - ), - ( - "last_name", - wagtail.blocks.CharBlock( - default="Tvoje pĹ™ĂjmenĂ", - help_text="ZobrazĂ se, pouze pokud umoĹľnĂte zadat jmĂ©no v konfiguraci", - label="Popisek pĹ™ĂjmenĂ", - ), - ), - ], - label="popisky vstupĹŻ uĹľivatele", - required=True, - ), - ), - ], - label="Design formuláře", - required=True, - ), - ), - ( - "config", - wagtail.blocks.StructBlock( - [ - ( - "show_name_input", - wagtail.blocks.BooleanBlock( - label="UmoĹľnit uĹľivateli pĹ™idat jmĂ©no a pĹ™ĂjmenĂ?", - required=False, - ), - ), - ( - "force_subscribe", - wagtail.blocks.BooleanBlock( - default=True, - label="PĹ™ihlásit uĹľivatele, i kdyĹľ se v minulosti odhlásil?", - required=False, - ), - ), - ( - "send_timezone", - wagtail.blocks.BooleanBlock( - label="ZasĂlat timezone podle uĹľivatelova prohlĂĹľeÄŤe?", - required=False, - ), - ), - ], - label="Konfigurace", - required=True, + "list_id", + wagtail.blocks.CharBlock( + label="ID newsletteru", required=True ), - ), + ) ] ), ), @@ -6545,66 +6269,4 @@ class Migration(migrations.Migration): verbose_name="Obsah stránky", ), ), - migrations.CreateModel( - name="DistrictNewsletterForm", - fields=[ - ( - "page_ptr", - models.OneToOneField( - auto_created=True, - on_delete=django.db.models.deletion.CASCADE, - parent_link=True, - primary_key=True, - serialize=False, - to="wagtailcore.page", - ), - ), - ( - "subscription_id", - models.CharField( - blank=True, - max_length=512, - null=True, - verbose_name="ID newsletteru", - ), - ), - ( - "access_token", - models.CharField( - blank=True, - max_length=512, - null=True, - verbose_name="Access token", - ), - ), - ( - "require_confirmation", - models.BooleanField( - default=True, - verbose_name="Bude API vyĹľadovat potvrzovacĂ email?", - ), - ), - ( - "search_image", - models.ForeignKey( - blank=True, - null=True, - on_delete=django.db.models.deletion.SET_NULL, - related_name="+", - to="wagtailimages.image", - verbose_name="Search image", - ), - ), - ], - options={ - "verbose_name": "Stránka pro zpracovánĂ newsletter poĹľadavkĹŻ", - }, - bases=( - wagtailmetadata.models.WagtailImageMetadataMixin, - shared.models.SubpageMixin, - wagtail.contrib.routable_page.models.RoutablePageMixin, - "wagtailcore.page", - models.Model, - ), - ), ] diff --git a/district/models.py b/district/models.py index 857c6d59d160d1e439e0f2a01d89fa4f066c819f..48f3269fb18b94398ef66c44f243b544d6a71868 100644 --- a/district/models.py +++ b/district/models.py @@ -42,7 +42,7 @@ from shared.blocks import ( FigureBlock, FullSizeHeaderBlock, HeadlineBlock, - NewsletterSubscriptionBlockFactory, + NewsletterSubscriptionBlock, YouTubeVideoBlock, ) from shared.const import RICH_TEXT_DEFAULT_FEATURES @@ -52,7 +52,6 @@ from shared.models import ( ExtendedMetadataPageMixin, FooterMixin, MenuMixin, - NewsletterFormMixin, SubpageMixin, ) from shared.utils import make_promote_panels, strip_all_html_tags, trim_to_length @@ -101,7 +100,7 @@ class DistrictHomePage( ("headline", HeadlineBlock()), ( "newsletter", - NewsletterSubscriptionBlockFactory("district.DistrictNewsletterForm"), + NewsletterSubscriptionBlock(), ), ], verbose_name="Obsah stránky", @@ -307,7 +306,6 @@ class DistrictHomePage( "district.DistrictProgramPage", "district.DistrictInteractiveProgramPage", "district.DistrictGeoFeatureCollectionPage", - "district.DistrictNewsletterForm", ] ### OTHERS @@ -1422,10 +1420,7 @@ class DistrictCustomPage( + [ ("badge", blocks.PersonBadgeBlock()), ("people_group", blocks.PeopleGroupListBlock()), - ( - "newsletter", - NewsletterSubscriptionBlockFactory("district.DistrictNewsletterForm"), - ), + ("newsletter", NewsletterSubscriptionBlock()), ], verbose_name="Obsah", blank=True, @@ -1873,23 +1868,3 @@ class DistrictGeoFeatureDetailPage( ) except ValueError as exc: raise ValidationError({"geojson": str(exc)}) from exc - - -class DistrictNewsletterForm( - MetadataPageMixin, SubpageMixin, RoutablePageMixin, NewsletterFormMixin, Page -): - ### PANELS - - content_panels = NewsletterFormMixin.content_panels - - ### RELATIONS - - parent_page_types = ["district.DistrictHomePage"] - subpage_types = [] - - @route(r"^newsletter/$", name="newsletter") - def newsletter_post(self, posted): - return super().newsletter_post(posted) - - class Meta: - verbose_name = "Stránka pro zpracovánĂ newsletter poĹľadavkĹŻ" diff --git a/majak/urls.py b/majak/urls.py index 7f83a4dc25a25f21206ce45db1460b903fc4df07..751044d87d3c4828d0209878ad91fd6e2e1899d5 100644 --- a/majak/urls.py +++ b/majak/urls.py @@ -10,6 +10,7 @@ from wagtail.documents import urls as wagtaildocs_urls from elections2021 import views as elections2021_views from maps_utils import urls as maps_utils_urls +from shared.utils import subscribe_to_newsletter_ajax from tuning.views import SitesListView handler404 = "shared.views.page_not_found" @@ -26,6 +27,7 @@ urlpatterns = [ path("maps/", include(maps_utils_urls)), path("captcha/", include(captcha.urls)), path("seznam-webu/", SitesListView.as_view()), + path("newsletter/", subscribe_to_newsletter_ajax), path("sitemap.xml", sitemap), ] + pirates_urlpatterns diff --git a/shared/blocks.py b/shared/blocks.py index 9c5fe5f610fc86f913790dd61f05079197b107b6..116159f06cac003eba712c6a8aea19a1092bf2da 100644 --- a/shared/blocks.py +++ b/shared/blocks.py @@ -1074,128 +1074,13 @@ class FooterLinksBlock(blocks.StructBlock): template = "shared/blocks/footer_links_block.html" -def NewsletterSubscriptionBlockFactory(page_type: str): - class NewsletterSubscriptionBlock(blocks.StructBlock): - submit_page = blocks.PageChooserBlock( - page_type=page_type, - label="Zvolte si formulářovou stránku, která bude zpracovávat newsletter poĹľadavky", - ) - design = blocks.StructBlock( - local_blocks=[ - ( - "icon", - ImageChooserBlock( - label="Vyberte si ikonku k formuláři", - required=False, - help_text="Pokud žádnou nevyberete, zobrazĂ se ikonka pro lahev v moĹ™i", - ), - ), - ( - "title", - blocks.CharBlock( - label="Nadpis formuláře", default="OdebĂrej náš newsletter" - ), - ), - ( - "subtitle", - blocks.CharBlock( - label="Podnadpis formuláře", - default="Fake news tam nenajdeš, ale dozvĂš se, co chystáme doopravdy!", - ), - ), - ( - "consent", - blocks.RichTextBlock( - label="Souhlas se zpracovánĂm osobnĂch ĂşdajĹŻ", - features=RICH_TEXT_DEFAULT_FEATURES, - default='SouhlasĂm se <a href="https://www.pirati.cz/ochrana-osobnich-udaju/">zpracovánĂm osobnĂch ĂşdajĹŻ</a>', - ), - ), - ( - "button", - blocks.CharBlock( - label="TlaÄŤĂtko pro pĹ™ihlášenĂ k newsletteru", - default="OdebĂrat", - ), - ), - ( - "confirmation", - blocks.CharBlock( - label="Zpráva o ĂşspěšnĂ©m odeslánĂ formuláře", - default="Zkontroluj si prosĂm schránku, poslali jsme ti potvrzovacĂ e-mail.", - ), - ), - ( - "labels", - blocks.StructBlock( - local_blocks=[ - ( - "email", - blocks.CharBlock( - label="Popisek emailu", default="TvĹŻj e-mail" - ), - ), - ( - "first_name", - blocks.CharBlock( - label="Popisek kĹ™estnĂho jmĂ©na", - default="Tvoje kĹ™estnĂ jmĂ©no", - help_text="ZobrazĂ se, pouze pokud umoĹľnĂte zadat jmĂ©no v konfiguraci", - ), - ), - ( - "last_name", - blocks.CharBlock( - label="Popisek pĹ™ĂjmenĂ", - default="Tvoje pĹ™ĂjmenĂ", - help_text="ZobrazĂ se, pouze pokud umoĹľnĂte zadat jmĂ©no v konfiguraci", - ), - ), - ], - label="popisky vstupĹŻ uĹľivatele", - required=True, - ), - ), - ], - label="Design formuláře", - required=True, - ) +class NewsletterSubscriptionBlock(blocks.StructBlock): + list_id = blocks.CharBlock(label="ID newsletteru", required=True) - config = blocks.StructBlock( - local_blocks=[ - ( - "show_name_input", - blocks.BooleanBlock( - label="UmoĹľnit uĹľivateli pĹ™idat jmĂ©no a pĹ™ĂjmenĂ?", - required=False, - ), - ), - ( - "force_subscribe", - blocks.BooleanBlock( - label="PĹ™ihlásit uĹľivatele, i kdyĹľ se v minulosti odhlásil?", - required=False, - default=True, - ), - ), - ( - "send_timezone", - blocks.BooleanBlock( - label="ZasĂlat timezone podle uĹľivatelova prohlĂĹľeÄŤe?", - required=False, - ), - ), - ], - label="Konfigurace", - required=True, - ) - - class Meta: - label = "Formulář pro odebĂránĂ newsletteru" - icon = "form" - template = "shared/blocks/newsletter_subscription_block.html" - - return NewsletterSubscriptionBlock() + class Meta: + label = "Formulář pro odebĂránĂ newsletteru" + icon = "form" + template = "shared/blocks/newsletter_subscription_block.html" DEFAULT_CONTENT_BLOCKS = [ diff --git a/shared/models.py b/shared/models.py index e86373e91647b03420fa36e8b85f841b752d642b..c4e7c0cb95346a32241692cfaecbd1302ae5e4d6 100644 --- a/shared/models.py +++ b/shared/models.py @@ -19,75 +19,6 @@ from shared.blocks import ( logger = logging.getLogger(__name__) -class NewsletterFormMixin(Page): - """ - Use this for pages to which you want to submit newsletter requests. - You must define the route in the child subpage for this mixing to work. - """ - - subscription_id = models.CharField( - verbose_name="ID newsletteru", max_length=512, null=True, blank=True - ) - access_token = models.CharField( - verbose_name="Access token", max_length=512, null=True, blank=True - ) - require_confirmation = models.BooleanField( - verbose_name="Bude API vyĹľadovat potvrzovacĂ email?", default=True - ) - - content_panels = Page.content_panels + [ - MultiFieldPanel( - [ - FieldPanel("subscription_id"), - FieldPanel("access_token"), - FieldPanel("require_confirmation"), - ], - "NastavenĂ API newsletteru", - ), - ] - - def newsletter_post(self, posted): - client_response = HttpResponse() - if posted.method == "POST": - body = json.loads(posted.body) - mailtrain_request = {"EMAIL": body["email"]} - if "first_name" in body and "last_name" in body: - mailtrain_request.update( - { - "MERGE_FIRST_NAME": body["first_name"], - "MERGE_LAST_NAME": body["last_name"], - } - ) - if "force_subscribe" in posted.GET: - mailtrain_request.update( - { - "FORCE_SUBSCRIBE": "yes", - } - ) - if "timezone" in body: - mailtrain_request.update({"TIMEZONE": body["timezone"]}) - if self.require_confirmation: - mailtrain_request.update({"REQUIRE_CONFIRMATION": "yes"}) - response = request( - "POST", - f"https://mailtrain.pirati.cz/api/subscribe/{self.subscription_id}?access_token={self.access_token}", - json=mailtrain_request, - ) - print(response.json()) - if "error" in response.json(): - client_response.status_code = 500 - else: - client_response.status_code = response.status_code - - return client_response - - client_response.status_code = 400 - return client_response - - class Meta: - abstract = True - - class SubpageMixin: """Must be used in class definition before MetadataPageMixin!""" diff --git a/shared/static/shared/js/vue-formulate-helper.js b/shared/static/shared/js/vue-formulate-helper.js index 9c9dbbabd0e1eabd95de93b99aea9bf6f4a15896..bef4160bec19decc1a56582299e1f507116459ea 100644 --- a/shared/static/shared/js/vue-formulate-helper.js +++ b/shared/static/shared/js/vue-formulate-helper.js @@ -17,14 +17,9 @@ Vue.options.methods.mailtrainsubmit = async function (data) { headers.append('X-CSRFToken', csrftoken); try { - const response = await fetch(this.$el.dataset.url, { + const response = await fetch("/newsletter/", { method: "POST", - body: JSON.stringify(data.timezone ? { - ...data, - timezone: Intl.DateTimeFormat().resolvedOptions().timeZone, - } : { - ...data, - }), + body: JSON.stringify(data), headers: headers, credentials: "include", }); diff --git a/shared/templates/shared/blocks/newsletter_subscription_block.html b/shared/templates/shared/blocks/newsletter_subscription_block.html index 4d65d91d11e0df7335ed73a11f5264812b71ad38..993795841024045d223934b5b471719214b7fb5d 100644 --- a/shared/templates/shared/blocks/newsletter_subscription_block.html +++ b/shared/templates/shared/blocks/newsletter_subscription_block.html @@ -1,27 +1,23 @@ -{% load static shared_filters wagtailcore_tags wagtailroutablepage_tags %} +{% load static wagtailcore_tags %} <div class="__js-root"> <ui-app inline-template> - <div data-url="{% routablepageurl self.submit_page 'newsletter' %}" class="flex flex-row flex-wrap xl:items-center mt-20 xl:mt-0"> + <div class="flex flex-row flex-wrap xl:items-center mt-20 xl:mt-0"> <div class="mr-20"> - {% if self.design.icon %} - <img src="{{ self.design.icon.full_url }}" alt="newsletter_icon" class="newsletter-grid__icon text-7xl xl:text-9xl w-32"> - {% else %} - <img src="{% static "shared/img/newsletter.svg" %}" alt="newsletter_icon" class="newsletter-grid__icon text-7xl xl:text-9xl w-32"> - {% endif %} + <img src="{% static "shared/img/newsletter.svg" %}" alt="newsletter_icon" class="newsletter-grid__icon text-7xl xl:text-9xl w-32"> </div> <div> <h5 class="font-alt mb-2 text-5xl md:text-6xl xl:text-7xl uppercase"> - {{ self.design.title }} + OdebĂrej náš newsletter </h5> <span class="inline-block uppercase text-xl xl:text-base font-bold mb-4 xl:w-full"> - {{ self.design.subtitle }} + Fake news tam nenajdeš, ale dozvĂš se, co chystáme doopravdy! </span> <div data-success="true" style="display: none;"> <ul class="flex flex-col w-full drop-shadow-lg"> <li class="px-5 py-4 text-center relative bg-green-400 text-white"> <i class="ico--checkmark mr-2"></i> - {{ self.design.confirmation }} + Zkontroluj si prosĂm schránku, poslali jsme ti potvrzovacĂ e-mail. </li> </ul> </div> @@ -31,44 +27,22 @@ {% csrf_token %} <formulate-input type="hidden" - name="secret_config" - value="{{ self.config.secret_config_id }}" + name="list_id" + value="{{ self.list_id }}" ></formulate-input> - {% if self.config.send_timezone %} - <formulate-input - type="hidden" - name="send_timezone" - value="True" - ></formulate-input> - {% endif %} - {% if self.config.force_subscribe %} - <formulate-input - type="hidden" - name="force_subscribe" - value="True" - ></formulate-input> - {% endif %} - {% if self.config.show_name_input %} - <formulate-input type="text" name="first_name" validation-name="KĹ™estnĂ jmĂ©no" label="{{ self.design.labels.first_name }}" - placeholder="{{ self.design.labels.first_name }}" validation="required" error-behavior="blur" - label-class="sr-only"></formulate-input> - <formulate-input type="text" name="last_name" validation-name="PĹ™ĂjmenĂ" label="{{ self.design.labels.last_name }}" - placeholder="{{ self.design.labels.last_name }}" validation="required" error-behavior="blur" - label-class="sr-only"></formulate-input> - {% endif %} - <formulate-input type="email" name="email" label="{{ self.design.labels.email }}" - placeholder="{{ self.design.labels.email }}" validation-name="E-mail" validation="^required|email" error-behavior="blur" + <formulate-input type="email" name="email" label="TvĹŻj e-mail" + placeholder="TvĹŻj e-mail" validation-name="E-mail" validation="^required|email" error-behavior="blur" label-class="sr-only"></formulate-input> <formulate-input type="checkbox" name="consent" validation-name="Souhlas se zpracovánĂm osobnĂch ĂşdajĹŻ" :wrapper-class="() => []" :input-class="() => []" :element-class="() => ['checkbox', 'form-field__control', 'flex', 'items-center', 'mb-3']" validation="required" error-behavior="blur"> <template #suffix="{ id }"> <label :for="id" class="text-xs font-alt font-light"> - {{ self.design.consent|inline_richtext }} + SouhlasĂm se <a href="https://www.pirati.cz/ochrana-osobnich-udaju/">zpracovánĂm osobnĂch ĂşdajĹŻ</a> </label> </template> </formulate-input> <button type="submit" class="btn btn--black font-alt btn--to-yellow-500 btn--hoveractive uppercase"> <div class="btn__body-wrap"> - <div v-html="isLoading ? 'OdesĂlám...' : '{{ self.design.button }}'" class="btn__body"></div> + <div v-html="isLoading ? 'OdesĂlám...' : 'OdebĂrat'" class="btn__body"></div> </div> </button> </formulate-form> diff --git a/shared/templatetags/shared_filters.py b/shared/templatetags/shared_filters.py index 7fcb579557eb348927a7d633ca82fda854544d7c..2f06f9ad6a92564d9e3e51757ad0d935f2f7f84f 100644 --- a/shared/templatetags/shared_filters.py +++ b/shared/templatetags/shared_filters.py @@ -1,7 +1,6 @@ import markdown as md from django import template from django.utils.safestring import mark_safe -from wagtail.rich_text import RichText, expand_db_html register = template.Library() @@ -10,21 +9,3 @@ register = template.Library() def markdown(value): """Prekonvertuje vstupni text na markdown, necekane""" return mark_safe(md.markdown(value)) - - -@register.filter -def inline_richtext(value): - if isinstance(value, RichText): - html = expand_db_html(value.source) - elif isinstance(value, str): - html = expand_db_html(value) - elif value is None: - html = "" - else: - raise TypeError( - "'richtext_withclasses' template filter received an invalid value; expected string, got {}.".format( - type(value) - ) - ) - - return mark_safe(html) diff --git a/shared/utils.py b/shared/utils.py index 48a1cc3205bff9cbfd9092545448d2c446bab499..dc03063ee17284b9f196b7995fa1e2debe3c7f0b 100644 --- a/shared/utils.py +++ b/shared/utils.py @@ -7,6 +7,7 @@ import bleach import requests from django.conf import settings from django.core.files import File +from django.http import HttpResponse from django.utils.translation import gettext_lazy from wagtail.admin.panels import CommentPanel, FieldPanel, HelpPanel, MultiFieldPanel from wagtail.images.edit_handlers import FieldPanel @@ -64,6 +65,23 @@ def subscribe_to_newsletter(email, list_id): logger.error( "Failed to subscribe!", extra={"data": data, "response": response.text} ) + return response + + +def subscribe_to_newsletter_ajax(request): + client_response = HttpResponse() + if request.method == "POST": + body = json.loads(request.body) + response = subscribe_to_newsletter(body["EMAIL"], body["LIST_ID"]) + if "error" in response.json(): + client_response.status_code = 500 + else: + client_response.status_code = response.status_code + + return client_response + + client_response.status_code = 400 + return client_response def strip_all_html_tags(value: str): diff --git a/uniweb/migrations/0044_alter_uniwebflexiblepage_content_and_more.py b/uniweb/migrations/0044_alter_uniwebflexiblepage_content_and_more.py index 4ba25daa4cff7b7170833d30c6f9758df91184db..3f2ed902934d6b624ded5b722cb0e348168427bd 100644 --- a/uniweb/migrations/0044_alter_uniwebflexiblepage_content_and_more.py +++ b/uniweb/migrations/0044_alter_uniwebflexiblepage_content_and_more.py @@ -1,23 +1,17 @@ -# Generated by Django 4.1.8 on 2023-05-15 10:36 +# Generated by Django 4.1.8 on 2023-05-21 15:26 -import django.db.models.deletion import wagtail.blocks import wagtail.blocks.static_block -import wagtail.contrib.routable_page.models import wagtail.contrib.table_block.blocks import wagtail.fields import wagtail.images.blocks -import wagtailmetadata.models -from django.db import migrations, models +from django.db import migrations import shared.blocks -import shared.models class Migration(migrations.Migration): dependencies = [ - ("wagtailcore", "0083_workflowcontenttype"), - ("wagtailimages", "0025_alter_image_file_alter_rendition_file"), ("uniweb", "0043_merge_20230502_1854"), ] @@ -610,146 +604,11 @@ class Migration(migrations.Migration): wagtail.blocks.StructBlock( [ ( - "submit_page", - wagtail.blocks.PageChooserBlock( - label="Zvolte si formulářovou stránku, která bude zpracovávat newsletter poĹľadavky", - page_type=["uniweb.UniwebNewsletterForm"], - ), - ), - ( - "design", - wagtail.blocks.StructBlock( - [ - ( - "icon", - wagtail.images.blocks.ImageChooserBlock( - help_text="Pokud žádnou nevyberete, zobrazĂ se ikonka pro lahev v moĹ™i", - label="Vyberte si ikonku k formuláři", - required=False, - ), - ), - ( - "title", - wagtail.blocks.CharBlock( - default="OdebĂrej náš newsletter", - label="Nadpis formuláře", - ), - ), - ( - "subtitle", - wagtail.blocks.CharBlock( - default="Fake news tam nenajdeš, ale dozvĂš se, co chystáme doopravdy!", - label="Podnadpis formuláře", - ), - ), - ( - "consent", - wagtail.blocks.RichTextBlock( - default='SouhlasĂm se <a href="https://www.pirati.cz/ochrana-osobnich-udaju/">zpracovánĂm osobnĂch ĂşdajĹŻ</a>', - features=[ - "h2", - "h3", - "h4", - "h5", - "bold", - "italic", - "ol", - "ul", - "hr", - "link", - "document-link", - "image", - "superscript", - "subscript", - "strikethrough", - "blockquote", - "embed", - ], - label="Souhlas se zpracovánĂm osobnĂch ĂşdajĹŻ", - ), - ), - ( - "button", - wagtail.blocks.CharBlock( - default="OdebĂrat", - label="TlaÄŤĂtko pro pĹ™ihlášenĂ k newsletteru", - ), - ), - ( - "confirmation", - wagtail.blocks.CharBlock( - default="Zkontroluj si prosĂm schránku, poslali jsme ti potvrzovacĂ e-mail.", - label="Zpráva o ĂşspěšnĂ©m odeslánĂ formuláře", - ), - ), - ( - "labels", - wagtail.blocks.StructBlock( - [ - ( - "email", - wagtail.blocks.CharBlock( - default="TvĹŻj e-mail", - label="Popisek emailu", - ), - ), - ( - "first_name", - wagtail.blocks.CharBlock( - default="Tvoje kĹ™estnĂ jmĂ©no", - help_text="ZobrazĂ se, pouze pokud umoĹľnĂte zadat jmĂ©no v konfiguraci", - label="Popisek kĹ™estnĂho jmĂ©na", - ), - ), - ( - "last_name", - wagtail.blocks.CharBlock( - default="Tvoje pĹ™ĂjmenĂ", - help_text="ZobrazĂ se, pouze pokud umoĹľnĂte zadat jmĂ©no v konfiguraci", - label="Popisek pĹ™ĂjmenĂ", - ), - ), - ], - label="popisky vstupĹŻ uĹľivatele", - required=True, - ), - ), - ], - label="Design formuláře", - required=True, - ), - ), - ( - "config", - wagtail.blocks.StructBlock( - [ - ( - "show_name_input", - wagtail.blocks.BooleanBlock( - label="UmoĹľnit uĹľivateli pĹ™idat jmĂ©no a pĹ™ĂjmenĂ?", - required=False, - ), - ), - ( - "force_subscribe", - wagtail.blocks.BooleanBlock( - default=True, - label="PĹ™ihlásit uĹľivatele, i kdyĹľ se v minulosti odhlásil?", - required=False, - ), - ), - ( - "send_timezone", - wagtail.blocks.BooleanBlock( - label="ZasĂlat timezone podle uĹľivatelova prohlĂĹľeÄŤe?", - required=False, - ), - ), - ], - label="Konfigurace", - required=True, + "list_id", + wagtail.blocks.CharBlock( + label="ID newsletteru", required=True ), - ), + ) ] ), ), @@ -1347,146 +1206,11 @@ class Migration(migrations.Migration): wagtail.blocks.StructBlock( [ ( - "submit_page", - wagtail.blocks.PageChooserBlock( - label="Zvolte si formulářovou stránku, která bude zpracovávat newsletter poĹľadavky", - page_type=["uniweb.UniwebNewsletterForm"], - ), - ), - ( - "design", - wagtail.blocks.StructBlock( - [ - ( - "icon", - wagtail.images.blocks.ImageChooserBlock( - help_text="Pokud žádnou nevyberete, zobrazĂ se ikonka pro lahev v moĹ™i", - label="Vyberte si ikonku k formuláři", - required=False, - ), - ), - ( - "title", - wagtail.blocks.CharBlock( - default="OdebĂrej náš newsletter", - label="Nadpis formuláře", - ), - ), - ( - "subtitle", - wagtail.blocks.CharBlock( - default="Fake news tam nenajdeš, ale dozvĂš se, co chystáme doopravdy!", - label="Podnadpis formuláře", - ), - ), - ( - "consent", - wagtail.blocks.RichTextBlock( - default='SouhlasĂm se <a href="https://www.pirati.cz/ochrana-osobnich-udaju/">zpracovánĂm osobnĂch ĂşdajĹŻ</a>', - features=[ - "h2", - "h3", - "h4", - "h5", - "bold", - "italic", - "ol", - "ul", - "hr", - "link", - "document-link", - "image", - "superscript", - "subscript", - "strikethrough", - "blockquote", - "embed", - ], - label="Souhlas se zpracovánĂm osobnĂch ĂşdajĹŻ", - ), - ), - ( - "button", - wagtail.blocks.CharBlock( - default="OdebĂrat", - label="TlaÄŤĂtko pro pĹ™ihlášenĂ k newsletteru", - ), - ), - ( - "confirmation", - wagtail.blocks.CharBlock( - default="Zkontroluj si prosĂm schránku, poslali jsme ti potvrzovacĂ e-mail.", - label="Zpráva o ĂşspěšnĂ©m odeslánĂ formuláře", - ), - ), - ( - "labels", - wagtail.blocks.StructBlock( - [ - ( - "email", - wagtail.blocks.CharBlock( - default="TvĹŻj e-mail", - label="Popisek emailu", - ), - ), - ( - "first_name", - wagtail.blocks.CharBlock( - default="Tvoje kĹ™estnĂ jmĂ©no", - help_text="ZobrazĂ se, pouze pokud umoĹľnĂte zadat jmĂ©no v konfiguraci", - label="Popisek kĹ™estnĂho jmĂ©na", - ), - ), - ( - "last_name", - wagtail.blocks.CharBlock( - default="Tvoje pĹ™ĂjmenĂ", - help_text="ZobrazĂ se, pouze pokud umoĹľnĂte zadat jmĂ©no v konfiguraci", - label="Popisek pĹ™ĂjmenĂ", - ), - ), - ], - label="popisky vstupĹŻ uĹľivatele", - required=True, - ), - ), - ], - label="Design formuláře", - required=True, - ), - ), - ( - "config", - wagtail.blocks.StructBlock( - [ - ( - "show_name_input", - wagtail.blocks.BooleanBlock( - label="UmoĹľnit uĹľivateli pĹ™idat jmĂ©no a pĹ™ĂjmenĂ?", - required=False, - ), - ), - ( - "force_subscribe", - wagtail.blocks.BooleanBlock( - default=True, - label="PĹ™ihlásit uĹľivatele, i kdyĹľ se v minulosti odhlásil?", - required=False, - ), - ), - ( - "send_timezone", - wagtail.blocks.BooleanBlock( - label="ZasĂlat timezone podle uĹľivatelova prohlĂĹľeÄŤe?", - required=False, - ), - ), - ], - label="Konfigurace", - required=True, + "list_id", + wagtail.blocks.CharBlock( + label="ID newsletteru", required=True ), - ), + ) ] ), ), @@ -1496,66 +1220,4 @@ class Migration(migrations.Migration): verbose_name="obsah stránky", ), ), - migrations.CreateModel( - name="UniwebNewsletterForm", - fields=[ - ( - "page_ptr", - models.OneToOneField( - auto_created=True, - on_delete=django.db.models.deletion.CASCADE, - parent_link=True, - primary_key=True, - serialize=False, - to="wagtailcore.page", - ), - ), - ( - "subscription_id", - models.CharField( - blank=True, - max_length=512, - null=True, - verbose_name="ID newsletteru", - ), - ), - ( - "access_token", - models.CharField( - blank=True, - max_length=512, - null=True, - verbose_name="Access token", - ), - ), - ( - "require_confirmation", - models.BooleanField( - default=True, - verbose_name="Bude API vyĹľadovat potvrzovacĂ email?", - ), - ), - ( - "search_image", - models.ForeignKey( - blank=True, - null=True, - on_delete=django.db.models.deletion.SET_NULL, - related_name="+", - to="wagtailimages.image", - verbose_name="Search image", - ), - ), - ], - options={ - "verbose_name": "Stránka pro zpracovánĂ newsletter poĹľadavkĹŻ", - }, - bases=( - wagtailmetadata.models.WagtailImageMetadataMixin, - shared.models.SubpageMixin, - wagtail.contrib.routable_page.models.RoutablePageMixin, - "wagtailcore.page", - models.Model, - ), - ), ] diff --git a/uniweb/models.py b/uniweb/models.py index 4f31ae871213125f3f422e164cc3307935177e86..7c191ff29a7cbcebb6f352947cbe915ac0a0ba4b 100644 --- a/uniweb/models.py +++ b/uniweb/models.py @@ -18,7 +18,6 @@ from wagtail.admin.panels import ( ) from wagtail.contrib.forms.models import AbstractForm, AbstractFormField from wagtail.contrib.forms.panels import FormSubmissionsPanel -from wagtail.contrib.routable_page.models import RoutablePageMixin, route from wagtail.contrib.table_block.blocks import TableBlock from wagtail.fields import RichTextField, StreamField from wagtail.images.blocks import ImageChooserBlock @@ -26,14 +25,13 @@ from wagtail.models import Page from wagtailmetadata.models import MetadataPageMixin from calendar_utils.models import CalendarMixin -from shared.blocks import ChartBlock, NewsletterSubscriptionBlockFactory +from shared.blocks import ChartBlock, NewsletterSubscriptionBlock from shared.const import RICH_TEXT_DEFAULT_FEATURES from shared.models import ( ArticleMixin, ExtendedMetadataHomePageMixin, ExtendedMetadataPageMixin, FooterMixin, - NewsletterFormMixin, SubpageMixin, ) from shared.utils import make_promote_panels, strip_all_html_tags, trim_to_length @@ -312,13 +310,7 @@ class UniwebHomePage( ### FIELDS content = StreamField( - CONTENT_STREAM_BLOCKS - + [ - ( - "newsletter", - NewsletterSubscriptionBlockFactory("uniweb.UniwebNewsletterForm"), - ) - ], + CONTENT_STREAM_BLOCKS + [("newsletter", NewsletterSubscriptionBlock())], verbose_name="obsah stránky", blank=True, use_json_field=True, @@ -409,7 +401,6 @@ class UniwebHomePage( "uniweb.UniwebArticlesIndexPage", "uniweb.UniwebFormPage", "uniweb.UniwebPeoplePage", - "uniweb.UniwebNewsletterForm", ] ### OTHERS @@ -435,13 +426,7 @@ class UniwebFlexiblePage( ### FIELDS content = StreamField( - CONTENT_STREAM_BLOCKS - + [ - ( - "newsletter", - NewsletterSubscriptionBlockFactory("uniweb.UniwebNewsletterForm"), - ) - ], + CONTENT_STREAM_BLOCKS + [("newsletter", NewsletterSubscriptionBlock())], verbose_name="obsah stránky", blank=True, use_json_field=True, @@ -819,25 +804,3 @@ class UniwebPeoplePage( class Meta: verbose_name = "LidĂ©" - - -class UniwebNewsletterForm( - MetadataPageMixin, SubpageMixin, RoutablePageMixin, NewsletterFormMixin, Page -): - ### PANELS - - content_panels = NewsletterFormMixin.content_panels - - ### RELATIONS - - parent_page_types = ["uniweb.UniwebHomePage"] - subpage_types = [] - - ### OTHERS - - @route(r"^newsletter/$", name="newsletter") - def newsletter_post(self, posted): - return super().newsletter_post(posted) - - class Meta: - verbose_name = "Stránka pro zpracovánĂ newsletter poĹľadavkĹŻ"