From 3a4141652f628ecb687a3b7048abea0f911f1022 Mon Sep 17 00:00:00 2001
From: xaralis <filip.varecha@fragaria.cz>
Date: Mon, 25 Apr 2022 11:26:06 +0200
Subject: [PATCH] fix(district,region): center page spacing and conditional
 logic

---
 district/blocks.py                            |  10 +-
 ...rictcenterpage_sidebar_content_and_more.py | 175 ++++++++++++++++++
 district/models.py                            |   2 +-
 .../district/blocks/contact_block.html        |  12 +-
 .../district/district_center_page.html        |  15 +-
 region/blocks.py                              |  10 +-
 ..._alter_regioncenterpage_sidebar_content.py | 175 ++++++++++++++++++
 region/models.py                              |   2 +-
 .../region/blocks/contact_block.html          |  12 +-
 .../templates/region/region_center_page.html  |  15 +-
 .../shared/followus_snippet_column.html       |   6 +-
 11 files changed, 401 insertions(+), 33 deletions(-)
 create mode 100644 district/migrations/0056_alter_districtcenterpage_sidebar_content_and_more.py
 create mode 100644 region/migrations/0031_alter_regioncenterpage_sidebar_content.py

diff --git a/district/blocks.py b/district/blocks.py
index 60d1573bb..294e7930d 100644
--- a/district/blocks.py
+++ b/district/blocks.py
@@ -125,12 +125,14 @@ class CardLinkWithHeadlineBlock(StructBlock):
         label = "Karta odkazu s nadpisem"
 
 
+class CenterContactPersonBlock(StructBlock):
+    person = PageChooserBlock(label="Osoba", page_type=["district.DistrictPersonPage"])
+    position = CharBlock(label="Pozice", required=False)
+
+
 class CenterContactBlock(StructBlock):
     title = CharBlock(label="Titulek", required=True)
-    contact_list = ListBlock(
-        PageChooserBlock(label="Osoba", page_type=["district.DistrictPersonPage"]),
-        label="List kontaktů",
-    )
+    contact_list = ListBlock(CenterContactPersonBlock())
 
     class Meta:
         template = "district/blocks/contact_block.html"
