From b486adbaec9195b1f54ab9fa55a2c5fdac9271b6 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Valenta?= <tomas@imaniti.org>
Date: Thu, 20 Jun 2024 11:01:11 +0200
Subject: [PATCH]  Custom positions for people on people page, run hooks

---
 district/blocks.py                            | 16 ++++-
 .../migrations/0161_auto_20240513_1733.py     | 20 +++++-
 .../migrations/0218_auto_20240601_1530.py     | 48 +++++++++++++-
 .../0232_alter_districtpeoplepage_people.py   | 66 +++++++++++++++++++
 .../0233_alter_districtpeoplepage_people.py   | 21 ++++++
 .../0234_alter_districtpeoplepage_people.py   | 21 ++++++
 shared/templates/styleguide2/people_page.html | 21 ++++--
 7 files changed, 202 insertions(+), 11 deletions(-)
 create mode 100644 district/migrations/0232_alter_districtpeoplepage_people.py
 create mode 100644 district/migrations/0233_alter_districtpeoplepage_people.py
 create mode 100644 district/migrations/0234_alter_districtpeoplepage_people.py

diff --git a/district/blocks.py b/district/blocks.py
index 6dd6cde9..24ffec61 100644
--- a/district/blocks.py
+++ b/district/blocks.py
@@ -126,6 +126,17 @@ class NewsletterBlock(StructBlock):
         template = "styleguide2/includes/organisms/main_section/newsletter_section.html"
 
 
+class PersonCustomPositionBlock(StructBlock):
+    page = PageChooserBlock(
+        page_type="district.DistrictPersonPage", label="Detail osoby"
+    )
+    position = CharBlock(
+        label="Pozice",
+        help_text="Pokud není pozice vyplněná, použije se pozice ze stránky osoby.",
+        required=False,
+    )
+
+
 class PeopleGroupBlock(StructBlock):
     title = CharBlock(label="Titulek")
     slug = CharBlock(
@@ -133,9 +144,8 @@ class PeopleGroupBlock(StructBlock):
         required=False,
         help_text="Není třeba vyplňovat, bude automaticky vyplněno",
     )
