# Path: django_backend/user_profile/serializers.py
# class UserSerializer(UserDetailsSerializer):
# 
#     website = serializers.URLField(source="userprofile.website", allow_blank=True, required=False)
#     about = serializers.CharField(source="userprofile.about", allow_blank=True, required=False)
# 
#     class Meta(UserDetailsSerializer.Meta):
#         fields = UserDetailsSerializer.Meta.fields + ('website', 'about')
# 
#     def update(self, instance, validated_data):
#         profile_data = validated_data.pop('userprofile', {})
#         website = profile_data.get('website')
#         about = profile_data.get('about')
# 
#         instance = super(UserSerializer, self).update(instance, validated_data)
# 
#         # get and update user profile
#         profile = instance.userprofile
#         if profile_data:
#             if website:
#                 profile.website = website
#             if about:
#                 profile.about = about
#             profile.save()
#         return instance


from django.contrib.auth import get_user_model
from .serializers import UserSerializer
from rest_framework import viewsets

User = get_user_model()


class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """

    queryset = User.objects.all().order_by('-date_joined')

# Path: django_backend/user_profile/serializers.py
# class UserSerializer(UserDetailsSerializer):
# 
#     website = serializers.URLField(source="userprofile.website", allow_blank=True, required=False)
#     about = serializers.CharField(source="userprofile.about", allow_blank=True, required=False)
# 
#     class Meta(UserDetailsSerializer.Meta):
#         fields = UserDetailsSerializer.Meta.fields + ('website', 'about')
# 
#     def update(self, instance, validated_data):
#         profile_data = validated_data.pop('userprofile', {})
#         website = profile_data.get('website')
#         about = profile_data.get('about')
# 
#         instance = super(UserSerializer, self).update(instance, validated_data)
# 
#         # get and update user profile
#         profile = instance.userprofile
#         if profile_data:
#             if website:
#                 profile.website = website
#             if about:
#                 profile.about = about
#             profile.save()
#         return instance


# Path: django_backend/user_profile/views.py
from django.contrib.auth import get_user_model
from .serializers import UserSerializer
from rest_framework import viewsets


User = get_user_model()


class UserViewSet(viewsets.ModelViewSet):
    """
    API endpoint that allows users to be viewed or edited.
    """

    queryset = User.objects.all().order_by('-date_joined')

    serializer_class = UserSerializer