access profiles with username instead of id

This commit is contained in:
AdrienLSH
2024-01-18 10:31:56 +01:00
parent 1af55795d9
commit dee71c7c8d
7 changed files with 22 additions and 21 deletions

View File

@ -7,9 +7,9 @@ from . import views
urlpatterns = [
path("me", viewsets.MyProfileViewSet.as_view({'patch': 'partial_update', 'get': 'retrieve'}), name="my_profile_page"),
path("<int:pk>", viewsets.ProfileViewSet.as_view({'get': 'retrieve'}), name="profile_page"),
path("", viewsets.ProfileViewSet.as_view({'get': 'list'}), name="profiles_list"),
path("block", views.BlocksView.as_view(), name="block_page"),
path("block/<int:pk>", views.BlockView.as_view(), name="block_page"),
path("<str:username>", viewsets.ProfileViewSet.as_view({'get': 'retrieve'}), name="profile_page"),
] + static("/static/avatars/", document_root="./avatars")

View File

@ -7,6 +7,7 @@ from rest_framework.authentication import SessionAuthentication
from django.http import HttpRequest
from django.db.models import QuerySet
from django.contrib.auth.models import User
from .serializers import ProfileSerializer
from .models import ProfileModel
@ -17,10 +18,11 @@ class ProfileViewSet(viewsets.ModelViewSet):
parser_classes = (MultiPartParser, FormParser)
permission_classes = (permissions.IsAuthenticatedOrReadOnly,)
def retrieve(self, request: HttpRequest, pk=None):
if (not self.queryset().filter(pk=pk).exists()):
def retrieve(self, request: HttpRequest, username=None):
user = User.objects.filter(username=username)
if (not user):
return Response({"detail": "Profile not found."}, status=status.HTTP_404_NOT_FOUND)
instance = self.queryset().get(pk=pk)
instance = self.queryset().get(pk=user[0].pk)
instance.avatar_url.name = instance.avatar_url.name[instance.avatar_url.name.find("static") - 1:]
return Response(self.serializer_class(instance).data,
status=status.HTTP_200_OK)