From bff1e0c378e7b5d6a7899054439289a3c6d8d415 Mon Sep 17 00:00:00 2001 From: shoxxd221 Date: Sun, 3 Mar 2024 15:42:23 +0300 Subject: [PATCH 1/2] Added filters for projects --- src/backend/api/v1/projects/filters.py | 29 ++++++++++++++++++++++++++ src/backend/api/v1/projects/views.py | 5 ++++- src/backend/apps/project/models.py | 11 ++++++++++ 3 files changed, 44 insertions(+), 1 deletion(-) create mode 100644 src/backend/api/v1/projects/filters.py diff --git a/src/backend/api/v1/projects/filters.py b/src/backend/api/v1/projects/filters.py new file mode 100644 index 0000000..c0dcd2b --- /dev/null +++ b/src/backend/api/v1/projects/filters.py @@ -0,0 +1,29 @@ +from django_filters.rest_framework import FilterSet +from django_filters import filters + +from apps.general.constants import LEVEL_CHOICES +from apps.general.models import Skill +from apps.project.constants import BUSYNESS_CHOICES, STATUS_CHOICES, DIRECTION_CHOICES +from apps.project.models import Project + + +class ProjectFilter(FilterSet): + """ + Класс для фильтрации проектов по имени, дате начала, дате конца, занятости в часах в неделю, статусу набора + участников, статусу проекта, направлению разработки, навыкам и уровню + """ + name = filters.CharFilter(lookup_expr="icontains") + started = filters.DateFromToRangeFilter() + ended = filters.DateFromToRangeFilter() + busyness = filters.MultipleChoiceFilter(choices=BUSYNESS_CHOICES) + recruitment_status = filters.BooleanFilter() + status = filters.MultipleChoiceFilter(choices=STATUS_CHOICES) + direction = filters.MultipleChoiceFilter(choices=DIRECTION_CHOICES) + skill = filters.ModelMultipleChoiceFilter(queryset=Skill.objects.all()) + level = filters.MultipleChoiceFilter(choices=LEVEL_CHOICES) + + class Meta: + model = Project + fields = ( + "name", "started", "ended", "busyness", "recruitment_status", "status", "direction", "skill", "level" + ) diff --git a/src/backend/api/v1/projects/views.py b/src/backend/api/v1/projects/views.py index c20ef8c..86dfb34 100644 --- a/src/backend/api/v1/projects/views.py +++ b/src/backend/api/v1/projects/views.py @@ -1,11 +1,12 @@ from django.db.models import Prefetch +from django_filters.rest_framework import DjangoFilterBackend from rest_framework import mixins from rest_framework.permissions import AllowAny, IsAuthenticatedOrReadOnly from rest_framework.viewsets import GenericViewSet, ModelViewSet -from apps.general.models import Skill from apps.project.models import Project, ProjectSpecialist +from .filters import ProjectFilter from .constants import PROJECT_PREVIEW_MAIN_FIELDS from .paginations import ProjectPagination, ProjectPreviewMainPagination from .serializers import ProjectPreviewMainSerializer, ProjectSerializer @@ -29,6 +30,8 @@ class ProjectViewSet(ModelViewSet): permission_classes = (IsAuthenticatedOrReadOnly,) serializer_class = ProjectSerializer pagination_class = ProjectPagination + filter_backends = (DjangoFilterBackend,) + filterset_class = ProjectFilter class ProjectPreviewMainViewSet(mixins.ListModelMixin, GenericViewSet): diff --git a/src/backend/apps/project/models.py b/src/backend/apps/project/models.py index 10cad2a..ec1c422 100644 --- a/src/backend/apps/project/models.py +++ b/src/backend/apps/project/models.py @@ -99,6 +99,17 @@ class Project(CreatedModifiedFields): related_name="projects_participated", blank=True, ) + skills = models.ManyToManyField( + Skill, + related_name="projects", + verbose_name="Проекты" + ) + level = models.PositiveSmallIntegerField( + verbose_name="Уровень", + choices=LEVEL_CHOICES, + null=True, + blank=True + ) class Meta: verbose_name = "Проект" From f9f155b912fb1056d311c605810843d4f8bf059e Mon Sep 17 00:00:00 2001 From: shoxxd221 Date: Sun, 3 Mar 2024 15:43:17 +0300 Subject: [PATCH 2/2] Refactor code --- src/backend/api/v1/projects/filters.py | 17 +++++++++++++++-- src/backend/apps/project/models.py | 9 ++------- 2 files changed, 17 insertions(+), 9 deletions(-) diff --git a/src/backend/api/v1/projects/filters.py b/src/backend/api/v1/projects/filters.py index c0dcd2b..e99ab0b 100644 --- a/src/backend/api/v1/projects/filters.py +++ b/src/backend/api/v1/projects/filters.py @@ -3,7 +3,11 @@ from apps.general.constants import LEVEL_CHOICES from apps.general.models import Skill -from apps.project.constants import BUSYNESS_CHOICES, STATUS_CHOICES, DIRECTION_CHOICES +from apps.project.constants import ( + BUSYNESS_CHOICES, + STATUS_CHOICES, + DIRECTION_CHOICES, +) from apps.project.models import Project @@ -12,6 +16,7 @@ class ProjectFilter(FilterSet): Класс для фильтрации проектов по имени, дате начала, дате конца, занятости в часах в неделю, статусу набора участников, статусу проекта, направлению разработки, навыкам и уровню """ + name = filters.CharFilter(lookup_expr="icontains") started = filters.DateFromToRangeFilter() ended = filters.DateFromToRangeFilter() @@ -25,5 +30,13 @@ class ProjectFilter(FilterSet): class Meta: model = Project fields = ( - "name", "started", "ended", "busyness", "recruitment_status", "status", "direction", "skill", "level" + "name", + "started", + "ended", + "busyness", + "recruitment_status", + "status", + "direction", + "skill", + "level", ) diff --git a/src/backend/apps/project/models.py b/src/backend/apps/project/models.py index ec1c422..a48544b 100644 --- a/src/backend/apps/project/models.py +++ b/src/backend/apps/project/models.py @@ -100,15 +100,10 @@ class Project(CreatedModifiedFields): blank=True, ) skills = models.ManyToManyField( - Skill, - related_name="projects", - verbose_name="Проекты" + Skill, related_name="projects", verbose_name="Проекты" ) level = models.PositiveSmallIntegerField( - verbose_name="Уровень", - choices=LEVEL_CHOICES, - null=True, - blank=True + verbose_name="Уровень", choices=LEVEL_CHOICES, null=True, blank=True ) class Meta: