profiles(block): get profiles of blocked users
This commit is contained in:
@ -1,10 +1,21 @@
|
||||
from rest_framework.serializers import ModelSerializer
|
||||
from rest_framework.serializers import (ModelSerializer,
|
||||
ReadOnlyField,
|
||||
SerializerMethodField)
|
||||
|
||||
from ..models import ProfileModel
|
||||
from ..models import BlockModel
|
||||
|
||||
|
||||
class BlockSerializer(ModelSerializer):
|
||||
blocked_username = ReadOnlyField(source='blocked.username')
|
||||
blocked_avatar = SerializerMethodField()
|
||||
|
||||
class Meta:
|
||||
model = BlockModel
|
||||
fields = ['blocked']
|
||||
fields = ['blocked', 'blocked_username', 'blocked_avatar']
|
||||
|
||||
def get_blocked_avatar(self, instance):
|
||||
blocked_profile = ProfileModel.objects.filter(user_id=instance.blocked.pk)
|
||||
if (blocked_profile.exists()):
|
||||
return blocked_profile[0].avatar.name
|
||||
return '/static/avatars/default.avif'
|
||||
|
@ -6,8 +6,8 @@ from rest_framework.authentication import SessionAuthentication
|
||||
from django.contrib.auth.models import User
|
||||
from django.utils.translation import gettext as _
|
||||
|
||||
from ..models import BlockModel
|
||||
from ..serializers.BlockSerializer import BlockSerializer
|
||||
from ..models import BlockModel, ProfileModel
|
||||
from ..serializers.ProfileSerializer import ProfileSerializer
|
||||
|
||||
|
||||
class GetBlocksView(APIView):
|
||||
@ -15,9 +15,14 @@ class GetBlocksView(APIView):
|
||||
authentication_classes = (SessionAuthentication,)
|
||||
|
||||
def get(self, request):
|
||||
blocked_profiles = BlockModel.objects.filter(blocker=request.user.pk)
|
||||
response_status: int = status.HTTP_200_OK if blocked_profiles else status.HTTP_204_NO_CONTENT
|
||||
return Response(BlockSerializer(blocked_profiles, many=True).data, response_status)
|
||||
profiles = []
|
||||
blocks = BlockModel.objects.filter(blocker=request.user.pk)
|
||||
for block in blocks:
|
||||
profile = ProfileModel.objects.filter(user_id=block.blocked.pk)
|
||||
if (profile.exists()):
|
||||
profiles.append(profile[0])
|
||||
|
||||
return Response(ProfileSerializer(profiles, many=True).data)
|
||||
|
||||
|
||||
class EditBlocksView(APIView):
|
||||
|
Reference in New Issue
Block a user