Skip to content

Commit

Permalink
Don't cache tournaments/rounds for API views
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
tienne-B committed Dec 13, 2020
1 parent 0ef97e6 commit dada372
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 4 deletions.
19 changes: 16 additions & 3 deletions tabbycat/api/mixins.py
Original file line number Diff line number Diff line change
@@ -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}

Expand All @@ -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})

Expand Down
2 changes: 1 addition & 1 deletion tabbycat/utils/middleware.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)

Expand Down

0 comments on commit dada372

Please sign in to comment.