views.py 1.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344
  1. from django.contrib.auth.models import User, Group
  2. from rest_framework import viewsets
  3. from .serializers import UserSerializer, GroupSerializer
  4. from rest_framework.views import APIView
  5. from .permission import HasAPIKey
  6. from rest_framework.permissions import IsAuthenticated
  7. from .models import APIKey
  8. from rest_framework.response import Response
  9. from django.utils.decorators import method_decorator
  10. from .decorators import api_permission_required
  11. class UserViewSet(viewsets.ModelViewSet):
  12. """
  13. API endpoint that allows users to be viewed or edited.
  14. """
  15. queryset = User.objects.all().order_by('-date_joined')
  16. serializer_class = UserSerializer
  17. permission_classes = [HasAPIKey | IsAuthenticated]
  18. class GroupViewSet(viewsets.ModelViewSet):
  19. """
  20. API endpoint that allows groups to be viewed or edited.
  21. """
  22. queryset = Group.objects.all()
  23. serializer_class = GroupSerializer
  24. permission_classes = [HasAPIKey | IsAuthenticated]
  25. class SnippetUSER(APIView):
  26. permission_classes = []
  27. @method_decorator(api_permission_required("user", "post"), name='dispatch')
  28. def get(self, request, format=None):
  29. snippets = User.objects.all()
  30. serializer = UserSerializer(snippets, many=True, context={'request': request})
  31. try:
  32. header_key = request.META["HTTP_X_API_KEY"]
  33. db_key = APIKey.objects.get_from_key(header_key)
  34. print(db_key.has_permission_method("user", "get"))
  35. except:
  36. header_key = request.META["HTTP_X_CSRFTOKEN"]
  37. return Response(serializer.data)