Merge pull request '13_export_ical_event' (#116) from 13_export_ical_event into main

Reviewed-on: #116
This commit is contained in:
jmtrivial 2024-06-01 10:53:49 +02:00
commit ce1269827e
4 changed files with 69 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,46 @@ 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

@ -70,11 +70,12 @@
{% endif %}
</p>
</div>
{% if perms.agenda_culturel.change_event and not noedit %}
<div class="buttons">
{% include "agenda_culturel/edit-buttons-inc.html" with event=event %}
<a href="{% url 'export_event_ical' event.start_day.year event.start_day.month event.start_day.day event.id %}" role="button">Exporter ical {% picto_from_name "calendar" %}</a>
{% if perms.agenda_culturel.change_event and not noedit %}
{% include "agenda_culturel/edit-buttons-inc.html" with event=event %}
{% endif %}
</div>
{% endif %}
</footer>
</article>

View File

@ -158,6 +158,10 @@ urlpatterns = [
UnknownPlaceAddView.as_view(),
name="add_place_to_event",
),
path(
"event/<int:year>/<int:month>/<int:day>/<int:pk>/ical",
export_event_ical,
name="export_event_ical"),
]
if settings.DEBUG:

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
from django.urls import reverse
from collections import Counter
@ -598,6 +598,24 @@ def import_from_url(request):
)
def export_event_ical(request, year, month, day, pk):
event = get_object_or_404(Event, pk=pk)
event = event.get_recurrence_at_date(year, month, day)
events = list()
events.append(event)
cal = Event.export_to_ics(events)
response = HttpResponse(content_type="text/calendar")
response.content = cal.to_ical().decode("utf-8").replace("\r\n", "\n")
response["Content-Disposition"] = "attachment; filename={0}{1}".format(
event.title, ".ics"
)
return response
class EventFilterAdmin(django_filters.FilterSet):
status = django_filters.MultipleChoiceFilter(
choices=Event.STATUS.choices, widget=forms.CheckboxSelectMultiple