Skip to content

Commit

Permalink
added vacancy role in serializers
Browse files Browse the repository at this point in the history
  • Loading branch information
sh1nkey committed Jul 31, 2024
1 parent b6131b8 commit e7dd828
Show file tree
Hide file tree
Showing 2 changed files with 16 additions and 9 deletions.
14 changes: 6 additions & 8 deletions vacancy/managers.py
Original file line number Diff line number Diff line change
Expand Up @@ -37,14 +37,11 @@ def get_vacancy_for_detail_view(self):

class VacancyResponseManager(Manager):
def get_vacancy_response_for_list_view(self):
return (
self.get_queryset()
.select_related(
"vacancy",
"vacancy__project",
"vacancy__project__leader",
"accompanying_file",
)
return self.get_queryset().select_related(
"vacancy",
"vacancy__project",
"vacancy__project__leader",
"accompanying_file",
)

def get_vacancy_response_for_email(self):
Expand Down Expand Up @@ -75,6 +72,7 @@ def get_vacancy_response_for_detail_view(self):
.only(
"user",
"vacancy",
"vacancy__role",
"why_me",
"accompanying_file",
"is_approved",
Expand Down
11 changes: 10 additions & 1 deletion vacancy/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,6 +25,7 @@ class RequiredSkillsWriteSerializerMixin(RequiredSkillsSerializerMixin):

class AbstractVacancyReadOnlyFields(serializers.Serializer):
"""Abstract read-only fields for Vacancy."""

datetime_closed = serializers.DateTimeField(read_only=True)


Expand Down Expand Up @@ -149,7 +150,7 @@ class Meta:
]


class VacancyResponseListSerializer(serializers.ModelSerializer[VacancyResponse]):
class VacancyResponseListSerializer(serializers.ModelSerializer):
is_approved = serializers.BooleanField(read_only=True)
user = UserDetailSerializer(read_only=True)
user_id = serializers.IntegerField(write_only=True)
Expand All @@ -159,6 +160,9 @@ class VacancyResponseListSerializer(serializers.ModelSerializer[VacancyResponse]
required=False,
allow_null=True,
)
vacancy_role = (
serializers.SerializerMethodField()
) # SerializerMethodField to access related field

class Meta:
model = VacancyResponse
Expand All @@ -170,11 +174,15 @@ class Meta:
"accompanying_file",
"is_approved",
"vacancy",
"vacancy_role",
]
extra_kwargs = {
"user_id": {"write_only": True},
}

def get_vacancy_role(self, obj: VacancyResponse) -> str:
return obj.vacancy.role

def validate(self, attrs):
vacancy = attrs["vacancy"]
user = self.validate_user_exists(attrs["user_id"])
Expand All @@ -199,6 +207,7 @@ def create(self, validated_data):

class VacancyResponseFullFileInfoListSerializer(VacancyResponseListSerializer):
"""Returns full file info."""

accompanying_file = UserFileSerializer(read_only=True)


Expand Down

0 comments on commit e7dd828

Please sign in to comment.