fix: profile not found GET return error

This commit is contained in:
starnakin 2023-12-16 11:06:24 +01:00
parent 1b44d5c94f
commit 115ae9357a

View File

@ -17,6 +17,8 @@ class ProfileViewSet(viewsets.ModelViewSet):
permission_classes = (permissions.IsAuthenticatedOrReadOnly,) permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
def retrieve(self, request: HttpRequest, pk=None): def retrieve(self, request: HttpRequest, pk=None):
if (not ProfileModel.objects.filter(pk=pk).exists()):
return Response({"detail": "Profile not found."}, status=status.HTTP_404_NOT_FOUND)
instance = ProfileModel.objects.get(pk=pk) instance = ProfileModel.objects.get(pk=pk)
instance.avatar_url.name = instance.avatar_url.name[instance.avatar_url.name.find("static") - 1:] instance.avatar_url.name = instance.avatar_url.name[instance.avatar_url.name.find("static") - 1:]
return Response(self.serializer_class(instance).data, return Response(self.serializer_class(instance).data,
@ -32,9 +34,8 @@ class ProfileViewSet(viewsets.ModelViewSet):
serializer.save(user=self.request.user) serializer.save(user=self.request.user)
def perform_update(self, serializer): def perform_update(self, serializer):
query: QuerySet = ProfileModel.objects.filter(pk=self.request.user.pk) if (not ProfileModel.objects.filter(pk=pk).exists()):
if (not query.exists()): return Response({"detail": "Profile not found."}, status=status.HTTP_404_NOT_FOUND)
return Response("profile not found", status=status.HTTP_400_BAD_REQUEST)
profile: ProfileModel = ProfileModel.objects.get(pk=self.request.user.pk) profile: ProfileModel = ProfileModel.objects.get(pk=self.request.user.pk)
avatar = self.request.data.get("file", None) avatar = self.request.data.get("file", None)
if (avatar is not None): if (avatar is not None):