diff --git a/district/migrations/0042_auto_20220309_1319.py b/district/migrations/0042_auto_20220309_1319.py new file mode 100644 index 0000000000000000000000000000000000000000..21267c9a3008fc173dd5de4efaa4c76690bd883c --- /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 905e64badf54565269215acfa707e427d3796fd6..c409c8530a96a2ac0207bf04ca8c3ed2a070bf3a 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 d9a36d3afbb0e79a3a74fcdea3c8a1dbfa68c83e..7119f32a157b5fe19ad7584673dea18cb37959f9 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 0000000000000000000000000000000000000000..6a529c6f1f1c425b5487a84877355a110a470155 --- /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 f661c87f7360418c52424bb7145cd8977de71b94..d09dc92995a0d3ce11c91dcc78a6c75c169d7ed4 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 b09c579ef592e06caf24a073806ff5626e56c022..d094c82e44458f589f2a7de37741911a98b08560 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">