Ajout d'un export ical

This commit is contained in:
Jean-Marie Favreau 2024-06-26 16:19:02 +02:00
parent ab4bd28607
commit 4c431e515d
6 changed files with 75 additions and 21 deletions

View File

@ -175,6 +175,11 @@ class CalendarList:
def calendar_days_list(self):
return list(self.calendar_days.values())
def export_to_ics(self):
from .models import Event
events = [event for day in self.calendar_days.values() for event in day.events]
return Event.export_to_ics(events)
class CalendarMonth(CalendarList):
def __init__(self, year, month, filter):

View File

@ -1099,28 +1099,50 @@ class Event(models.Model):
cal.add("version", "2.0")
for event in events:
ed = event.get_consolidated_end_day()
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,
),
)
if event.start_time is None:
eventIcal.add(
"dtstart",
date(
event.start_day.year,
event.start_day.month,
event.start_day.day,
),
)
else:
eventIcal.add(
"dtstart",
datetime(
event.start_day.year,
event.start_day.month,
event.start_day.day,
event.start_time.hour,
event.start_time.minute,
),
)
if not event.end_day is None:
if event.end_time is None:
eventIcal.add(
"dtend",
date(
event.end_day.year,
event.end_day.month,
event.end_day.day,
),
)
else:
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(

View File

@ -61,6 +61,7 @@
{% if calendar.today_in_calendar %}
<a role="button" href="{% url 'aujourdhui' %}?{{ filter.get_url }}">Aujourd'hui</a>
<a role="button" href="{% url 'cette_semaine' %}?{{ filter.get_url }}">Cette semaine</a>
<a role="button" data-tooltip="Copiez ce lien et importez-le dans votre agenda" href="{% url 'export_ical' %}?{{ filter.get_url }}">Export ical {% picto_from_name "calendar" %}</a>
{% endif %}
</footer>
</article>

View File

@ -65,6 +65,7 @@ Semaine du {{ calendar.calendar_days_list.0.date|date|frdate }}
{% if calendar.firstdate.month != calendar.lastdate.month %}
<a role="button" href="{% url 'month_view' calendar.lastdate.year calendar.lastdate.month %}?{{ filter.get_url }}">{{ calendar.lastdate | date:"F o" }}</a>
{% endif %}
<a role="button" data-tooltip="Copiez ce lien et importez-le dans votre agenda" href="{% url 'export_ical' %}?{{ filter.get_url }}">Export ical {% picto_from_name "calendar" %}</a>
</footer>
</article>

View File

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

View File

@ -58,7 +58,7 @@ from django.contrib.auth.decorators import login_required, permission_required
from django.contrib import messages
from django.contrib.messages.views import SuccessMessageMixin
from .calendar import CalendarMonth, CalendarWeek, CalendarDay
from .calendar import CalendarMonth, CalendarWeek, CalendarDay, CalendarList
from .import_tasks.importer import URL2Events
from .import_tasks.extractor import Extractor
@ -203,6 +203,12 @@ class EventFilter(django_filters.FilterSet):
def get_cities(self):
return self.form.cleaned_data["city"]
def to_str(self, prefix=''):
result = ' '.join([c.name for c in self.get_categories()] + [t for t in self.get_tags()] + [c for c in self.get_cities()])
if len(result) > 0:
result = prefix + result
return result
def get_status_names(self):
if "status" in self.form.cleaned_data:
return [
@ -616,6 +622,21 @@ def export_event_ical(request, year, month, day, pk):
return response
def export_ical(request):
now = date.today()
filter = EventFilter(request.GET, queryset=get_event_qs(request), request=request)
calendar = CalendarList(now + timedelta(days=-7), now + timedelta(days=+60), filter)
ical = calendar.export_to_ics()
response = HttpResponse(content_type="text/calendar")
response.content = ical.to_ical().decode("utf-8").replace("\r\n", "\n")
response["Content-Disposition"] = "attachment; filename={0}{1}{2}".format(
'Pommes de lune', filter.to_str(' '), ".ics"
)
return response
class EventFilterAdmin(django_filters.FilterSet):
status = django_filters.MultipleChoiceFilter(
choices=Event.STATUS.choices, widget=forms.CheckboxSelectMultiple