From 624d6e82e09f9e417f9adfe643b1b94c52a83daa Mon Sep 17 00:00:00 2001 From: Andrewakiv Date: Thu, 19 Dec 2024 16:13:37 +0200 Subject: [PATCH] added name and is_inactive parameters --- BackEnd/administration/filters.py | 17 ++++++++++++++--- BackEnd/administration/serializers.py | 1 + BackEnd/administration/views.py | 6 ++---- 3 files changed, 17 insertions(+), 7 deletions(-) diff --git a/BackEnd/administration/filters.py b/BackEnd/administration/filters.py index a025bca6..84cb1ec5 100644 --- a/BackEnd/administration/filters.py +++ b/BackEnd/administration/filters.py @@ -1,3 +1,4 @@ +from django.db.models import Q from django_filters import filters from django_filters.rest_framework import FilterSet @@ -5,16 +6,18 @@ class UsersFilter(FilterSet): """ Filters - /?id= , /?surname=, /?email= , /?is_active= , /?is_staff=, - /?is_superuser=, /?is_deleted=True or False, /?company_name=, /?registration_date=, + /?id= , ,/?name= /?surname=, /?email= , /?is_active= , /?is_staff=, + /?is_superuser=, /?is_deleted=True or False, /?company_name=, + /?registration_date=, Ordering sample /?ordering=id asc or /?ordering=-id desc """ - id = filters.NumberFilter(lookup_expr="contains") + name = filters.CharFilter(lookup_expr="icontains") surname = filters.CharFilter(lookup_expr="icontains") email = filters.CharFilter(lookup_expr="icontains") is_active = filters.BooleanFilter() + is_inactive = filters.BooleanFilter(method="is_inactive_filter") is_staff = filters.BooleanFilter() is_superuser = filters.BooleanFilter() is_deleted = filters.BooleanFilter(method="is_deleted_filter") @@ -25,6 +28,14 @@ class UsersFilter(FilterSet): field_name="profile__created_at", lookup_expr="icontains" ) + def is_inactive_filter(self, queryset, name, value): + if value: + queryset = queryset.filter( + ~Q(email__startswith="is_deleted_"), + is_active=False + ) + return queryset + def is_deleted_filter(self, queryset, name, value): if value: queryset = queryset.filter(email__startswith="is_deleted_") diff --git a/BackEnd/administration/serializers.py b/BackEnd/administration/serializers.py index ca0c6a8b..c9471fed 100644 --- a/BackEnd/administration/serializers.py +++ b/BackEnd/administration/serializers.py @@ -82,6 +82,7 @@ def get_status(self, obj) -> dict: "is_staff": obj.is_staff, "is_superuser": obj.is_superuser, "is_deleted": obj.email.startswith("is_deleted_"), + "is_inactive": not obj.is_active and not obj.email.startswith("is_deleted_"), } return data diff --git a/BackEnd/administration/views.py b/BackEnd/administration/views.py index 6b246324..b19c6d24 100644 --- a/BackEnd/administration/views.py +++ b/BackEnd/administration/views.py @@ -38,10 +38,8 @@ from .serializers import FeedbackSerializer from utils.administration.send_email_feedback import send_email_feedback -from django_filters.rest_framework import DjangoFilterBackend from .filters import UsersFilter, CategoriesFilter from utils.administration.send_email_notification import send_email_to_user -from .filters import UsersFilter class UsersListView(ListAPIView): @@ -49,8 +47,8 @@ class UsersListView(ListAPIView): View to list users with optional filtering and ordering. ### Query Parameters: - - **id** / **surname** / **email** / **is_active** / **is_staff** / **is_superuser** / **is_deleted** - - **company_name** / **registration_date** + - **name** / **surname** /**email** / **is_active** / **is_staff** / + - **is_superuser** / **is_deleted**/ **company_name** / **registration_date** ### Ordering: - Use the `ordering` parameter to sort the results.