Skip to content

Commit

Permalink
Improve ICS calendar generation by handling missing and invalid start…
Browse files Browse the repository at this point in the history
… dates
  • Loading branch information
seanmorley15 committed Dec 15, 2024
1 parent 54c859b commit edc5fdf
Showing 1 changed file with 16 additions and 2 deletions.
18 changes: 16 additions & 2 deletions backend/server/adventures/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -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())
Expand Down

0 comments on commit edc5fdf

Please sign in to comment.