From d435837c80653f53052b7ca02a987911304f0ef0 Mon Sep 17 00:00:00 2001 From: Emmy D'Anello Date: Mon, 29 Jan 2024 22:48:50 +0100 Subject: [PATCH] Add API cache --- sncf/api/views.py | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/sncf/api/views.py b/sncf/api/views.py index 4e40115..8665375 100644 --- a/sncf/api/views.py +++ b/sncf/api/views.py @@ -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