from rest_framework import generics, permissions
from django.contrib.auth.models import User
# from T.tings.models.models_users import TUserProfile
from T.tings.serializers.serializers_users import TUserSerializer


class TUserPermission(permissions.BasePermission):
    """
    Custom permission to only allow owners of an object to edit it.
    """

    def has_object_permission(self, request, view, obj):
        # anybody can submit GET, HEAD or OPTIONS requests...
        if request.method in permissions.SAFE_METHODS:
            return True
        # only the admin or collection owners can submit PUT, POST, or DELETE requests...
        user = request.user
        return user.is_superuser or user == obj


class TUserList(generics.ListCreateAPIView):

    queryset = User.objects.all()
    serializer_class = TUserSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly, TUserPermission,)


class TUserDetail(generics.RetrieveUpdateDestroyAPIView):
    queryset = User.objects.all()
    serializer_class = TUserSerializer
    permission_classes = (permissions.IsAuthenticatedOrReadOnly, TUserPermission,)


