From d5cc6253198fc437a9c310ee3103421d58620923 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?=C5=A0t=C4=9Bp=C3=A1n=20Farka?= <stepanfarka11@gmail.com>
Date: Wed, 11 May 2022 16:05:18 +0200
Subject: [PATCH] [FIX] copy page with calendar

---
 district/wagtail_hooks.py | 22 ++++++++++++++++++++++
 1 file changed, 22 insertions(+)

diff --git a/district/wagtail_hooks.py b/district/wagtail_hooks.py
index ea3324d4..461e746e 100644
--- a/district/wagtail_hooks.py
+++ b/district/wagtail_hooks.py
@@ -1,3 +1,7 @@
+from wagtail.core import hooks
+
+from district.models import DistrictCenterPage, DistrictHomePage
+
 # import re
 #
 # from wagtail.core import hooks
@@ -44,3 +48,21 @@
 #
 #     web_pages_id_list = root_page.get_descendants().live().values_list("id", flat=True)
 #     return pages.filter(id__in=web_pages_id_list)
+
+
+@hooks.register("after_copy_page")
+def handle_copy_calendar(request, origin_page, copied_page):
+    if (
+        isinstance(copied_page, DistrictHomePage)
+        or isinstance(copied_page, DistrictCenterPage)
+    ) and origin_page.specific.calendar_id:
+        # get current calendar from origin page
+        calendar = origin_page.specific.calendar
+
+        # create copy of calendar
+        calendar.pk = None
+        calendar.save()
+
+        # set new copy of calendar
+        copied_page.calendar_id = calendar.id
+        copied_page.save()
-- 
GitLab