You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

43 lines
1.6 KiB

3 years ago
3 years ago
2 years ago
3 years ago
2 years ago
3 years ago
3 years ago
2 years ago
  1. from dj_rest_auth.registration.serializers import RegisterSerializer
  2. from django.contrib.auth.models import User
  3. from django_filters.rest_framework import DjangoFilterBackend
  4. from rest_framework import filters, generics, status
  5. from rest_framework.permissions import IsAdminUser, IsAuthenticated
  6. from rest_framework.response import Response
  7. from rest_framework.views import APIView
  8. from .serializers import UserSerializer
  9. from projects.permissions import IsProjectAdmin
  10. class Me(APIView):
  11. permission_classes = (IsAuthenticated,)
  12. def get(self, request, *args, **kwargs):
  13. serializer = UserSerializer(request.user, context={"request": request})
  14. return Response(serializer.data)
  15. class Users(generics.ListAPIView):
  16. queryset = User.objects.all()
  17. serializer_class = UserSerializer
  18. permission_classes = [IsAuthenticated & IsProjectAdmin]
  19. pagination_class = None
  20. filter_backends = (DjangoFilterBackend, filters.SearchFilter)
  21. search_fields = ("username",)
  22. class UserCreation(generics.CreateAPIView):
  23. serializer_class = RegisterSerializer
  24. permission_classes = [IsAuthenticated & IsAdminUser]
  25. def create(self, request, *args, **kwargs):
  26. serializer = self.get_serializer(data=request.data)
  27. serializer.is_valid(raise_exception=True)
  28. user = self.perform_create(serializer)
  29. headers = self.get_success_headers(serializer.data)
  30. return Response(UserSerializer(user).data, status=status.HTTP_201_CREATED, headers=headers)
  31. def perform_create(self, serializer):
  32. user = serializer.save(self.request)
  33. return user