From dada372cdebc6159f1887f4f877ddb24f5c1131d Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?=C3=89tienne=20Beaul=C3=A9?= Date: Sun, 13 Dec 2020 11:19:10 -0500 Subject: [PATCH] Don't cache tournaments/rounds for API views Getting odd intermittent 404 errors, and which were Django HTML rather than DRF JSON, so was either a wrong URL or the Tournament/Round not getting found. For that, the debate middleware will ignore API paths, and the properties in the API mixins will not use cached values, returning DRF 404s. --- tabbycat/api/mixins.py | 19 ++++++++++++++++--- tabbycat/utils/middleware.py | 2 +- 2 files changed, 17 insertions(+), 4 deletions(-) diff --git a/tabbycat/api/mixins.py b/tabbycat/api/mixins.py index bdb00da7523..24e15a54e09 100644 --- a/tabbycat/api/mixins.py +++ b/tabbycat/api/mixins.py @@ -1,18 +1,25 @@ import operator +from rest_framework.generics import get_object_or_404 from rest_framework.permissions import IsAdminUser -from tournaments.mixins import RoundFromUrlMixin, TournamentFromUrlMixin +from tournaments.models import Round, Tournament from .permissions import APIEnabledPermission, IsAdminOrReadOnly, PublicIfReleasedPermission, PublicPreferencePermission -class TournamentAPIMixin(TournamentFromUrlMixin): +class TournamentAPIMixin: tournament_field = 'tournament' access_operator = operator.eq access_setting = True + @property + def tournament(self): + if not hasattr(self, "_tournament"): + self._tournament = get_object_or_404(Tournament, slug=self.kwargs['tournament_slug']) + return self._tournament + def lookup_kwargs(self): return {self.tournament_field: self.tournament} @@ -28,10 +35,16 @@ def get_serializer_context(self): return context -class RoundAPIMixin(TournamentAPIMixin, RoundFromUrlMixin): +class RoundAPIMixin(TournamentAPIMixin): tournament_field = 'round__tournament' round_field = 'round' + @property + def round(self): + if not hasattr(self, "_round"): + self._round = get_object_or_404(Round, tournament=self.tournament, seq=self.kwargs['round_seq']) + return self._round + def perform_create(self, serializer): serializer.save(**{self.round_field: self.round}) diff --git a/tabbycat/utils/middleware.py b/tabbycat/utils/middleware.py index 967fa68aeac..cce7f618aa2 100644 --- a/tabbycat/utils/middleware.py +++ b/tabbycat/utils/middleware.py @@ -14,7 +14,7 @@ def __call__(self, request): return response def process_view(self, request, view_func, view_args, view_kwargs): - if 'tournament_slug' in view_kwargs: + if 'tournament_slug' in view_kwargs and request.path.split('/')[1] != 'api': cached_key = "%s_%s" % (view_kwargs['tournament_slug'], 'object') cached_tournament_object = cache.get(cached_key)