views.py 1.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  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.decorators import api_view, permission_classes
  9. from rest_framework.response import Response
  10. class UserViewSet(viewsets.ModelViewSet):
  11. """
  12. API endpoint that allows users to be viewed or edited.
  13. """
  14. queryset = User.objects.all().order_by('-date_joined')
  15. serializer_class = UserSerializer
  16. keys = APIKey.objects.all()
  17. for i in keys:
  18. print()
  19. permission_classes = [HasAPIKey | IsAuthenticated]
  20. class GroupViewSet(viewsets.ModelViewSet):
  21. """
  22. API endpoint that allows groups to be viewed or edited.
  23. """
  24. queryset = Group.objects.all()
  25. serializer_class = GroupSerializer
  26. permission_classes = [HasAPIKey | IsAuthenticated]
  27. class SnippetUSER(APIView):
  28. permission_classes = []
  29. print(permission_classes)
  30. def get(self, request, format=None):
  31. snippets = User.objects.all()
  32. serializer = UserSerializer(snippets, many=True, context={'request': request})
  33. try:
  34. header_key = request.META["HTTP_X_API_KEY"]
  35. db_key = APIKey.objects.get_from_key(header_key)
  36. print(db_key.has_permission_method("user", "get"))
  37. except:
  38. header_key = request.META["HTTP_X_CSRFTOKEN"]
  39. return Response(serializer.data)