diff --git a/district/migrations/0056_alter_districtcenterpage_sidebar_content_and_more.py b/district/migrations/0056_alter_districtcenterpage_sidebar_content_and_more.py
new file mode 100644
index 000000000..47b03cf8e
--- /dev/null
+++ b/district/migrations/0056_alter_districtcenterpage_sidebar_content_and_more.py
@@ -0,0 +1,175 @@
+# Generated by Django 4.0.3 on 2022-04-25 08:24
+
+import wagtail.core.blocks
+import wagtail.core.fields
+import wagtail.images.blocks
+from django.db import migrations
+from wagtail.core.blocks.stream_block import StreamValue
+
+
+def plain_to_structured(block):
+    modified_items = []
+
+    for item in block["value"]["contact_list"]:
+        modified_items.append(
+            {
+                **item,
+                "value": {
+                    "person": item["value"],
+                    "position": "",
+                },
+            }
+        )
+
+    block["value"]["contact_list"] = modified_items
+
+    return block
+
+
+def structured_to_plain(block):
+    modified_items = []
+
+    for item in block["value"]["contact_list"]:
+        modified_items.append({**item, "value": item["value"]["person"]})
+
+    block["value"]["contact_list"] = modified_items
+
+    return block
+
+
+def get_sidebar_content(page, mapper):
+    stream_data = []
+    mapped = False
+
+    for block in page.sidebar_content.raw_data:
+        if block["type"] == "contact":
+            stream_data.append(mapper(block))
+            mapped = True
+
+        else:
+            stream_data.append(block)
+
+    return stream_data, mapped
+
+
+def migrate(apps, mapper):
+    DistrictCenterPage = apps.get_model("district", "DistrictCenterPage")
+
+    for page in DistrictCenterPage.objects.all():
+        sidebar_content, mapped = get_sidebar_content(page, mapper)
+
+        if mapped:
+            page.sidebar_content = StreamValue(
+                page.sidebar_content, sidebar_content, is_lazy=True
+            )
+            page.save()
+
+
+def forwards(apps, schema_editor):
+    migrate(apps, plain_to_structured)
+
+
+def backwards(apps, schema_editor):
+    migrate(apps, structured_to_plain)
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("district", "0055_alter_districtcenterpage_content_and_more"),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name="districtcenterpage",
+            name="sidebar_content",
+            field=wagtail.core.fields.StreamField(
+                [
+                    (
+                        "address",
+                        wagtail.core.blocks.StructBlock(
+                            [
+                                (
+                                    "title",
+                                    wagtail.core.blocks.CharBlock(
+                                        label="Titulek", required=True
+                                    ),
+                                ),
+                                (
+                                    "map_image",
+                                    wagtail.images.blocks.ImageChooserBlock(
+                                        label="Obrázek mapy", required=False
+                                    ),
+                                ),
+                                (
+                                    "map_link",
+                                    wagtail.core.blocks.URLBlock(
+                                        label="Odkaz na detail mapy", required=False
+                                    ),
+                                ),
+                                (
+                                    "address",
+                                    wagtail.core.blocks.TextBlock(
+                                        label="Adresa", required=True
+                                    ),
+                                ),
+                                (
+                                    "address_info",
+                                    wagtail.core.blocks.TextBlock(
+                                        label="Info k adrese", required=False
+                                    ),
+                                ),
+                            ]
+                        ),
+                    ),
+                    (
+                        "contact",
+                        wagtail.core.blocks.StructBlock(
+                            [
+                                (
+                                    "title",
+                                    wagtail.core.blocks.CharBlock(
+                                        label="Titulek", required=True
+                                    ),
+                                ),
+                                (
+                                    "contact_list",
+                                    wagtail.core.blocks.ListBlock(
+                                        wagtail.core.blocks.StructBlock(
+                                            [
+                                                (
+                                                    "person",
+                                                    wagtail.core.blocks.PageChooserBlock(
+                                                        label="Osoba",
+                                                        page_type=[
+                                                            "district.DistrictPersonPage"
+                                                        ],
+                                                    ),
+                                                ),
+                                                (
+                                                    "position",
+                                                    wagtail.core.blocks.CharBlock(
+                                                        label="Pozice", required=False
+                                                    ),
+                                                ),
+                                            ]
+                                        )
+                                    ),
+                                ),
+                            ]
+                        ),
+                    ),
+                ],
+                blank=True,
+                verbose_name="Obsah bočního panelu",
+            ),
+        ),
+        migrations.AlterField(
+            model_name="districtcenterpage",
+            name="text",
+            field=wagtail.core.fields.RichTextField(
+                blank=True, null=True, verbose_name="Text"
+            ),
+        ),
+        migrations.RunPython(forwards, backwards),
+    ]
diff --git a/district/models.py b/district/models.py
index e08306c85..2d51744f9 100644
--- a/district/models.py
+++ b/district/models.py
@@ -899,7 +899,7 @@ class DistrictCenterPage(CalendarMixin, SubpageMixin, MetadataPageMixin, Page):
         verbose_name="Obsah",
         blank=True,
     )
