Add API cache

This commit is contained in:
Emmy D'Anello 2024-01-29 22:48:50 +01:00
parent 0c7d039106
commit d435837c80
Signed by: ynerant
GPG Key ID: 3A75C55819C8CF85

View File

@ -1,6 +1,8 @@
from datetime import datetime, timedelta, date
from django.db.models import F, Q, Value
from django.utils.decorators import method_decorator
from django.views.decorators.http import last_modified
from django_filters.rest_framework import DjangoFilterBackend
from rest_framework import viewsets
from rest_framework.filters import OrderingFilter
@ -12,6 +14,11 @@ from sncfgtfs.models import Agency, Stop, Route, Trip, StopTime, Calendar, Calen
Transfer, FeedInfo
LAST_MODIFIED_CACHE = last_modified(lambda *args, **kwargs: datetime.fromisoformat(FeedInfo.objects.get().version))
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
class AgencyViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Agency.objects.all()
serializer_class = AgencySerializer
@ -19,6 +26,8 @@ class AgencyViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = '__all__'
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
@method_decorator(name='retrieve', decorator=[LAST_MODIFIED_CACHE])
class StopViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Stop.objects.all()
serializer_class = StopSerializer
@ -26,6 +35,8 @@ class StopViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = '__all__'
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
class RouteViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Route.objects.all()
serializer_class = RouteSerializer
@ -33,6 +44,8 @@ class RouteViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = '__all__'
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
class TripViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Trip.objects.all()
serializer_class = TripSerializer
@ -40,6 +53,8 @@ class TripViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = '__all__'
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
class StopTimeViewSet(viewsets.ReadOnlyModelViewSet):
queryset = StopTime.objects.order_by('id').all()
serializer_class = StopTimeSerializer
@ -49,6 +64,8 @@ class StopTimeViewSet(viewsets.ReadOnlyModelViewSet):
ordering = ['stop_sequence', ]
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
class CalendarViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Calendar.objects.all()
serializer_class = CalendarSerializer
@ -56,6 +73,8 @@ class CalendarViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = '__all__'
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
class CalendarDateViewSet(viewsets.ReadOnlyModelViewSet):
queryset = CalendarDate.objects.all()
serializer_class = CalendarDateSerializer
@ -63,12 +82,16 @@ class CalendarDateViewSet(viewsets.ReadOnlyModelViewSet):
filterset_fields = '__all__'
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
class TransferViewSet(viewsets.ReadOnlyModelViewSet):
queryset = Transfer.objects.all()
serializer_class = TransferSerializer
filter_backends = [DjangoFilterBackend]
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
class FeedInfoViewSet(viewsets.ReadOnlyModelViewSet):
queryset = FeedInfo.objects.all()
serializer_class = FeedInfoSerializer