-
-
Save imbue/b5a5cffd212a409bf4a37424b51c7076 to your computer and use it in GitHub Desktop.
.py
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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