Skip to content

Commit

Permalink
Merge pull request #387 from Pet-projects-CodePET/refactor/requests_a…
Browse files Browse the repository at this point in the history
…nd_serializers

Refactor/requests and serializers
  • Loading branch information
Denis-Shtanskiy authored Nov 3, 2024
2 parents f29e7ff + 2c67f78 commit 02770f3
Show file tree
Hide file tree
Showing 23 changed files with 468 additions and 201 deletions.
5 changes: 4 additions & 1 deletion src/backend/api/v1/general/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,10 @@ class ProfessionSerializer(CustomModelSerializer):

class Meta:
model = Profession
fields = "__all__"
fields = (
"speciality",
"specialization",
)


class SkillSerializer(CustomModelSerializer):
Expand Down
9 changes: 8 additions & 1 deletion src/backend/api/v1/profile/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,13 @@ class Meta:
"specialists",
)

def __init__(self, *args, **kwargs):
exclude = kwargs.pop("exclude", None)
super().__init__(*args, **kwargs)
if exclude:
for field in exclude:
self.fields.pop(field, None)


class ProfilePreviewReadSerializer(BaseProfileSerializer):
"""Сериализатор для чтения превью профилей специалистов."""
Expand Down Expand Up @@ -182,7 +189,7 @@ def get_projects(profile: Profile) -> list[Optional[dict]]:
Project.objects.filter(
Q(creator=user) | Q(participants=user) | Q(owner=user)
)
.exclude(status=Project.DRAFT)
.exclude(project_status=Project.DRAFT)
.only("id", "name")
)

Expand Down
6 changes: 3 additions & 3 deletions src/backend/api/v1/projects/constants.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,15 +3,15 @@

PROJECT_PARTICIPATION_REQUEST_DESTROY_ONLY_FIELDS = (
"user__id",
"status",
"project__project_status",
)
PROJECT_PARTICIPATION_REQUEST_LIST_ONLY_FIELDS = (
*PROJECT_PARTICIPATION_REQUEST_DESTROY_ONLY_FIELDS,
"project__creator__id",
"project__owner__id",
"project__name",
"project__directions",
"project__status",
"request_status",
"position__is_required",
"position__profession",
"position__count",
Expand All @@ -27,5 +27,5 @@
PROJECT_PARTICIPATION_REQUEST_ONLY_FIELDS = {
"destroy": PROJECT_PARTICIPATION_REQUEST_DESTROY_ONLY_FIELDS,
"list": PROJECT_PARTICIPATION_REQUEST_LIST_ONLY_FIELDS,
"retrieve": PROJECT_PARTICIPATION_REQUEST_RETRIEVE_ONLY_FIELDS,
# "retrieve": PROJECT_PARTICIPATION_REQUEST_RETRIEVE_ONLY_FIELDS,
}
26 changes: 21 additions & 5 deletions src/backend/api/v1/projects/filters.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,14 +5,20 @@

from apps.general.constants import LEVEL_CHOICES
from apps.general.models import Profession, Skill
from apps.projects.constants import BUSYNESS_CHOICES, PROJECT_STATUS_CHOICES
from apps.projects.models import Direction, Project
from apps.projects.constants import (
BUSYNESS_CHOICES,
PROJECT_STATUS_CHOICES,
RequestStatuses,
)
from apps.projects.models import Direction, ParticipationRequest, Project


class ProjectFilter(FilterSet):
"""Класс фильтрации проектов, по запросу на главной странице."""

status = filters.MultipleChoiceFilter(choices=PROJECT_STATUS_CHOICES)
project_status = filters.MultipleChoiceFilter(
choices=PROJECT_STATUS_CHOICES
)
started = filters.DateFromToRangeFilter()
ended = filters.DateFromToRangeFilter()
recruitment_status = filters.NumberFilter(
Expand All @@ -39,7 +45,7 @@ class ProjectFilter(FilterSet):
class Meta:
model = Project
fields = (
"status",
"project_status",
"started",
"ended",
"recruitment_status",
Expand Down Expand Up @@ -75,7 +81,7 @@ def get_project_role(self, queryset, name, value):
return queryset

def project_search(self, queryset, name, value):
if value:
if len(value) >= 3:
search_language = detect(value)
if search_language == "ru":
search_query = SearchQuery(value, config="russian")
Expand All @@ -94,3 +100,13 @@ def filter_is_favorite_project(self, queryset, name, value):
if value == 1 and user.is_authenticated:
return queryset.filter(favorited_by=user)
return queryset


class MyRequestsFilter(FilterSet):
"""Класс фильтрации запросов на участие в проектах."""

request_status = filters.MultipleChoiceFilter(choices=RequestStatuses)

class Meta:
model = ParticipationRequest
fields = ("request_status",)
2 changes: 1 addition & 1 deletion src/backend/api/v1/projects/mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -115,7 +115,7 @@ def validate(self, attrs) -> Dict[str, Any]:
self, "instance", None
) # mypy по другому не пропускал
started = attrs.get("started", instance.started if instance else None)
ended = attrs.get("ended", instance.started if instance else None)
ended = attrs.get("ended", instance.ended if instance else None)
if started + datetime.timedelta(days=2) > ended:
errors.setdefault("invalid_dates", []).append(
"Дата завершения проекта не может быть "
Expand Down
4 changes: 2 additions & 2 deletions src/backend/api/v1/projects/permissions.py
Original file line number Diff line number Diff line change
Expand Up @@ -38,7 +38,7 @@ def has_object_permission(self, request, view, obj) -> bool:
return bool(
request.user == obj.user
and (
obj.status == RequestStatuses.IN_PROGRESS
obj.request_status == RequestStatuses.IN_PROGRESS
or request.method == "DELETE"
)
or (
Expand Down Expand Up @@ -66,7 +66,7 @@ class IsProjectCreatorOrOwnerForParticipationRequest(IsProjectCreatorOrOwner):

def has_object_permission(self, request, view, obj) -> bool:
return bool(
obj.status
obj.request_status
not in (
RequestStatuses.ACCEPTED,
RequestStatuses.REJECTED,
Expand Down
Loading

0 comments on commit 02770f3

Please sign in to comment.