Skip to content

Commit

Permalink
Improve ICS calendar generation by handling missing and invalid dates
Browse files Browse the repository at this point in the history
  • Loading branch information
seanmorley15 authored Dec 15, 2024
2 parents f3c3e19 + edc5fdf commit e029654
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 @@ -1233,10 +1233,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 e029654

Please sign in to comment.