From dce53639d00baf7bf125351cbd3824b37b5e5200 Mon Sep 17 00:00:00 2001
From: "jindra12.underdark" <jindra12.underdark@gmail.com>
Date: Thu, 13 Apr 2023 23:05:36 +0200
Subject: [PATCH] Add models for calendar page

---
 calendar_utils/models.py | 42 ++++++++++++++++++++++++++++++++++++++++
 district/models.py       | 20 ++++++++++++++++---
 elections2021/models.py  | 10 ++++++++--
 senat_campaign/models.py | 10 +++++++++-
 uniweb/models.py         | 10 +++++++++-
 5 files changed, 85 insertions(+), 7 deletions(-)

diff --git a/calendar_utils/models.py b/calendar_utils/models.py
index e5840399..6d11e7b7 100644
--- a/calendar_utils/models.py
+++ b/calendar_utils/models.py
@@ -7,6 +7,9 @@ from django.core.serializers.json import DjangoJSONEncoder
 from django.core.validators import URLValidator, ValidationError
 from django.db import models
 from icalevents import icalevents
+from wagtail.admin.panels import FieldPanel
+from wagtail.models import Page
+from wagtailmetadata.models import MetadataPageMixin
 
 from .parser import process_event_list
 
@@ -85,6 +88,13 @@ class CalendarMixin(models.Model):
     calendar = models.ForeignKey(
         Calendar, null=True, blank=True, on_delete=models.PROTECT
     )
+    calendar_page = models.ForeignKey(
+        "calendar_utils.CalendarPage",
+        verbose_name="Stránka s kalendářem",
+        on_delete=models.PROTECT,
+        null=True,
+        blank=True,
+    )
 
     class Meta:
         abstract = True
@@ -143,3 +153,35 @@ class CalendarMixin(models.Model):
             self.calendar = None
 
         super().save(*args, **kwargs)
+
+
+class CalendarPage(SubpageMixin, MetadataPageMixin, CalendarMixin, Page):
+    """
+    Page for displaying full calendar
+    """
+
+    calendar_url = models.URLField(
+        "URL kalendáře ve formátu iCal", blank=False, null=True
+    )
+
+    ### PANELS
+
+    content_panels = Page.content_panels + [
+        FieldPanel("calendar_url"),
+    ]
+
+    ### RELATIONS
+
+    parent_page_types = [
+        "district.DistrictCenterPage",
+        "district.DistrictHomePage",
+        "elections2021.Elections2021CalendarPage",
+        "senat_campaign.SenatCampaignHomePage",
+        "uniweb.UniwebHomePage",
+    ]
+    subpage_types = []
+
+    ### OTHERS
+
+    class Meta:
+        verbose_name = "Stránka s kalendářem"
diff --git a/district/models.py b/district/models.py
index 48f3269f..9f14489f 100644
--- a/district/models.py
+++ b/district/models.py
@@ -274,7 +274,13 @@ class DistrictHomePage(
             [
                 FieldPanel("region_map_button_text"),
                 FieldPanel("calendar_button_text"),
-                FieldPanel("calendar_url"),
+                MultiFieldPanel(
+                    [
+                        FieldPanel("calendar_url"),
+                        PageChooserPanel("calendar_page"),
+                    ],
+                    "Kalendář",
+                ),
             ],
             gettext_lazy("Nastavení lišty s kalendářem a mapou"),
         ),
@@ -306,6 +312,7 @@ class DistrictHomePage(
         "district.DistrictProgramPage",
         "district.DistrictInteractiveProgramPage",
         "district.DistrictGeoFeatureCollectionPage",
+        "calendar_utils.CalendarPage",
     ]
 
     ### OTHERS
@@ -1295,7 +1302,14 @@ class DistrictCenterPage(
         FieldPanel("background_photo"),
         FieldPanel("text"),
         FieldPanel("content"),
-        FieldPanel("calendar_url"),
+        MultiFieldPanel(
+            [
+                FieldPanel("calendar_url"),
+                PageChooserPanel("calendar_page"),
+            ],
+            "Kalendář",
+        ),
+        PageChooserPanel("calendar_page"),
         FieldPanel("sidebar_content"),
     ]
 
