From ff498b7063d022ac07ee622b9c1753e5ce077c9e Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Tom=C3=A1=C5=A1=20Valenta?= <git@imaniti.org>
Date: Tue, 2 May 2023 21:46:03 +0200
Subject: [PATCH] merge personcalendarmixin and calendarmixin

---
 calendar_utils/models.py                      | 53 +++++++------------
 .../migrations/0114_merge_20230502_2140.py    | 14 +++++
 district/models.py                            |  4 +-
 .../district/district_person_page.html        |  4 +-
 main/models.py                                |  4 +-
 main/templates/main/main_person_page.html     |  4 +-
 6 files changed, 42 insertions(+), 41 deletions(-)
 create mode 100644 district/migrations/0114_merge_20230502_2140.py

diff --git a/calendar_utils/models.py b/calendar_utils/models.py
index 1793c7de..34f970b0 100644
--- a/calendar_utils/models.py
+++ b/calendar_utils/models.py
@@ -89,6 +89,26 @@ class CalendarMixin(models.Model):
     class Meta:
         abstract = True
 
+    def get_fullcalendar_data(self) -> str:
+        calendar_format_events = []
+
+        for event in self.calendar.past_events + self.calendar.future_events:
+            parsed_event = {
+                "allDay": event["all_day"],
+                "start": event["start"].isoformat(),
+                "end": event["end"].isoformat(),
+            }
+
+            if event["summary"] is not None:
+                parsed_event["title"] = event["summary"]
+
+            if event["url"] is not None:
+                parsed_event["url"] = event["url"]
+
+            calendar_format_events.append(parsed_event)
+
+        return json.dumps(calendar_format_events)
+
     def save(self, *args, **kwargs):
         # create or update related Calendar
         if self.calendar_url:
@@ -111,36 +131,3 @@ class CalendarMixin(models.Model):
             self.calendar = None
 
         super().save(*args, **kwargs)
-
-
-class PersonCalendarMixin(CalendarMixin):
-    def get_parsed_calendar_data(self) -> list:
-        calendar_format_events = []
-
-        for event in self.calendar.past_events + self.calendar.future_events:
-            parsed_event = {
-                "allDay": event["all_day"],
-                "start": event["start"].isoformat(),
-                "end": event["end"].isoformat(),
-            }
-
-            if event["summary"] is not None:
-                parsed_event["title"] = event["summary"]
-
-            if event["url"] is not None:
-                parsed_event["url"] = event["url"]
-
-            calendar_format_events.append(parsed_event)
-
-        return calendar_format_events
-
-    def get_context(self, request) -> dict:
-        context = super().get_context(request)
-
-        if self.calendar:
-            context["calendar_data"] = json.dumps(self.get_parsed_calendar_data())
-
-        return context
-
-    class Meta:
-        abstract = True
diff --git a/district/migrations/0114_merge_20230502_2140.py b/district/migrations/0114_merge_20230502_2140.py
new file mode 100644
index 00000000..d565ee04
--- /dev/null
+++ b/district/migrations/0114_merge_20230502_2140.py
@@ -0,0 +1,14 @@
+# Generated by Django 4.1.8 on 2023-05-02 19:40
+
+from django.db import migrations
+
+
+class Migration(migrations.Migration):
+
+    dependencies = [
+        ('district', '0110_remove_districtpersonpage_ical_calendar_url_and_more'),
+        ('district', '0113_merge_20230502_1854'),
+    ]
+
+    operations = [
+    ]
diff --git a/district/models.py b/district/models.py
index 22b585a7..9f31756a 100644
--- a/district/models.py
+++ b/district/models.py
@@ -26,7 +26,7 @@ from wagtail.fields import RichTextField, StreamField
 from wagtail.models import Orderable, Page
 from wagtailmetadata.models import MetadataPageMixin
 
-from calendar_utils.models import CalendarMixin, PersonCalendarMixin
+from calendar_utils.models import CalendarMixin
 from maps_utils.blocks import MapPointBlock
 from maps_utils.const import (
     DEFAULT_MAP_STYLE,
@@ -622,7 +622,7 @@ class DistrictPersonPage(
     ExtendedMetadataPageMixin,
     SubpageMixin,
     MetadataPageMixin,
-    PersonCalendarMixin,
+    CalendarMixin,
     Page,
 ):
     ### FIELDS
diff --git a/district/templates/district/district_person_page.html b/district/templates/district/district_person_page.html
index a21b9f4b..3c967e5c 100644
--- a/district/templates/district/district_person_page.html
+++ b/district/templates/district/district_person_page.html
@@ -24,10 +24,10 @@
       <div class="content-block w-full mb-16">
         {{ page.text|richtext }}
       </div>
-      {% if calendar_data %}
+      {% if page.calendar %}
         <section>
           <h2 class="head-alt-md mb-3"><i class="ico--calendar mr-4"></i>Kalendář</h2>
-          <ui-person-calendar events='{{ calendar_data|safe }}'></ui-person-calendar>
+          <ui-person-calendar events='{{ page.get_fullcalendar_data|safe }}'></ui-person-calendar>
         </section>
       {% endif %}
     </section>
diff --git a/main/models.py b/main/models.py
index a4c0f86b..566ab68b 100644
--- a/main/models.py
+++ b/main/models.py
@@ -29,7 +29,7 @@ from wagtail.models import Page
 from wagtail.search import index
 from wagtailmetadata.models import MetadataPageMixin
 
-from calendar_utils.models import PersonCalendarMixin
+from calendar_utils.models import CalendarMixin
 from elections2021.constants import REGION_CHOICES  # pozor, import ze sousedního modulu
 from instagram_utils.models import InstagramPost
 from shared.forms import SubscribeForm
@@ -733,7 +733,7 @@ class MainPersonPage(
     ExtendedMetadataPageMixin,
     SubpageMixin,
     MetadataPageMixin,
-    PersonCalendarMixin,
+    CalendarMixin,
     Page,
 ):
     ### FIELDS
diff --git a/main/templates/main/main_person_page.html b/main/templates/main/main_person_page.html
index 5d560575..850e80eb 100644
--- a/main/templates/main/main_person_page.html
+++ b/main/templates/main/main_person_page.html
@@ -109,14 +109,14 @@
       </section>
     {% endif %}
 
-    {% if calendar_data %}
+    {% if page.calendar %}
       <section class="grid-container no-max mr-0 mb-4 xl:mb-20">
         <div class="grid-content-with-right-side">
           <h2 class="head-4xl text-left">
             Kalendář
           </h2>
           <div class="xl:max-w-[1145px] __js-root">
-            <ui-person-calendar events='{{ calendar_data|safe }}'></ui-person-calendar>
+            <ui-person-calendar events='{{ page.get_fullcalendar_data|safe }}'></ui-person-calendar>
           </div>
         </div>
       </section>
-- 
GitLab