text: from django.contrib.auth.hashers import make_password
  from rest_framework.decorators import action
  from rest_framework.permissions import AllowAny, IsAdminUser, IsAuthenticated
  from rest_framework.response import Response
  from rest_framework.status import HTTP_400_BAD_REQUEST
  from rest_framework.viewsets import ModelViewSet
  from rest_framework_simplejwt.views import TokenObtainPairView
  
  from .models import User
  from .serializers import LoginSerializer, TokenSerializer, UserSerializer
  
  
  class UserViewSet(ModelViewSet):
      queryset = User.objects.all()
      serializer_class = UserSerializer
  
      def get_permissions(self):
          permission_classes = [
              AllowAny] if self.action == 'register' else [IsAdminUser]
          return [permission() for permission in permission_classes]
  
      @action(detail=False, methods=['post'])
      def register(self, request):
          data = request.data
          try:
              user = User.objects.create(
                  name=data['name'],
                  email=data['email'],
                  password=make_password(data['password'])
              )
              serializer = TokenSerializer(user, many=False)
              return Response(serializer.data)
          except:
              return Response('User with the given credentials already exists.', status=HTTP_400_BAD_REQUEST)
  
      @action(detail=False, url_path='profile', url_name='profile')
      def retrieve_profile(self, request):
          user = request.user
          serializer = UserSerializer(user, many=False)
          return Response(serializer.data)
  
      @action(detail=False, methods=['put'], url_path='profile/update', url_name='update-profile')
      def update_profile(self, request):
          user = request.user
          data = request.data
          serializer = TokenSerializer(user, many=False)
          user.name = data['name']
          user.email = data['email']
          if data['password'] != '':
              user.password = make_password(data['password'])
          user.save()
          return Response(serializer.data)
  
  
  class LoginView(TokenObtainPairView):
      serializer_class = LoginSerializer
  