diff --git a/calendar_utils/models.py b/calendar_utils/models.py index 1793c7de141917960967c60fecb50aae6179a669..34f970b0cb75787d7f4513def2d7521eeed33034 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 0000000000000000000000000000000000000000..d565ee04ea3d58ebc76b40458f8e939b1cf1c95d --- /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 22b585a7739a6e8f58b09be31a7a3054a97e9a10..9f31756a9fa0762a31802819f2a92b6491544e1a 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 a21b9f4bf4c2c5458f4ec7e1a4f9745822db25f8..3c967e5c8d791498c4fba093dfe7d546cef877c9 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 a4c0f86b4184521a416599d4ec425d0542df53f9..566ab68b10f935bccac126d85209e9cb371c0132 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 5d5605757efdf68d73d281147241d26e25554f9f..850e80ebf12a6fd514fa559008752dfb398812e1 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>