ajout de l’export ical vue jour

This commit is contained in:
SebF 2024-05-07 11:58:13 +02:00
parent 7b3aca21f4
commit bd310c7b44
4 changed files with 16 additions and 10 deletions

View File

@ -12,6 +12,7 @@ import os
from django.core.files import File from django.core.files import File
from django.utils import timezone from django.utils import timezone
from django.contrib.postgres.search import TrigramSimilarity from django.contrib.postgres.search import TrigramSimilarity
from django.http import HttpResponse
from django.db.models import Q, Count from django.db.models import Q, Count
import recurrence.fields import recurrence.fields
import recurrence import recurrence
@ -1092,7 +1093,7 @@ class Event(models.Model):
return (dtstart <= e_dtstart <= dtend) or (e_dtstart <= dtstart <= e_dtend) return (dtstart <= e_dtstart <= dtend) or (e_dtstart <= dtstart <= e_dtend)
def export_to_ics(events): def export_to_ics(filename, events):
cal = icalCal() cal = icalCal()
# Some properties are required to be compliant # Some properties are required to be compliant
cal.add("prodid", "-//My calendar product//example.com//") cal.add("prodid", "-//My calendar product//example.com//")
@ -1130,7 +1131,13 @@ class Event(models.Model):
cal.add_component(eventIcal) cal.add_component(eventIcal)
return cal 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(
filename, ".ics"
)
return response
class ContactMessage(models.Model): class ContactMessage(models.Model):

View File

@ -95,7 +95,7 @@
<h2>Voir aussi</h2> <h2>Voir aussi</h2>
</header> </header>
<div class="buttons"> <div class="buttons">
<a role="button" href="{% url 'export_events_ical' day.year day.month day.day%}" >Exporter ical {% picto_from_name "calendar" %}</a> <a role="button" href="{% url 'day_view' day.year day.month day.day%}" >Exporter ical {% picto_from_name "calendar" %}</a>
<a role="button" href="{% url 'week_view' day.year day|week %}?{{ filter.get_url }}">Toute la semaine</a> <a role="button" href="{% url 'week_view' day.year day|week %}?{{ filter.get_url }}">Toute la semaine</a>
</div> </div>
</article> </article>

View File

@ -13,6 +13,7 @@ urlpatterns = [
path("semaine/<int:year>/<int:week>/", week_view, name="week_view"), path("semaine/<int:year>/<int:week>/", week_view, name="week_view"),
path("mois/<int:year>/<int:month>/", month_view, name="month_view"), path("mois/<int:year>/<int:month>/", month_view, name="month_view"),
path("jour/<int:year>/<int:month>/<int:day>/", day_view, name="day_view"), path("jour/<int:year>/<int:month>/<int:day>/", day_view, name="day_view"),
path("jour/<int:year>/<int:month>/<int:day>?feed=ical", day_view, name="day_view"),
path("aujourdhui/", day_view, name="aujourdhui"), path("aujourdhui/", day_view, name="aujourdhui"),
path("cette-semaine/", week_view, name="cette_semaine"), path("cette-semaine/", week_view, name="cette_semaine"),
path("ce-mois-ci", month_view, name="ce_mois_ci"), path("ce-mois-ci", month_view, name="ce_mois_ci"),

View File

@ -325,6 +325,10 @@ def day_view(request, year=None, month=None, day=None):
categories = [(k, v) for k, v in categories.items()] categories = [(k, v) for k, v in categories.items()]
categories.sort(key=lambda k: -k[1]) categories.sort(key=lambda k: -k[1])
if "ical" in str(request):
response = Event.export_to_ics(str(day), cday.get_events())
return response
context = { context = {
"day": day, "day": day,
"events": cday.get_events(), "events": cday.get_events(),
@ -604,13 +608,7 @@ def export_event_ical(request, pk):
events = list() events = list()
events.append(event) events.append(event)
cal = Event.export_to_ics(events) response = Event.export_to_ics(event.title, 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 return response