-    text = RichTextField("Text", null=True)
+    text = RichTextField("Text", blank=True, null=True)
     sidebar_content = StreamField(
         [("address", blocks.AddressBlock()), ("contact", blocks.CenterContactBlock())],
         verbose_name="Obsah bočního panelu",
diff --git a/district/templates/district/blocks/contact_block.html b/district/templates/district/blocks/contact_block.html
index 55406a141..162d56baa 100644
--- a/district/templates/district/blocks/contact_block.html
+++ b/district/templates/district/blocks/contact_block.html
@@ -2,8 +2,10 @@
   {{ self.title }}
 </h2>
 
-{% for person_page in self.contact_list %}
-  <div class="{% if not forloop.last %}mb-4{% endif %}">
-    {% include "shared/person_badge_snippet.html" %}
-  </div>
-{% endfor %}
+<div class="space-y-4">
+  {% for contact_details in self.contact_list %}
+    {% with contact_details.person as person_page %}
+      {% include "shared/person_badge_snippet.html" with title=contact_details.position|default:person_page.job_function %}
+    {% endwith %}
+  {% endfor %}
+</div>
diff --git a/district/templates/district/district_center_page.html b/district/templates/district/district_center_page.html
index a4c194bd9..488d67787 100644
--- a/district/templates/district/district_center_page.html
+++ b/district/templates/district/district_center_page.html
@@ -9,30 +9,35 @@
         {{ page.title }}
       </h1>
       <h2 class="head-xs mt-2">
-        {{ page.subtitle }}
+        {{ page.perex }}
       </h2>
     </div>
   </aside>
 {% endblock %}
 
+{% block container_spacing %}pt-8 lg:pb-16{% endblock %}
+
 {% block content %}
     <article>
       <div class="lg:flex lg:space-x-8 xl:space-x-16">
         <div class="lg:w-3/5 xl:w-2/3">
           <div class="content-block">
             {{ page.text | richtext }}
-              {% for block in page.content %}
-                  {% include_block block %}
-              {% endfor %}
+
+            {% for block in page.content %}
+                {% include_block block %}
+            {% endfor %}
           </div>
           {% include "shared/small_calendar_snippet.html" %}
         </div>
-        <div class="pt-8 lg:w-2/5 xl:w-1/3 lg:pt-0">
+
+        <div class="pt-8 lg:w-2/5 xl:w-1/3 lg:pt-0 space-y-4">
           <div class="lg:card lg:elevation-10">
             <div class="lg:card__body">
               <div class="content-block">
                 {% for block in page.sidebar_content %}
                   {% include_block block %}
+
                   {% if not forloop.last %}
                     <hr>
                   {% endif %}
diff --git a/region/blocks.py b/region/blocks.py
index 9513750f4..feb697d79 100644
--- a/region/blocks.py
+++ b/region/blocks.py
@@ -125,12 +125,14 @@ class CardLinkWithHeadlineBlock(StructBlock):
         label = "Karta odkazu s nadpisem"
 
 
+class CenterContactPersonBlock(StructBlock):
+    person = PageChooserBlock(label="Osoba", page_type=["region.RegionPersonPage"])
+    position = CharBlock(label="Pozice", required=False)
+
+
 class CenterContactBlock(StructBlock):
     title = CharBlock(label="Titulek", required=True)
-    contact_list = ListBlock(
-        PageChooserBlock(label="Osoba", page_type=["region.RegionPersonPage"]),
-        label="List kontaktů",
-    )
+    contact_list = ListBlock(CenterContactPersonBlock())
 
     class Meta:
         template = "region/blocks/contact_block.html"
diff --git a/region/migrations/0031_alter_regioncenterpage_sidebar_content.py b/region/migrations/0031_alter_regioncenterpage_sidebar_content.py
new file mode 100644
index 000000000..0fe7cbd7c
--- /dev/null
+++ b/region/migrations/0031_alter_regioncenterpage_sidebar_content.py
@@ -0,0 +1,175 @@
+# Generated by Django 4.0.3 on 2022-04-25 09:18
+
+import wagtail.core.blocks
+import wagtail.core.fields
+import wagtail.images.blocks
+from django.db import migrations
+from wagtail.core.blocks.stream_block import StreamValue
+
+
+def plain_to_structured(block):
+    modified_items = []
+
+    for item in block["value"]["contact_list"]:
+        modified_items.append(
+            {
+                **item,
+                "value": {
+                    "person": item["value"],
+                    "position": "",
+                },
+            }
+        )
+
+    block["value"]["contact_list"] = modified_items
+
+    return block
+
+
+def structured_to_plain(block):
+    modified_items = []
+
+    for item in block["value"]["contact_list"]:
+        modified_items.append({**item, "value": item["value"]["person"]})
+
+    block["value"]["contact_list"] = modified_items
+
+    return block
+
+
+def get_sidebar_content(page, mapper):
+    stream_data = []
+    mapped = False
+
+    for block in page.sidebar_content.raw_data:
+        if block["type"] == "contact":
+            stream_data.append(mapper(block))
+            mapped = True
+
+        else:
+            stream_data.append(block)
+
+    return stream_data, mapped
+
+
+def migrate(apps, mapper):
+    DistrictCenterPage = apps.get_model("region", "RegionCenterPage")
+
+    for page in DistrictCenterPage.objects.all():
+        sidebar_content, mapped = get_sidebar_content(page, mapper)
+
+        if mapped:
+            page.sidebar_content = StreamValue(
+                page.sidebar_content, sidebar_content, is_lazy=True
+            )
+            page.save()
+
+
+def forwards(apps, schema_editor):
+    migrate(apps, plain_to_structured)
+
+
+def backwards(apps, schema_editor):
+    migrate(apps, structured_to_plain)
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ("region", "0030_alter_regioncenterpage_content_and_more"),
+    ]
+
+    operations = [
+        migrations.AlterField(
+            model_name="regioncenterpage",
+            name="sidebar_content",
+            field=wagtail.core.fields.StreamField(
+                [
+                    (
+                        "address",
+                        wagtail.core.blocks.StructBlock(
+                            [
+                                (
+                                    "title",
+                                    wagtail.core.blocks.CharBlock(
+                                        label="Titulek", required=True
+                                    ),
+                                ),
+                                (
+                                    "map_image",
+                                    wagtail.images.blocks.ImageChooserBlock(
+                                        label="Obrázek mapy", required=False
+                                    ),
+                                ),
+                                (
+                                    "map_link",
+                                    wagtail.core.blocks.URLBlock(
+                                        label="Odkaz na detail mapy", required=False
+                                    ),
+                                ),
+                                (
+                                    "address",
+                                    wagtail.core.blocks.TextBlock(
+                                        label="Adresa", required=True
+                                    ),
+                                ),
+                                (
+                                    "address_info",
+                                    wagtail.core.blocks.TextBlock(
+                                        label="Info k adrese", required=False
+                                    ),
+                                ),
+                            ]
+                        ),
+                    ),
+                    (
+                        "contact",
+                        wagtail.core.blocks.StructBlock(
+                            [
+                                (
+                                    "title",
+                                    wagtail.core.blocks.CharBlock(
+                                        label="Titulek", required=True
+                                    ),
+                                ),
+                                (
+                                    "contact_list",
+                                    wagtail.core.blocks.ListBlock(
+                                        wagtail.core.blocks.StructBlock(
+                                            [
+                                                (
+                                                    "person",
+                                                    wagtail.core.blocks.PageChooserBlock(
+                                                        label="Osoba",
+                                                        page_type=[
+                                                            "region.RegionPersonPage"
+                                                        ],
+                                                    ),
+                                                ),
+                                                (
+                                                    "position",
+                                                    wagtail.core.blocks.CharBlock(
+                                                        label="Pozice", required=False
+                                                    ),
+                                                ),
+                                            ]
+                                        )
+                                    ),
+                                ),
+                            ]
+                        ),
+                    ),
+                ],
+                blank=True,
+                verbose_name="Obsah bočního panelu",
+            ),
+        ),
+        migrations.AlterField(
+            model_name="regioncenterpage",
+            name="text",
+            field=wagtail.core.fields.RichTextField(
+                blank=True, null=True, verbose_name="Text"
+            ),
+        ),
+        migrations.RunPython(forwards, backwards),
+    ]
diff --git a/region/models.py b/region/models.py
index 15bf1bdf1..434ed8deb 100644
--- a/region/models.py
+++ b/region/models.py
@@ -885,7 +885,7 @@ class RegionCenterPage(CalendarMixin, SubpageMixin, MetadataPageMixin, Page):
         verbose_name="Obsah",
         blank=True,
     )
