From 8039a944fcf952fb3670c4adff834c52bab2cf06 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Farka?= <stepanfarka11@gmail.com> Date: Wed, 9 Mar 2022 14:40:05 +0100 Subject: [PATCH] [ADD] unique email per people page, show email boolean, required email --- .../migrations/0042_auto_20220309_1319.py | 25 ++++++++++++ district/models.py | 21 +++++++++- .../district/district_person_page.html | 39 ++++++++++--------- region/migrations/0017_auto_20220309_1357.py | 25 ++++++++++++ region/models.py | 21 +++++++++- .../templates/region/region_person_page.html | 39 ++++++++++--------- 6 files changed, 132 insertions(+), 38 deletions(-) create mode 100644 district/migrations/0042_auto_20220309_1319.py create mode 100644 region/migrations/0017_auto_20220309_1357.py diff --git a/district/migrations/0042_auto_20220309_1319.py b/district/migrations/0042_auto_20220309_1319.py new file mode 100644 index 00000000..21267c9a --- /dev/null +++ b/district/migrations/0042_auto_20220309_1319.py @@ -0,0 +1,25 @@ +# Generated by Django 3.2.11 on 2022-03-09 12:19 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("district", "0041_auto_20220309_1109"), + ] + + operations = [ + migrations.AddField( + model_name="districtpersonpage", + name="show_email", + field=models.BooleanField( + default=True, verbose_name="Zobrazovat email na stránce?" + ), + ), + migrations.AlterField( + model_name="districtpersonpage", + name="email", + field=models.EmailField(max_length=254, null=True, verbose_name="Email"), + ), + ] diff --git a/district/models.py b/district/models.py index 905e64ba..c409c853 100644 --- a/district/models.py +++ b/district/models.py @@ -1,5 +1,6 @@ import random +from django.core.exceptions import ValidationError from django.core.paginator import Paginator from django.db import models from django.shortcuts import render @@ -16,6 +17,7 @@ from wagtail.admin.edit_handlers import ( StreamFieldPanel, TabbedInterface, ) +from wagtail.admin.forms import WagtailAdminPageForm from wagtail.contrib.table_block.blocks import TableBlock from wagtail.core.blocks import RichTextBlock from wagtail.core.fields import RichTextField, StreamField @@ -434,8 +436,23 @@ class DistrictPersonTag(TaggedItemBase): ) +class DistrictPersonPageForm(WagtailAdminPageForm): + def clean(self): + cleaned_data = super().clean() + email = cleaned_data.get("email", None) + if email: + pages_with_email = DistrictPersonPage.objects.filter(email=email) + num_pages_with_email = len(pages_with_email) + if num_pages_with_email > 1 or ( + num_pages_with_email == 1 and pages_with_email[0] != self.instance + ): + raise ValidationError({"email": "Stránka s tímto emailem již existuje"}) + return cleaned_data + + class DistrictPersonPage(SubpageMixin, MetadataPageMixin, Page): ### FIELDS + base_form_class = DistrictPersonPageForm job = models.CharField( "Povolání", @@ -465,7 +482,8 @@ class DistrictPersonPage(SubpageMixin, MetadataPageMixin, Page): ) text = RichTextField("text", blank=True, features=RICH_TEXT_FEATURES) - email = models.EmailField("Email", blank=True, null=True) + email = models.EmailField("Email", null=True) + show_email = models.BooleanField("Zobrazovat email na stránce?", default=True) phone = models.CharField("Telefon", max_length=16, blank=True, null=True) city = models.CharField("Město/obec", max_length=64, blank=True, null=True) age = models.IntegerField("Věk", blank=True, null=True) @@ -504,6 +522,7 @@ class DistrictPersonPage(SubpageMixin, MetadataPageMixin, Page): MultiFieldPanel( [ FieldPanel("email"), + FieldPanel("show_email"), FieldPanel("phone"), FieldPanel("city"), FieldPanel("age"), diff --git a/district/templates/district/district_person_page.html b/district/templates/district/district_person_page.html index d9a36d3a..7119f32a 100644 --- a/district/templates/district/district_person_page.html +++ b/district/templates/district/district_person_page.html @@ -90,25 +90,28 @@ {# </div>#} <hr> <div class="content-block"> - <div class="space-y-4"> - {% if page.phone %} - <div> - <h4>Telefon</h4> - <a href="tel:{{ page.phone }}" class="contact-line icon-link content-block--nostyle "> - <i class="ico--phone"></i><span>{{ page.phone }}</span> - </a> - </div> - {% endif %} - {% if page.phone %} - <div> - <h4>Email</h4> - <a href="mailto:{{ page.email }}" class="contact-line icon-link content-block--nostyle "> - <i class="ico--envelope"></i><span>{{ page.email }}</span> - </a> - </div> + {% if page.phone or page.email and page.show_email %} + <div class="space-y-4"> + {% if page.phone %} + <div> + <h4>Telefon</h4> + <a href="tel:{{ page.phone }}" class="contact-line icon-link content-block--nostyle "> + <i class="ico--phone"></i><span>{{ page.phone }}</span> + </a> + </div> + {% endif %} + {% if page.email and page.show_email %} + <div> + <h4>Email</h4> + <a href="mailto:{{ page.email }}" + class="contact-line icon-link content-block--nostyle "> + <i class="ico--envelope"></i><span>{{ page.email }}</span> + </a> + </div> + {% endif %} + </div> + <hr> {% endif %} - </div> - <hr> <h2>Lidé</h2> <div class="space-y-4 mt-4"> diff --git a/region/migrations/0017_auto_20220309_1357.py b/region/migrations/0017_auto_20220309_1357.py new file mode 100644 index 00000000..6a529c6f --- /dev/null +++ b/region/migrations/0017_auto_20220309_1357.py @@ -0,0 +1,25 @@ +# Generated by Django 3.2.11 on 2022-03-09 12:57 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("region", "0016_auto_20220309_1109"), + ] + + operations = [ + migrations.AddField( + model_name="regionpersonpage", + name="show_email", + field=models.BooleanField( + default=True, verbose_name="Zobrazovat email na stránce?" + ), + ), + migrations.AlterField( + model_name="regionpersonpage", + name="email", + field=models.EmailField(max_length=254, null=True, verbose_name="Email"), + ), + ] diff --git a/region/models.py b/region/models.py index f661c87f..d09dc929 100644 --- a/region/models.py +++ b/region/models.py @@ -1,5 +1,6 @@ import random +from django.core.exceptions import ValidationError from django.core.paginator import Paginator from django.db import models from django.shortcuts import render @@ -16,6 +17,7 @@ from wagtail.admin.edit_handlers import ( StreamFieldPanel, TabbedInterface, ) +from wagtail.admin.forms import WagtailAdminPageForm from wagtail.contrib.table_block.blocks import TableBlock from wagtail.core.blocks import RichTextBlock from wagtail.core.fields import RichTextField, StreamField @@ -427,7 +429,22 @@ class RegionPersonTag(TaggedItemBase): content_object = ParentalKey("region.RegionPersonPage", on_delete=models.CASCADE) +class RegionPersonPageForm(WagtailAdminPageForm): + def clean(self): + cleaned_data = super().clean() + email = cleaned_data.get("email", None) + if email: + pages_with_email = RegionPersonPage.objects.filter(email=email) + num_pages_with_email = len(pages_with_email) + if num_pages_with_email > 1 or ( + num_pages_with_email == 1 and pages_with_email[0] != self.instance + ): + raise ValidationError({"email": "Stránka s tímto emailem již existuje"}) + return cleaned_data + + class RegionPersonPage(SubpageMixin, MetadataPageMixin, Page): + base_form_class = RegionPersonPageForm ### FIELDS job = models.CharField( @@ -458,7 +475,8 @@ class RegionPersonPage(SubpageMixin, MetadataPageMixin, Page): ) text = RichTextField("text", blank=True, features=RICH_TEXT_FEATURES) - email = models.EmailField("Email", blank=True, null=True) + email = models.EmailField("Email", null=True) + show_email = models.BooleanField("Zobrazovat email na stránce?", default=True) phone = models.CharField("Telefon", max_length=16, blank=True, null=True) city = models.CharField("Město/obec", max_length=64, blank=True, null=True) age = models.IntegerField("Věk", blank=True, null=True) @@ -497,6 +515,7 @@ class RegionPersonPage(SubpageMixin, MetadataPageMixin, Page): MultiFieldPanel( [ FieldPanel("email"), + FieldPanel("show_email"), FieldPanel("phone"), FieldPanel("city"), FieldPanel("age"), diff --git a/region/templates/region/region_person_page.html b/region/templates/region/region_person_page.html index b09c579e..d094c82e 100644 --- a/region/templates/region/region_person_page.html +++ b/region/templates/region/region_person_page.html @@ -95,25 +95,28 @@ {# </div>#} <hr> <div class="content-block"> - <div class="space-y-4"> - {% if page.phone %} - <div> - <h4>Telefon</h4> - <a href="tel:{{ page.phone }}" class="contact-line icon-link content-block--nostyle "> - <i class="ico--phone"></i><span>{{ page.phone }}</span> - </a> - </div> - {% endif %} - {% if page.phone %} - <div> - <h4>Email</h4> - <a href="mailto:{{ page.email }}" class="contact-line icon-link content-block--nostyle "> - <i class="ico--envelope"></i><span>{{ page.email }}</span> - </a> - </div> + {% if page.phone or page.email and page.show_email %} + <div class="space-y-4"> + {% if page.phone %} + <div> + <h4>Telefon</h4> + <a href="tel:{{ page.phone }}" class="contact-line icon-link content-block--nostyle "> + <i class="ico--phone"></i><span>{{ page.phone }}</span> + </a> + </div> + {% endif %} + {% if page.email and page.show_email %} + <div> + <h4>Email</h4> + <a href="mailto:{{ page.email }}" + class="contact-line icon-link content-block--nostyle "> + <i class="ico--envelope"></i><span>{{ page.email }}</span> + </a> + </div> + {% endif %} + </div> + <hr> {% endif %} - </div> - <hr> <h2>Lidé</h2> <div class="space-y-4 mt-4"> -- GitLab