fix: profile: block: fix test and simplify code

This commit is contained in:
starnakin 2024-04-08 13:00:46 +02:00
parent 9bbe5a4705
commit c1624cce83
2 changed files with 9 additions and 4 deletions

View File

@ -3,6 +3,8 @@ from django.http import HttpResponse
from django.contrib.auth.models import User from django.contrib.auth.models import User
from rest_framework import status from rest_framework import status
from profiles.serializers.ProfileSerializer import ProfileSerializer
from profiles.models.ProfileModel import ProfileModel
class ProfileTest(TestCase): class ProfileTest(TestCase):
def setUp(self): def setUp(self):
@ -29,6 +31,8 @@ class BlockTest(TestCase):
self.client.login(username=self.blocker.username, password=self.blocker_password) self.client.login(username=self.blocker.username, password=self.blocker_password)
response: HttpResponse = self.client.post(f'/api/profiles/block/{self.blocked.pk}') response: HttpResponse = self.client.post(f'/api/profiles/block/{self.blocked.pk}')
self.assertEqual(response.status_code, status.HTTP_201_CREATED) self.assertEqual(response.status_code, status.HTTP_201_CREATED)
blocked_profile = ProfileModel.objects.get(user=self.blocked)
response = self.client.get('/api/profiles/block') response = self.client.get('/api/profiles/block')
self.assertListEqual(response.json(), [{'blocked': self.blocked.pk}]) self.assertListEqual(response.json(), [ProfileSerializer(blocked_profile).data])

View File

@ -2,6 +2,7 @@ from rest_framework.views import APIView
from rest_framework.response import Response from rest_framework.response import Response
from rest_framework import permissions, status from rest_framework import permissions, status
from rest_framework.authentication import SessionAuthentication from rest_framework.authentication import SessionAuthentication
from rest_framework.request import Request
from django.contrib.auth.models import User from django.contrib.auth.models import User
from django.utils.translation import gettext as _ from django.utils.translation import gettext as _
@ -15,11 +16,11 @@ class GetBlocksView(APIView):
permission_classes = (permissions.IsAuthenticated,) permission_classes = (permissions.IsAuthenticated,)
authentication_classes = (SessionAuthentication,) authentication_classes = (SessionAuthentication,)
def get(self, request): def get(self, request: Request):
profiles = [] profiles = []
blocks = BlockModel.objects.filter(blocker=request.user.pk) blocks = BlockModel.objects.filter(blocker=request.user)
for block in blocks: for block in blocks:
profile = ProfileModel.objects.filter(user_id=block.blocked.pk) profile = ProfileModel.objects.filter(user=block.blocked)
if (profile.exists()): if (profile.exists()):
profiles.append(profile[0]) profiles.append(profile[0])