Skip to content

Commit

Permalink
Merge branch 'develop' into refactor/validate_fields
Browse files Browse the repository at this point in the history
  • Loading branch information
Denis-Shtanskiy committed Aug 4, 2024
2 parents 452af68 + 4d308e9 commit 43b2fcb
Show file tree
Hide file tree
Showing 6 changed files with 36 additions and 26 deletions.
33 changes: 11 additions & 22 deletions src/backend/api/v1/projects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -90,25 +90,10 @@ class Meta:
"project_specialists",
"status",
)

def _get_base_fields(self):
"""Метод получения полей создателя и владельца."""

return {
"creator": serializers.SlugRelatedField(
slug_field="username", read_only=True
),
"owner": serializers.SlugRelatedField(
slug_field="username", read_only=True
),
}

def get_fields(self):
"""метод получения полей сериализатора."""

fields = super().get_fields()
fields.update(self._get_base_fields())
return fields
read_only_fields: ClassVar[Tuple[str, ...]] = (
"creator",
"owner",
)


class ReadParticipantSerializer(CustomModelSerializer):
Expand Down Expand Up @@ -143,6 +128,9 @@ class ReadProjectSerializer(RecruitmentStatusMixin, BaseProjectSerializer):
recruitment_status = serializers.SerializerMethodField()
is_favorite = serializers.SerializerMethodField(read_only=True)
owner = serializers.SerializerMethodField()
creator = serializers.SlugRelatedField(
slug_field="username", read_only=True
)
project_participants = ReadParticipantSerializer(many=True)
unique_project_participants_skills = serializers.SerializerMethodField()

Expand All @@ -161,9 +149,10 @@ def get_is_favorite(self, project) -> bool:
В противном случе возвращает False.
Для неавторизованных пользователей всегда возвращает False.
"""
user = self.context["request"].user
if user.is_authenticated:
return project.favorited_by.filter(id=user.id).exists()
request = self.context.get("request", None)
if user := getattr(request, "user", None):
if user.is_authenticated:
return project.favorited_by.filter(id=user.id).exists()
return False

def get_owner(self, project):
Expand Down
8 changes: 8 additions & 0 deletions src/backend/tests/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -14,3 +14,11 @@ def user(django_user_model):
def user_client(user, client):
client.force_login(user)
return client


@pytest.fixture
def profile(user):
profile = user.profile
profile.name = "profile_name"
profile.save()
return profile
3 changes: 2 additions & 1 deletion src/backend/tests/profile/conftest.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,8 @@


@pytest.fixture
def profile(user):
def profile_new(user):
"""Профайл с незаполненной информацией"""
return user.profile


Expand Down
6 changes: 3 additions & 3 deletions src/backend/tests/profile/test_model.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,10 +19,10 @@ def test_create_profile_with_user():


@pytest.mark.django_db
def test_update_profiles_data(profile, update_data):
def test_update_profiles_data(profile_new, update_data):
"""Тест обновления информации в профиле"""
assert Profile.objects.filter(**update_data).count() == 0
for field, value in update_data.items():
setattr(profile, field, value)
profile.save()
setattr(profile_new, field, value)
profile_new.save()
assert Profile.objects.filter(**update_data).count() == 1
File renamed without changes.
12 changes: 12 additions & 0 deletions src/backend/tests/projects/test_serializers.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
from api.v1.projects.serializers import ReadProjectSerializer


def test_owner_field(profile, project):
valid_data = {
"id": profile.user.id,
"username": profile.user.username,
"name": profile.name,
"avatar": (profile.avatar.url if profile.avatar else None),
}
serializer = ReadProjectSerializer(instance=project)
assert serializer.data["owner"] == valid_data

0 comments on commit 43b2fcb

Please sign in to comment.