-    text = RichTextField("Text", null=True)
+    text = RichTextField("Text", blank=True, null=True)
     sidebar_content = StreamField(
         [("address", blocks.AddressBlock()), ("contact", blocks.CenterContactBlock())],
         verbose_name="Obsah bočního panelu",
diff --git a/region/templates/region/blocks/contact_block.html b/region/templates/region/blocks/contact_block.html
index 55406a141..162d56baa 100644
--- a/region/templates/region/blocks/contact_block.html
+++ b/region/templates/region/blocks/contact_block.html
@@ -2,8 +2,10 @@
   {{ self.title }}
 </h2>
 
-{% for person_page in self.contact_list %}
-  <div class="{% if not forloop.last %}mb-4{% endif %}">
-    {% include "shared/person_badge_snippet.html" %}
-  </div>
-{% endfor %}
+<div class="space-y-4">
+  {% for contact_details in self.contact_list %}
+    {% with contact_details.person as person_page %}
+      {% include "shared/person_badge_snippet.html" with title=contact_details.position|default:person_page.job_function %}
+    {% endwith %}
+  {% endfor %}
+</div>
diff --git a/region/templates/region/region_center_page.html b/region/templates/region/region_center_page.html
index 5206ea08f..74d3c2e02 100644
--- a/region/templates/region/region_center_page.html
+++ b/region/templates/region/region_center_page.html
@@ -9,30 +9,35 @@
         {{ page.title }}
       </h1>
       <h2 class="head-xs mt-2">
-        {{ page.subtitle }}
+        {{ page.perex }}
       </h2>
     </div>
   </aside>
 {% endblock %}
 
+{% block container_spacing %}pt-8 lg:pb-16{% endblock %}
+
 {% block content %}
     <article>
       <div class="lg:flex lg:space-x-8 xl:space-x-16">
         <div class="lg:w-3/5 xl:w-2/3">
           <div class="content-block">
             {{ page.text | richtext }}
-              {% for block in page.content %}
-                  {% include_block block %}
-              {% endfor %}
+
+            {% for block in page.content %}
+                {% include_block block %}
+            {% endfor %}
           </div>
           {% include "shared/small_calendar_snippet.html" %}
         </div>
-        <div class="pt-8 lg:w-2/5 xl:w-1/3 lg:pt-0">
+
+        <div class="pt-8 lg:w-2/5 xl:w-1/3 lg:pt-0 space-y-4">
           <div class="lg:card lg:elevation-10">
             <div class="lg:card__body">
               <div class="content-block">
                 {% for block in page.sidebar_content %}
                   {% include_block block %}
+
                   {% if not forloop.last %}
                     <hr>
                   {% endif %}
diff --git a/shared/templates/shared/followus_snippet_column.html b/shared/templates/shared/followus_snippet_column.html
index fead8e541..1834a2382 100644
--- a/shared/templates/shared/followus_snippet_column.html
+++ b/shared/templates/shared/followus_snippet_column.html
@@ -1,12 +1,12 @@
-<div class="flex flex-col">
+<div class="flex flex-col lg:space-y-4">
   <a href="{% firstof page.facebook page.root_page.facebook %}"
-     class="super-button bg-brands-facebook text-white container-padding--zero lg:container-padding--auto m-2">
+     class="super-button bg-brands-facebook text-white container-padding--zero lg:container-padding--auto lg:w-full">
     <span class="super-button__body">Sledujte nás na Facebooku</span>
     <i class="super-button__icon ico--facebook"></i>
   </a>
 
   <a href="{% firstof page.forum page.root_page.forum %}"
-     class="super-button bg-black text-white container-padding--zero lg:container-padding--auto m-2">
+     class="super-button bg-black text-white container-padding--zero lg:container-padding--auto lg:w-full">
     <span class="super-button__body">Sledujte naše fórum</span>
     <i class="super-button__icon ico--bubbles"></i>
   </a>
-- 
GitLab