Skip to content

Commit

Permalink
Merge pull request #382 from Pet-projects-CodePET/add/filter-for-profile
Browse files Browse the repository at this point in the history
add filter for username and name fields
  • Loading branch information
Denis-Shtanskiy authored Oct 28, 2024
2 parents d92fc66 + a45a0ca commit f29e7ff
Show file tree
Hide file tree
Showing 5 changed files with 44 additions and 7 deletions.
25 changes: 25 additions & 0 deletions src/backend/api/v1/profile/filters.py
Original file line number Diff line number Diff line change
@@ -1,9 +1,12 @@
from django.contrib.postgres.search import TrigramSimilarity
from django_filters.rest_framework import FilterSet, filters

from apps.general.constants import LEVEL_CHOICES
from apps.general.models import Profession, Skill
from apps.profile.models import Profile

SIMILARITY_CF: float = 0.05


class ProfileFilter(FilterSet):
"""Класс фильтрации профилей специалистов."""
Expand All @@ -19,6 +22,7 @@ class ProfileFilter(FilterSet):
field_name="specialists__skills", queryset=Skill.objects.all()
)
is_favorite = filters.NumberFilter(method="filter_is_favorite_profile")
user_search = filters.CharFilter(method="user_filter_search")

class Meta:
model = Profile
Expand All @@ -28,10 +32,31 @@ class Meta:
"specialization",
"skills",
"is_favorite",
"user_search",
)

def filter_is_favorite_profile(self, queryset, name, value):
user = self.request.user
if user.is_authenticated and value == 1:
return queryset.filter(favorited_by=user)
return queryset

def user_filter_search(self, queryset, name, value):
if value and len(value) > 2:
trigram_similarity = TrigramSimilarity(
"name", value
) + TrigramSimilarity("user__username", value)

annotated_queryset = queryset.annotate(
similarity=trigram_similarity
)

# Нужно поиграться со значением similarity__gte,
# это совпадение в процентах, а не в символах
# GPT рекомендует 0.1.
# Тестово оставил 0.05 для удобства тестирования на небольшой базе
return annotated_queryset.filter(
similarity__gte=SIMILARITY_CF
).order_by("-similarity")

return queryset
5 changes: 1 addition & 4 deletions src/backend/api/v1/projects/serializers.py
Original file line number Diff line number Diff line change
Expand Up @@ -382,10 +382,7 @@ def validate_project(self, value):
)
.first()
)
if (
user == project.creator
or user == project.owner
):
if user == project.creator or user == project.owner:
raise serializers.ValidationError(
"Вы не можете создать заявку на участие в проекте, в котором "
"уже участвуете."
Expand Down
6 changes: 3 additions & 3 deletions src/backend/api/v1/projects/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -303,9 +303,9 @@ def get_queryset(self) -> QuerySet["ParticipationRequest"]:
status_filter = self.request.query_params.get("status")
if status_filter:
queryset = queryset.filter(status=status_filter)
return queryset.filter(
Q(user=self.request.user)
).only(*PROJECT_PARTICIPATION_REQUEST_ONLY_FIELDS.get(self.action, ()))
return queryset.filter(Q(user=self.request.user)).only(
*PROJECT_PARTICIPATION_REQUEST_ONLY_FIELDS.get(self.action, ())
)

def get_object(self) -> ParticipationRequest:
"""Метод получения объекта запроса на участие в проекте."""
Expand Down
14 changes: 14 additions & 0 deletions src/backend/apps/profile/migrations/0010_add_trigramextension.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# Generated by Badmajor 5.0.1 on 2024-10-24 23:55

from django.contrib.postgres.operations import TrigramExtension
from django.db import migrations


class Migration(migrations.Migration):
dependencies = [
("profile", "0009_alter_profile_about"),
]

operations = [
TrigramExtension(),
]
1 change: 1 addition & 0 deletions src/backend/config/settings/base.py
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
"django.contrib.sessions",
"django.contrib.messages",
"django.contrib.staticfiles",
"django.contrib.postgres",
]

THIRD_PARTY_APPS = [
Expand Down

0 comments on commit f29e7ff

Please sign in to comment.