@@ -1306,7 +1320,7 @@ class DistrictCenterPage(
     ### RELATIONS
 
     parent_page_types = ["district.DistrictHomePage"]
-    subpage_types = []
+    subpage_types = ["calendar_utils.CalendarPage"]
 
     ### OTHERS
 
diff --git a/elections2021/models.py b/elections2021/models.py
index dc170d52..6ffdb58a 100644
--- a/elections2021/models.py
+++ b/elections2021/models.py
@@ -2576,7 +2576,13 @@ class Elections2021CalendarPage(SubpageMixin, MetadataPageMixin, CalendarMixin,
 
     content_panels = Page.content_panels + [
         FieldPanel("photo"),
-        FieldPanel("calendar_url"),
+        MultiFieldPanel(
+            [
+                FieldPanel("calendar_url"),
+                PageChooserPanel("calendar_page"),
+            ],
+            "Kalendář",
+        ),
     ]
 
     promote_panels = [
@@ -2600,7 +2606,7 @@ class Elections2021CalendarPage(SubpageMixin, MetadataPageMixin, CalendarMixin,
     ### RELATIONS
 
     parent_page_types = ["elections2021.Elections2021HomePage"]
-    subpage_types = []
+    subpage_types = ["calendar_utils.CalendarPage"]
 
     ### OTHERS
 
diff --git a/senat_campaign/models.py b/senat_campaign/models.py
index 01dc34c5..8086e732 100644
--- a/senat_campaign/models.py
+++ b/senat_campaign/models.py
@@ -7,6 +7,7 @@ from wagtail.admin.panels import (
     FieldPanel,
     HelpPanel,
     MultiFieldPanel,
+    PageChooserPanel,
     PublishingPanel,
 )
 from wagtail.documents.blocks import DocumentChooserBlock
@@ -187,7 +188,13 @@ class SenatCampaignHomePage(
             ],
             heading="Sociální sítě",
         ),
-        FieldPanel("calendar_url"),
+        MultiFieldPanel(
+            [
+                FieldPanel("calendar_url"),
+                PageChooserPanel("calendar_page"),
+            ],
+            "Kalendář",
+        ),
         FieldPanel("donations_url"),
         FieldPanel("matomo_id"),
         FieldPanel("title_suffix"),
@@ -203,6 +210,7 @@ class SenatCampaignHomePage(
         "senat_campaign.SenatCampaignNewsIndexPage",
         "senat_campaign.SenatCampaignProgramPage",
         "senat_campaign.SenatCampaignCookiesPage",
+        "calendar_utils.CalendarPage",
     ]
 
     ### OTHERS
diff --git a/uniweb/models.py b/uniweb/models.py
index f60e3bd4..3d419da6 100644
--- a/uniweb/models.py
+++ b/uniweb/models.py
@@ -14,6 +14,7 @@ from wagtail.admin.panels import (
     InlinePanel,
     MultiFieldPanel,
     ObjectList,
+    PageChooserPanel,
     TabbedInterface,
 )
 from wagtail.contrib.forms.models import AbstractForm, AbstractFormField
@@ -380,7 +381,13 @@ class UniwebHomePage(
             ],
             "nastavení webu",
         ),
-        FieldPanel("calendar_url"),
+        MultiFieldPanel(
+            [
+                FieldPanel("calendar_url"),
+                PageChooserPanel("calendar_page"),
+            ],
+            "Kalendář",
+        ),
         MultiFieldPanel(
             [
                 FieldPanel("hide_footer"),
@@ -414,6 +421,7 @@ class UniwebHomePage(
         "uniweb.UniwebArticlesIndexPage",
         "uniweb.UniwebFormPage",
         "uniweb.UniwebPeoplePage",
+        "calendar_utils.CalendarPage",
     ]
 
     ### OTHERS
-- 
GitLab