Skip to content

Instantly share code, notes, and snippets.

@bmease
Created April 11, 2018 15:27
Show Gist options
  • Save bmease/932fe80064c2508930ca88b21dd9cb3c to your computer and use it in GitHub Desktop.
Save bmease/932fe80064c2508930ca88b21dd9cb3c to your computer and use it in GitHub Desktop.
class PluginView(generics.RetrieveAPIView):
serializer_class = PluginSerializer
def get_object(self):
developer = User.objects.get(auth_token=self.request.auth)
return get_object_or_404(Plugin, developer=developer)
class AvatarView(mixins.RetrieveModelMixin, mixins.CreateModelMixin, generics.GenericAPIView):
queryset = Avatar.objects.all()
serializer_class = AvatarSerializer
def get_object(self):
"""
Lookup the user's avatar that is associated to the developer making the request, based on API key.
Add +1 to the times_used counter.
"""
token = self.request.auth
developer = User.objects.get(auth_token=token)
queryset = self.filter_queryset(self.get_queryset())
obj = get_object_or_404(queryset, user=self.request.query_params.get('user', None), developer=developer)
obj.times_used += 1
obj.save()
return obj
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment