Add API cache
This commit is contained in:
parent
0c7d039106
commit
d435837c80
|
@ -1,6 +1,8 @@
|
||||||
from datetime import datetime, timedelta, date
|
from datetime import datetime, timedelta, date
|
||||||
|
|
||||||
from django.db.models import F, Q, Value
|
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 django_filters.rest_framework import DjangoFilterBackend
|
||||||
from rest_framework import viewsets
|
from rest_framework import viewsets
|
||||||
from rest_framework.filters import OrderingFilter
|
from rest_framework.filters import OrderingFilter
|
||||||
|
@ -12,6 +14,11 @@ from sncfgtfs.models import Agency, Stop, Route, Trip, StopTime, Calendar, Calen
|
||||||
Transfer, FeedInfo
|
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):
|
class AgencyViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
queryset = Agency.objects.all()
|
queryset = Agency.objects.all()
|
||||||
serializer_class = AgencySerializer
|
serializer_class = AgencySerializer
|
||||||
|
@ -19,6 +26,8 @@ class AgencyViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
filterset_fields = '__all__'
|
filterset_fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
|
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||||
|
@method_decorator(name='retrieve', decorator=[LAST_MODIFIED_CACHE])
|
||||||
class StopViewSet(viewsets.ReadOnlyModelViewSet):
|
class StopViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
queryset = Stop.objects.all()
|
queryset = Stop.objects.all()
|
||||||
serializer_class = StopSerializer
|
serializer_class = StopSerializer
|
||||||
|
@ -26,6 +35,8 @@ class StopViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
filterset_fields = '__all__'
|
filterset_fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
|
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||||
|
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||||
class RouteViewSet(viewsets.ReadOnlyModelViewSet):
|
class RouteViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
queryset = Route.objects.all()
|
queryset = Route.objects.all()
|
||||||
serializer_class = RouteSerializer
|
serializer_class = RouteSerializer
|
||||||
|
@ -33,6 +44,8 @@ class RouteViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
filterset_fields = '__all__'
|
filterset_fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
|
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||||
|
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||||
class TripViewSet(viewsets.ReadOnlyModelViewSet):
|
class TripViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
queryset = Trip.objects.all()
|
queryset = Trip.objects.all()
|
||||||
serializer_class = TripSerializer
|
serializer_class = TripSerializer
|
||||||
|
@ -40,6 +53,8 @@ class TripViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
filterset_fields = '__all__'
|
filterset_fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
|
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||||
|
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||||
class StopTimeViewSet(viewsets.ReadOnlyModelViewSet):
|
class StopTimeViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
queryset = StopTime.objects.order_by('id').all()
|
queryset = StopTime.objects.order_by('id').all()
|
||||||
serializer_class = StopTimeSerializer
|
serializer_class = StopTimeSerializer
|
||||||
|
@ -49,6 +64,8 @@ class StopTimeViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
ordering = ['stop_sequence', ]
|
ordering = ['stop_sequence', ]
|
||||||
|
|
||||||
|
|
||||||
|
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||||
|
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||||
class CalendarViewSet(viewsets.ReadOnlyModelViewSet):
|
class CalendarViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
queryset = Calendar.objects.all()
|
queryset = Calendar.objects.all()
|
||||||
serializer_class = CalendarSerializer
|
serializer_class = CalendarSerializer
|
||||||
|
@ -56,6 +73,8 @@ class CalendarViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
filterset_fields = '__all__'
|
filterset_fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
|
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||||
|
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||||
class CalendarDateViewSet(viewsets.ReadOnlyModelViewSet):
|
class CalendarDateViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
queryset = CalendarDate.objects.all()
|
queryset = CalendarDate.objects.all()
|
||||||
serializer_class = CalendarDateSerializer
|
serializer_class = CalendarDateSerializer
|
||||||
|
@ -63,12 +82,16 @@ class CalendarDateViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
filterset_fields = '__all__'
|
filterset_fields = '__all__'
|
||||||
|
|
||||||
|
|
||||||
|
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||||
|
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||||
class TransferViewSet(viewsets.ReadOnlyModelViewSet):
|
class TransferViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
queryset = Transfer.objects.all()
|
queryset = Transfer.objects.all()
|
||||||
serializer_class = TransferSerializer
|
serializer_class = TransferSerializer
|
||||||
filter_backends = [DjangoFilterBackend]
|
filter_backends = [DjangoFilterBackend]
|
||||||
|
|
||||||
|
|
||||||
|
@method_decorator(name='list', decorator=LAST_MODIFIED_CACHE)
|
||||||
|
@method_decorator(name='retrieve', decorator=LAST_MODIFIED_CACHE)
|
||||||
class FeedInfoViewSet(viewsets.ReadOnlyModelViewSet):
|
class FeedInfoViewSet(viewsets.ReadOnlyModelViewSet):
|
||||||
queryset = FeedInfo.objects.all()
|
queryset = FeedInfo.objects.all()
|
||||||
serializer_class = FeedInfoSerializer
|
serializer_class = FeedInfoSerializer
|
||||||
|
|
Loading…
Reference in New Issue