From 2da703552467e8820665c39cc5f98ec6f91357eb Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Alexa=20Valentov=C3=A1?= <git@imaniti.org> Date: Wed, 26 Mar 2025 10:02:33 +0100 Subject: [PATCH] Fix uniweb ecomail integration, center form --- shared/templates/styleguide2/form_page.html | 6 +- ..._uniwebformpage_email_field_id_and_more.py | 23 +++++++ ...7_uniwebformpage_submission_button_text.py | 18 +++++ uniweb/models.py | 68 ++++++++++++++++++- .../uniweb/blocks/embedded_subpage_form.html | 2 + 5 files changed, 113 insertions(+), 4 deletions(-) create mode 100644 uniweb/migrations/0136_uniwebformpage_email_field_id_and_more.py create mode 100644 uniweb/migrations/0137_uniwebformpage_submission_button_text.py diff --git a/shared/templates/styleguide2/form_page.html b/shared/templates/styleguide2/form_page.html index 09f5a076..7d46b792 100644 --- a/shared/templates/styleguide2/form_page.html +++ b/shared/templates/styleguide2/form_page.html @@ -84,7 +84,11 @@ </div> {% endfor %} - {% include "styleguide2/includes/atoms/buttons/round_button_form.html" with show_arrow_on_hover=True text="Odeslat" %} + {% if page.submission_button_text %} + {% include "styleguide2/includes/atoms/buttons/round_button_form.html" with show_arrow_on_hover=True text=page.submission_button_text %} + {% else %} + {% include "styleguide2/includes/atoms/buttons/round_button_form.html" with show_arrow_on_hover=True text="Odeslat" %} + {% endif %} </form> {% for block in page.content_after %} diff --git a/uniweb/migrations/0136_uniwebformpage_email_field_id_and_more.py b/uniweb/migrations/0136_uniwebformpage_email_field_id_and_more.py new file mode 100644 index 00000000..bbc7f50e --- /dev/null +++ b/uniweb/migrations/0136_uniwebformpage_email_field_id_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 5.0.7 on 2025-03-26 08:30 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('uniweb', '0135_uniwebformpage_subscribe_to_ecomail'), + ] + + operations = [ + migrations.AddField( + model_name='uniwebformpage', + name='email_field_id', + field=models.CharField(blank=True, null=True, verbose_name='ID email fieldu'), + ), + migrations.AddField( + model_name='uniwebformpage', + name='name_and_surname_field_id', + field=models.CharField(blank=True, null=True, verbose_name='ID jmĂ©no & pĹ™ĂjmenĂ fieldu'), + ), + ] diff --git a/uniweb/migrations/0137_uniwebformpage_submission_button_text.py b/uniweb/migrations/0137_uniwebformpage_submission_button_text.py new file mode 100644 index 00000000..249bcbc3 --- /dev/null +++ b/uniweb/migrations/0137_uniwebformpage_submission_button_text.py @@ -0,0 +1,18 @@ +# Generated by Django 5.0.7 on 2025-03-26 08:32 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ('uniweb', '0136_uniwebformpage_email_field_id_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='uniwebformpage', + name='submission_button_text', + field=models.CharField(default='Odeslat', verbose_name='Text tlaÄŤĂtka pro odeslánĂ formuláře'), + ), + ] diff --git a/uniweb/models.py b/uniweb/models.py index 9be40559..b48ce84c 100644 --- a/uniweb/models.py +++ b/uniweb/models.py @@ -29,6 +29,7 @@ from shared.blocks import ( NewsletterSubscriptionBlock, ) from shared.const import RICH_TEXT_DEFAULT_FEATURES +from shared.utils import subscribe_to_ecomail_newsletter from shared.models import ( CalendarMixin, CustomLogoMixin, @@ -501,6 +502,23 @@ class UniwebFormPage( default=False, ) + name_and_surname_field_id = models.CharField( + verbose_name="ID jmĂ©no & pĹ™ĂjmenĂ fieldu", + blank=True, + null=True + ) + + email_field_id = models.CharField( + verbose_name="ID email fieldu", + blank=True, + null=True + ) + + submission_button_text = models.CharField( + verbose_name="Text tlaÄŤĂtka pro odeslánĂ formuláře", + default="Odeslat", + ) + ### PANELS content_panels = AbstractForm.content_panels + [ @@ -508,6 +526,7 @@ class UniwebFormPage( FieldPanel("show_transparent_header"), FieldPanel("content_before"), InlinePanel("form_fields", label="formulář"), + FieldPanel("submission_button_text"), FieldPanel("content_after"), FieldPanel("content_landing"), ] @@ -516,7 +535,12 @@ class UniwebFormPage( submissions_panels = [FormSubmissionsPanel()] - advanced_panels = [FieldPanel("show_in_parent_page"), FieldPanel("subscribe_to_ecomail")] + advanced_panels = [ + FieldPanel("show_in_parent_page"), + FieldPanel("subscribe_to_ecomail"), + FieldPanel("name_and_surname_field_id"), + FieldPanel("email_field_id"), + ] edit_handler = TabbedInterface( [ @@ -540,13 +564,51 @@ class UniwebFormPage( def serve(self, request): if request.method == "GET" and self.show_in_parent_page: - if hasattr(self, "parent_page"): - return redirect(self.parent_page.url) + if hasattr(self, "root_page"): + return redirect(self.root_page.url) else: return redirect("/") return super().serve(request) + def process_form_submission(self, form): + cleaned_data = form.cleaned_data + + if ( + hasattr(self, "root_page") and self.root_page + and self.subscribe_to_ecomail + and self.name_and_surname_field_id in cleaned_data + and self.email_field_id in cleaned_data + ): + name_and_surname = cleaned_data[self.name_and_surname_field_id] + name_and_surname = name_and_surname.split(" ") + + name = "" + surname = "" + + if len(name_and_surname) > 1: + name = name_and_surname[0] + surname = " ".join(name_and_surname[1:]) + else: + name = name_and_surname[0] + + custom_data = { + "name": name, + } + + if surname != "": + custom_data["surname"] = surname + + subscribe_to_ecomail_newsletter( + cleaned_data[self.email_field_id], + self.root_page.ecomail_newsletter_list_tags, + self.root_page.ecomail_newsletter_list_source, + self.root_page.ecomail_newsletter_list_id, + custom_data + ) + + super().process_form_submission(form) + class Meta: verbose_name = "Formulářová stránka" diff --git a/uniweb/templates/uniweb/blocks/embedded_subpage_form.html b/uniweb/templates/uniweb/blocks/embedded_subpage_form.html index df216d08..af646239 100644 --- a/uniweb/templates/uniweb/blocks/embedded_subpage_form.html +++ b/uniweb/templates/uniweb/blocks/embedded_subpage_form.html @@ -13,6 +13,8 @@ {% if self.page.root_page.content_is_centered %} mx-auto + {% else %} + items-center {% endif %} " action="{% pageurl self.page %}" -- GitLab