Skip to content

Instantly share code, notes, and snippets.

@imbue

imbue/.py Secret

Last active September 19, 2020 14:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save imbue/b5a5cffd212a409bf4a37424b51c7076 to your computer and use it in GitHub Desktop.
Save imbue/b5a5cffd212a409bf4a37424b51c7076 to your computer and use it in GitHub Desktop.
.py
class CacheMixin(object):
cache_timeout = 60 * 5
def get_cache_timeout(self):
return self.cache_timeout
def dispatch(self, *args, **kwargs):
print(self.request.user)
# AnonymousUser
# ^ will always print "AnonymousUser"...
if hasattr(self.request, 'user') and self.request.user.is_authenticated:
key = 'view_{}'.format(self.request.user.pk)
return cache_page(self.get_cache_timeout(), key_prefix=key)(super().dispatch)(
*args, **kwargs)
else:
return super().dispatch(*args, **kwargs)
class ProjectViewSet(CacheMixin, ModelViewSet):
queryset = Project.objects.all()
serializer_class = ProjectSerializer
def get_queryset(self):
qb = super().get_queryset()
print(self.request.user)
# logged in user, eg: liam@github.com
# ... do stuff here based on authed user
return qb
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment