From edc5fdf1cc267d40df22571932389dc6bc26e6fb Mon Sep 17 00:00:00 2001 From: Sean Morley Date: Sun, 15 Dec 2024 13:04:52 -0500 Subject: [PATCH] Improve ICS calendar generation by handling missing and invalid start dates --- backend/server/adventures/views.py | 18 ++++++++++++++++-- 1 file changed, 16 insertions(+), 2 deletions(-) diff --git a/backend/server/adventures/views.py b/backend/server/adventures/views.py index acb03f0..092b098 100644 --- a/backend/server/adventures/views.py +++ b/backend/server/adventures/views.py @@ -1234,10 +1234,24 @@ def generate(self, request): for adventure in serializer.data: if adventure['visits']: for visit in adventure['visits']: + # Skip if start_date is missing + if not visit.get('start_date'): + continue + + # Parse start_date and handle end_date + try: + start_date = datetime.strptime(visit['start_date'], '%Y-%m-%d').date() + except ValueError: + continue # Skip if the start_date is invalid + + end_date = ( + datetime.strptime(visit['end_date'], '%Y-%m-%d').date() + timedelta(days=1) + if visit.get('end_date') else start_date + timedelta(days=1) + ) + + # Create event event = Event() event.add('summary', adventure['name']) - start_date = datetime.strptime(visit['start_date'], '%Y-%m-%d').date() - end_date = datetime.strptime(visit['end_date'], '%Y-%m-%d').date() + timedelta(days=1) if visit['end_date'] else start_date + timedelta(days=1) event.add('dtstart', start_date) event.add('dtend', end_date) event.add('dtstamp', datetime.now())