profiles: serializers representation to parse avatar url

This commit is contained in:
AdrienLSH
2024-04-07 15:50:36 +02:00
parent 51f8dfcaa3
commit 13a078eb82
8 changed files with 123 additions and 127 deletions

View File

@ -0,0 +1,36 @@
from django.utils.translation import gettext as _
from django.contrib.auth.models import User
from rest_framework import permissions, status
from rest_framework import viewsets
from rest_framework.response import Response
from ..serializers import ProfileSerializer
from ..models import ProfileModel
class ProfileViewSet(viewsets.ModelViewSet):
queryset = ProfileModel.objects.all()
serializer_class = ProfileSerializer
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
def retrieve(self, request, username=None):
query = User.objects.filter(username=username)
if (not query):
return Response({"detail": _("Profile not found.")}, status.HTTP_404_NOT_FOUND)
query = self.get_queryset().filter(pk=query[0].pk)
if (not query):
return Response({"detail": _("Profile not found.")}, status.HTTP_404_NOT_FOUND)
instance = query[0]
return Response(self.serializer_class(instance).data)
def retrieve_id(self, request, pk=None):
query = self.get_queryset().filter(pk=pk)
if (not query):
return Response({"detail": _("Profile not found.")}, status.HTTP_404_NOT_FOUND)
instance = query[0]
return Response(self.serializer_class(instance).data)
def list(self, request):
serializer = ProfileSerializer(self.get_queryset(), many=True)
return Response(serializer.data)