views.py 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647
  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. keys = APIKey.objects.all()
  18. for i in keys:
  19. print()
  20. permission_classes = [HasAPIKey | IsAuthenticated]
  21. class GroupViewSet(viewsets.ModelViewSet):
  22. """
  23. API endpoint that allows groups to be viewed or edited.
  24. """
  25. queryset = Group.objects.all()
  26. serializer_class = GroupSerializer
  27. permission_classes = [HasAPIKey | IsAuthenticated]
  28. class SnippetUSER(APIView):
  29. permission_classes = []
  30. @method_decorator(api_permission_required("user", "post"), name='dispatch')
  31. def get(self, request, format=None):
  32. snippets = User.objects.all()
  33. serializer = UserSerializer(snippets, many=True, context={'request': request})
  34. try:
  35. header_key = request.META["HTTP_X_API_KEY"]
  36. db_key = APIKey.objects.get_from_key(header_key)
  37. print(db_key.has_permission_method("user", "get"))
  38. except:
  39. header_key = request.META["HTTP_X_CSRFTOKEN"]
  40. return Response(serializer.data)