Use signals to update internal ics

This commit is contained in:
2025-01-03 13:56:40 +01:00
parent a60e1f1fdc
commit 65df55a635
5 changed files with 53 additions and 26 deletions

View File

@ -1,14 +1,10 @@
from datetime import timedelta
from pathlib import Path
from django.conf import settings
from django.http import Http404
from django.urls import reverse
from django.utils import timezone
from ics import Calendar, Event
from ninja_extra import ControllerBase, api_controller, route
from com.models import IcsCalendar, NewsDate
from com.models import IcsCalendar
from core.views.files import send_raw_file
@ -33,23 +29,4 @@ class CalendarController(ControllerBase):
@route.get("/internal.ics")
def calendar_internal(self):
calendar = Calendar()
for news_date in NewsDate.objects.filter(
news__is_moderated=True,
end_date__gte=timezone.now()
- (timedelta(days=30) * 60), # Roughly get the last 6 months
).prefetch_related("news"):
event = Event(
name=news_date.news.title,
begin=news_date.start_date,
end=news_date.end_date,
url=reverse("com:news_detail", kwargs={"news_id": news_date.news.id}),
)
calendar.events.add(event)
# Create a file so we can offload the download to the reverse proxy if available
file = self.CACHE_FOLDER / "internal.ics"
self.CACHE_FOLDER.mkdir(parents=True, exist_ok=True)
with open(file, "wb") as f:
_ = f.write(calendar.serialize().encode("utf-8"))
return send_raw_file(file)
return send_raw_file(IcsCalendar.get_internal())