-    person_list = ListBlock(
-        PageChooserBlock(page_type="district.DistrictPersonPage", label="Detail osoby"),
-        label="Skupina osob",
+    person_list_with_custom_positions = ListBlock(
+        PersonCustomPositionBlock(), label="Skupina osob"
     )
 
     class Meta:
diff --git a/district/migrations/0161_auto_20240513_1733.py b/district/migrations/0161_auto_20240513_1733.py
index 98f143f5..f39d4639 100644
--- a/district/migrations/0161_auto_20240513_1733.py
+++ b/district/migrations/0161_auto_20240513_1733.py
@@ -2,11 +2,27 @@
 
 import wagtail
 from django.db import migrations, models
-
-from district.blocks import PeopleGroupBlock
+from wagtail.blocks import CharBlock, ListBlock, PageChooserBlock, StructBlock
 
 
 def migrate_people_blocks(apps, schema_editor):
+    # Recreate here because this block has been changed in the future
+
+    class PeopleGroupBlock(StructBlock):
+        title = CharBlock(label="Titulek")
+        slug = CharBlock(
+            label="Slug skupiny",
+            required=False,
+            help_text="Není třeba vyplňovat, bude automaticky vyplněno",
+        )
+
+        person_list = ListBlock(
+            PageChooserBlock(
+                page_type="district.DistrictPersonPage", label="Detail osoby"
+            ),
+            label="Skupina osob",
+        )
+
     # Get the page model
     DistrictPeoplePage = apps.get_model("district", "DistrictPeoplePage")
     DistrictPersonPage = apps.get_model("district", "DistrictPersonPage")
diff --git a/district/migrations/0218_auto_20240601_1530.py b/district/migrations/0218_auto_20240601_1530.py
index ca5a491c..12ad34de 100644
--- a/district/migrations/0218_auto_20240601_1530.py
+++ b/district/migrations/0218_auto_20240601_1530.py
@@ -5,11 +5,57 @@ from django.apps import apps as base_apps
 from django.contrib.contenttypes.management import create_contenttypes
 from django.db import migrations, transaction
 
-from district.blocks import ProgramGroupWithCandidatesBlock
+from district.blocks import (
+    ProgramGroupBlock,
+    ProgramGroupBlockCrossroad,
+    ProgramGroupBlockPopout,
+    CarouselProgramBlock,
+    CandidateListBlock,
+    CandidateSecondaryListBlock
+)
+from wagtail.blocks import StructBlock, CharBlock, RichTextBlock, BooleanBlock, StreamBlock
 from shared.blocks import SocialLinkBlock
 
 
 def migrate_programs(apps, schema_editor):
+    # Copy this block manually here, as it has been changed in future migrations.
+    class ProgramGroupWithCandidatesBlock(StructBlock):
+        title = CharBlock(
+            label="Název programu",
+            help_text="Např. 'Krajské volby 2024', 'Evropské volby 2024', ...",
+        )
+
+        preamble_content = RichTextBlock(
+            label="Preambule",
+            help_text="Text, který se zobrazí před přepínačem mezi kandidáty a programem.",
+            required=False,
+        )
+
+        primary_candidates = CandidateListBlock(
+            label="Osoby na čele kandidátky",
+            help_text="Zobrazí se ve velkých blocích na začátku stránky.",
+        )
+
+        secondary_candidates = CandidateSecondaryListBlock(
+            label="Ostatní osoby na kandidátce",
+            help_text="Zobrazí se v kompaktním seznamu pod čelem kandidátky.",
+        )
+
+        program = StreamBlock(
+            [
+                ("program_group", ProgramGroupBlock()),
+                ("program_group_crossroad", ProgramGroupBlockCrossroad()),
+                ("program_group_popout", ProgramGroupBlockPopout()),
+                (
+                    "carousel_program",
+                    CarouselProgramBlock(
+                        template="styleguide2/includes/molecules/program/program_block.html"
+                    ),
+                ),
+            ]
+        )
+
+
     # Get the models
     DistrictHomePage = apps.get_model("district", "DistrictHomePage")
 
diff --git a/district/migrations/0232_alter_districtpeoplepage_people.py b/district/migrations/0232_alter_districtpeoplepage_people.py
new file mode 100644
index 00000000..452f70d1
--- /dev/null
+++ b/district/migrations/0232_alter_districtpeoplepage_people.py
@@ -0,0 +1,66 @@
+# Generated by Django 5.0.6 on 2024-06-20 08:39
+
+import wagtail.blocks
+import wagtail.fields
+import wagtail.images.blocks
+from django.db import migrations
+from district.blocks import PeopleGroupBlock
+
+
+def migrate_people(apps, schema_editor):
+    DistrictPeoplePage = apps.get_model("district", "DistrictPeoplePage")
+    DistrictPersonPage = apps.get_model("district", "DistrictPersonPage")
+
+    for page in DistrictPeoplePage.objects.all():
+        prep_value = page.people.get_prep_value()
+
+        people_group_blocks = []
+
+        for person_group in prep_value:
+            people = []
+
+            if person_group["type"] != "people_group":
+                continue
+
+            person_group["value"]["person_list_with_custom_positions"] = []
+
+            for person_page_block in person_group["value"]["person_list"]:
+                person_block = {
+                    "page": person_page_block["value"],
+                    "position": "",
+                }
+
+                people.append(person_block)
+
+            group_block = PeopleGroupBlock().to_python(
+                {
+                    "title": person_group["value"]["title"],
+                    "slug": person_group["value"]["title"],
+                    "person_list_with_custom_positions": people,
+                }
+            )
+
+            people_group_blocks.append(group_block)
+
+        page.people = []
+        page.save()
+
+        for people_group_block in people_group_blocks:
+            page.people.append(("people_group", people_group_block))
+            page.save()
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('district', '0231_alter_districtnewprogrampage_program'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='districtpeoplepage',
+            name='people',
+            field=wagtail.fields.StreamField([('people_group', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(label='Titulek')), ('slug', wagtail.blocks.CharBlock(help_text='Není třeba vyplňovat, bude automaticky vyplněno', label='Slug skupiny', required=False)), ('person_list', wagtail.blocks.ListBlock(wagtail.blocks.PageChooserBlock(label='Detail osoby', page_type=['district.DistrictPersonPage']), label='Skupina osob')), ('person_list_with_custom_positions', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('page', wagtail.blocks.PageChooserBlock(label='Detail osoby', page_type=['district.DistrictPersonPage'])), ('position', wagtail.blocks.CharBlock(label='Pozice', required=False))]), label='Skupina osob (nastavitelné pozice)'))], label='Seznam osob')), ('team_group', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(label='Název sekce týmů')), ('slug', wagtail.blocks.CharBlock(help_text='Není třeba vyplňovat, bude automaticky vyplněno', label='Slug sekce', required=False)), ('team_list', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('headline', wagtail.blocks.CharBlock(label='Titulek bloku', required=False)), ('card_items', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('image', wagtail.images.blocks.ImageChooserBlock(label='Obrázek')), ('title', wagtail.blocks.CharBlock(label='Titulek', required=True)), ('text', wagtail.blocks.RichTextBlock(label='Krátký text pod nadpisem', required=False)), ('page', wagtail.blocks.PageChooserBlock(label='Stránka', page_type=['district.DistrictArticlesPage', 'district.DistrictCenterPage', 'district.DistrictContactPage', 'district.DistrictCrossroadPage', 'district.DistrictCustomPage', 'district.DistrictPeoplePage', 'district.DistrictGeoFeatureCollectionPage', 'district.DistrictCalendarPage', 'district.DistrictPdfPage', 'district.DistrictNewProgramPage'], required=False)), ('link', wagtail.blocks.URLBlock(label='Odkaz', required=False))], template='styleguide2/includes/molecules/boxes/card_box_block.html'), label='Karty s odkazy'))], label='Karta týmu'), label='Týmy'))]))], blank=True, verbose_name='Lidé a týmy'),
+        ),
+        migrations.RunPython(migrate_people)
+    ]
diff --git a/district/migrations/0233_alter_districtpeoplepage_people.py b/district/migrations/0233_alter_districtpeoplepage_people.py
new file mode 100644
index 00000000..e37b64ce
--- /dev/null
+++ b/district/migrations/0233_alter_districtpeoplepage_people.py
@@ -0,0 +1,21 @@
+# Generated by Django 5.0.6 on 2024-06-20 08:56
+
+import wagtail.blocks
+import wagtail.fields
+import wagtail.images.blocks
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('district', '0232_alter_districtpeoplepage_people'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='districtpeoplepage',
+            name='people',
+            field=wagtail.fields.StreamField([('people_group', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(label='Titulek')), ('slug', wagtail.blocks.CharBlock(help_text='Není třeba vyplňovat, bude automaticky vyplněno', label='Slug skupiny', required=False)), ('person_list_with_custom_positions', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('page', wagtail.blocks.PageChooserBlock(label='Detail osoby', page_type=['district.DistrictPersonPage'])), ('position', wagtail.blocks.CharBlock(label='Pozice', required=False))]), label='Skupina osob (nastavitelné pozice)'))], label='Seznam osob')), ('team_group', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(label='Název sekce týmů')), ('slug', wagtail.blocks.CharBlock(help_text='Není třeba vyplňovat, bude automaticky vyplněno', label='Slug sekce', required=False)), ('team_list', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('headline', wagtail.blocks.CharBlock(label='Titulek bloku', required=False)), ('card_items', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('image', wagtail.images.blocks.ImageChooserBlock(label='Obrázek')), ('title', wagtail.blocks.CharBlock(label='Titulek', required=True)), ('text', wagtail.blocks.RichTextBlock(label='Krátký text pod nadpisem', required=False)), ('page', wagtail.blocks.PageChooserBlock(label='Stránka', page_type=['district.DistrictArticlesPage', 'district.DistrictCenterPage', 'district.DistrictContactPage', 'district.DistrictCrossroadPage', 'district.DistrictCustomPage', 'district.DistrictPeoplePage', 'district.DistrictGeoFeatureCollectionPage', 'district.DistrictCalendarPage', 'district.DistrictPdfPage', 'district.DistrictNewProgramPage'], required=False)), ('link', wagtail.blocks.URLBlock(label='Odkaz', required=False))], template='styleguide2/includes/molecules/boxes/card_box_block.html'), label='Karty s odkazy'))], label='Karta týmu'), label='Týmy'))]))], blank=True, verbose_name='Lidé a týmy'),
+        ),
+    ]
diff --git a/district/migrations/0234_alter_districtpeoplepage_people.py b/district/migrations/0234_alter_districtpeoplepage_people.py
new file mode 100644
index 00000000..cc7b866a
--- /dev/null
+++ b/district/migrations/0234_alter_districtpeoplepage_people.py
@@ -0,0 +1,21 @@
+# Generated by Django 5.0.6 on 2024-06-20 09:00
+
+import wagtail.blocks
+import wagtail.fields
+import wagtail.images.blocks
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('district', '0233_alter_districtpeoplepage_people'),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name='districtpeoplepage',
+            name='people',
+            field=wagtail.fields.StreamField([('people_group', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(label='Titulek')), ('slug', wagtail.blocks.CharBlock(help_text='Není třeba vyplňovat, bude automaticky vyplněno', label='Slug skupiny', required=False)), ('person_list_with_custom_positions', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('page', wagtail.blocks.PageChooserBlock(label='Detail osoby', page_type=['district.DistrictPersonPage'])), ('position', wagtail.blocks.CharBlock(help_text='Pokud není pozice vyplněná, použije se pozice ze stránky osoby.', label='Pozice', required=False))]), label='Skupina osob'))], label='Seznam osob')), ('team_group', wagtail.blocks.StructBlock([('title', wagtail.blocks.CharBlock(label='Název sekce týmů')), ('slug', wagtail.blocks.CharBlock(help_text='Není třeba vyplňovat, bude automaticky vyplněno', label='Slug sekce', required=False)), ('team_list', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('headline', wagtail.blocks.CharBlock(label='Titulek bloku', required=False)), ('card_items', wagtail.blocks.ListBlock(wagtail.blocks.StructBlock([('image', wagtail.images.blocks.ImageChooserBlock(label='Obrázek')), ('title', wagtail.blocks.CharBlock(label='Titulek', required=True)), ('text', wagtail.blocks.RichTextBlock(label='Krátký text pod nadpisem', required=False)), ('page', wagtail.blocks.PageChooserBlock(label='Stránka', page_type=['district.DistrictArticlesPage', 'district.DistrictCenterPage', 'district.DistrictContactPage', 'district.DistrictCrossroadPage', 'district.DistrictCustomPage', 'district.DistrictPeoplePage', 'district.DistrictGeoFeatureCollectionPage', 'district.DistrictCalendarPage', 'district.DistrictPdfPage', 'district.DistrictNewProgramPage'], required=False)), ('link', wagtail.blocks.URLBlock(label='Odkaz', required=False))], template='styleguide2/includes/molecules/boxes/card_box_block.html'), label='Karty s odkazy'))], label='Karta týmu'), label='Týmy'))]))], blank=True, verbose_name='Lidé a týmy'),
+        ),
+    ]
diff --git a/shared/templates/styleguide2/people_page.html b/shared/templates/styleguide2/people_page.html
index 9d637dca..576e9da9 100644
--- a/shared/templates/styleguide2/people_page.html
+++ b/shared/templates/styleguide2/people_page.html
@@ -27,11 +27,22 @@
             {% for block in page.people %}
               {% if block.block_type == "people_group" %}
                 <template v-if="isCurrentView('{{ block.value.slug }}-{{ forloop.counter }}')">
