From e546c923298d4a87b05d92234a6a1061292c74f7 Mon Sep 17 00:00:00 2001
From: =?UTF-8?q?Jan=20Bedna=C5=99=C3=ADk?= <jan.bednarik@gmail.com>
Date: Mon, 12 Sep 2022 12:23:33 +0200
Subject: [PATCH] calendar utils: Handle wrong calendars

---
 .../management/commands/update_callendars.py       |  6 +++++-
 calendar_utils/models.py                           | 14 ++++++++++++--
 2 files changed, 17 insertions(+), 3 deletions(-)

diff --git a/calendar_utils/management/commands/update_callendars.py b/calendar_utils/management/commands/update_callendars.py
index a66e95aa..e8375498 100644
--- a/calendar_utils/management/commands/update_callendars.py
+++ b/calendar_utils/management/commands/update_callendars.py
@@ -1,7 +1,11 @@
+import logging
+
 from django.core.management.base import BaseCommand
 
 from ...models import Calendar
 
+logger = logging.getLogger(__name__)
+
 
 class Command(BaseCommand):
     def handle(self, *args, **options):
@@ -12,7 +16,7 @@ class Command(BaseCommand):
                 cal.update_source()
                 self.stdout.write("+ ok")
             except Exception as e:
-                # TODO logging
+                logger.error("Calendar update failed for %s", cal.url, exc_info=True)
                 self.stdout.write("- failed")
                 self.stdout.write(str(e))
 
diff --git a/calendar_utils/models.py b/calendar_utils/models.py
index 4a072cda..b16d2137 100644
--- a/calendar_utils/models.py
+++ b/calendar_utils/models.py
@@ -1,3 +1,4 @@
+import logging
 from datetime import date, timedelta
 
 import arrow
@@ -7,6 +8,8 @@ from icalevnt import icalevents
 
 from .parser import process_event_list
 
+logger = logging.getLogger(__name__)
+
 
 def _convert_arrow_to_datetime(event):
     event["start"] = event["start"].datetime
@@ -86,11 +89,18 @@ class CalendarMixin(models.Model):
                     self.calendar.save()
             else:
                 self.calendar = Calendar.objects.create(url=self.calendar_url)
-            self.calendar.update_source()
+
+            try:
+                self.calendar.update_source()
+            except:
+                logger.error(
+                    "Calendar update failed for %s", self.calendar.url, exc_info=True
+                )
 
         # delete related Calendar when URL is cleared
         if not self.calendar_url and self.calendar:
-            self.calendar.delete()
+            # TODO handle revisions and maybe other live pages with the same calendar
+            # self.calendar.delete()
             self.calendar = None
 
         super().save(*args, **kwargs)
-- 
GitLab