export single event

This commit is contained in:
SebF 2024-05-04 15:47:39 +02:00
parent 0c283e4ae0
commit 1fef9eeef9
2 changed files with 51 additions and 4 deletions

View File

@ -26,6 +26,8 @@ from django.utils import timezone
from location_field.models.plain import PlainLocationField
from .calendar import CalendarList, CalendarDay
from icalendar import Calendar as icalCal
from icalendar import Event as icalEvent
import logging
@ -1090,6 +1092,44 @@ class Event(models.Model):
return (dtstart <= e_dtstart <= dtend) or (e_dtstart <= dtstart <= e_dtend)
def export_to_ics(events):
cal = icalCal()
# Some properties are required to be compliant
cal.add("prodid", "-//My calendar product//example.com//")
cal.add("version", "2.0")
for event in events:
eventIcal = icalEvent()
# mapping
eventIcal.add(
"dtstart",
datetime(
event.start_day.year,
event.start_day.month,
event.start_day.day,
event.start_time.hour,
event.start_time.minute,
),
)
eventIcal.add(
"dtend",
datetime(
event.end_day.year,
event.end_day.month,
event.end_day.day,
event.end_time.hour,
event.end_time.minute,
),
)
eventIcal.add("summary", event.title)
eventIcal.add("name", event.title)
eventIcal.add("description", event.description + "\r" + event.reference_urls[0])
eventIcal.add("location", event.exact_location or event.location)
cal.add_component(eventIcal)
return cal
class ContactMessage(models.Model):
class Meta:

View File

@ -10,7 +10,7 @@ from django.http import QueryDict
from django import forms
from django.contrib.postgres.search import SearchQuery, SearchHeadline
from django.http import HttpResponseRedirect
from django.http import HttpResponseRedirect, HttpResponse, HttpResponseNotFound, FileResponse
from django.urls import reverse
from collections import Counter
@ -600,10 +600,17 @@ def import_from_url(request):
def export_event_ical(request, pk):
event = get_object_or_404(Event, pk=pk)
logger = logging.getLogger(__name__)
logger.info("titre event" + event.title)
return HttpResponse(event.title + ".ical", content_type="text/calendar")
events = list()
events.append(event)
cal = Event.export_to_ics(events)
return FileResponse(
cal.to_ical().decode("utf-8").replace("\r\n", "\n"),
as_attachment=True,
filename= event.title + ".ics",
content_type="text/calendar")
class EventFilterAdmin(django_filters.FilterSet):