-                  {% for person_page in block.value.person_list %}
-                    {% image person_page.profile_image fill-480x480 as profile_image %}
+                  {% if block.value.person_list %}
+                    {% for person_page in block.value.person_list %}
+                      {% image person_page.profile_image fill-480x480 as profile_image %}
 
-                    {% include 'styleguide2/includes/molecules/contact/contact_person_large_box.html' with image=profile_image name=person_page.title function=person_page.position telephone=person_page.phone mail=person_page.email url=person_page.url %}
-                  {% endfor %}
+                      {% include 'styleguide2/includes/molecules/contact/contact_person_large_box.html' with image=profile_image name=person_page.title function=person_page.position telephone=person_page.phone mail=person_page.email url=person_page.url %}
+                    {% endfor %}
+                  {% elif block.value.person_list_with_custom_positions %}
+                    {% for person in block.value.person_list_with_custom_positions %}
+                      {% with person.page as person_page %}
+                        {% image person_page.profile_image fill-480x480 as profile_image %}
+                        {% firstof person.position person_page.position as position %}
+
+                        {% include 'styleguide2/includes/molecules/contact/contact_person_large_box.html' with image=profile_image name=person_page.title function=position telephone=person_page.phone mail=person_page.email url=person_page.url %}
+                      {% endwith %}
+                    {% endfor %}
+                  {% endif %}
                 </template>
               {% endif %}
             {% endfor %}
@@ -68,4 +79,4 @@
   {% include 'styleguide2/includes/organisms/main_section/newsletter_section.html' %}
 {% endblock %}
 
-{% endblock content %}
+{% endblock content %}
\ No newline at end of file
-- 
GitLab