✨(backend) allow filtering by documents marked as favorite
We recently allowed authenticated users to mark a document as favorite. We were lacking the possibility for users to see only the documents they marked as favorite.
This commit is contained in:
committed by
Anthony LC
parent
23f90156bf
commit
08bb64ddc1
@@ -15,10 +15,13 @@ class DocumentFilter(django_filters.FilterSet):
|
|||||||
is_creator_me = django_filters.BooleanFilter(
|
is_creator_me = django_filters.BooleanFilter(
|
||||||
method="filter_is_creator_me", label=_("Creator is me")
|
method="filter_is_creator_me", label=_("Creator is me")
|
||||||
)
|
)
|
||||||
|
is_favorite = django_filters.BooleanFilter(
|
||||||
|
method="filter_is_favorite", label=_("Favorite")
|
||||||
|
)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
model = models.Document
|
model = models.Document
|
||||||
fields = ["is_creator_me"]
|
fields = ["is_creator_me", "is_favorite"]
|
||||||
|
|
||||||
# pylint: disable=unused-argument
|
# pylint: disable=unused-argument
|
||||||
def filter_is_creator_me(self, queryset, name, value):
|
def filter_is_creator_me(self, queryset, name, value):
|
||||||
@@ -47,9 +50,9 @@ class DocumentFilter(django_filters.FilterSet):
|
|||||||
Filter documents based on whether they are marked as favorite by the current user.
|
Filter documents based on whether they are marked as favorite by the current user.
|
||||||
|
|
||||||
Example:
|
Example:
|
||||||
- /api/v1.0/documents/?favorite=true
|
- /api/v1.0/documents/?is_favorite=true
|
||||||
→ Filters documents marked as favorite by the logged-in user
|
→ Filters documents marked as favorite by the logged-in user
|
||||||
- /api/v1.0/documents/?favorite=false
|
- /api/v1.0/documents/?is_favorite=false
|
||||||
→ Filters documents not marked as favorite by the logged-in user
|
→ Filters documents not marked as favorite by the logged-in user
|
||||||
"""
|
"""
|
||||||
user = self.request.user
|
user = self.request.user
|
||||||
@@ -57,7 +60,7 @@ class DocumentFilter(django_filters.FilterSet):
|
|||||||
if not user.is_authenticated:
|
if not user.is_authenticated:
|
||||||
return queryset
|
return queryset
|
||||||
|
|
||||||
clause = "filter" if value else "exclude"
|
if value:
|
||||||
return getattr(queryset, clause)(
|
return queryset.filter(favorited_by_users__user=user)
|
||||||
favorited_by_users__user=user, favorited_by_users__is_favorite=True
|
|
||||||
)
|
return queryset.exclude(favorited_by_users__user=user)
|
||||||
|
|||||||
@@ -324,10 +324,12 @@ class DocumentViewSet(
|
|||||||
Filtering:
|
Filtering:
|
||||||
- `is_creator_me=true`: Returns documents created by the current user.
|
- `is_creator_me=true`: Returns documents created by the current user.
|
||||||
- `is_creator_me=false`: Returns documents created by other users.
|
- `is_creator_me=false`: Returns documents created by other users.
|
||||||
|
- `is_favorite=true`: Returns documents marked as favorite by the current user
|
||||||
|
- `is_favorite=false`: Returns documents not marked as favorite by the current user
|
||||||
|
|
||||||
Example Usage:
|
Example Usage:
|
||||||
- GET /api/v1.0/documents/?creator=me
|
- GET /api/v1.0/documents/?is_creator_me=true&is_favorite=true
|
||||||
- GET /api/v1.0/documents/?creator=other
|
- GET /api/v1.0/documents/?is_creator_me=false
|
||||||
"""
|
"""
|
||||||
|
|
||||||
filter_backends = [filters.DjangoFilterBackend, drf_filters.OrderingFilter]
|
filter_backends = [filters.DjangoFilterBackend, drf_filters.OrderingFilter]
|
||||||
|
|||||||
@@ -433,7 +433,6 @@ def test_api_documents_list_filter_is_creator_me_invalid():
|
|||||||
results = response.json()["results"]
|
results = response.json()["results"]
|
||||||
assert len(results) == 5
|
assert len(results) == 5
|
||||||
|
|
||||||
|
|
||||||
# Filters: is_favorite
|
# Filters: is_favorite
|
||||||
|
|
||||||
|
|
||||||
@@ -496,7 +495,6 @@ def test_api_documents_list_filter_is_favorite_invalid():
|
|||||||
results = response.json()["results"]
|
results = response.json()["results"]
|
||||||
assert len(results) == 5
|
assert len(results) == 5
|
||||||
|
|
||||||
|
|
||||||
# Filters: link_reach
|
# Filters: link_reach
|
||||||
|
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user