From 2afa2d4f5c8d74455dfabd2bf07335d72e2259c2 Mon Sep 17 00:00:00 2001 From: Polawat Phetra Date: Mon, 29 Jan 2024 11:56:36 +0700 Subject: [PATCH] #57 Update user deletion logic and filter active users in queries --- .../mutations/admin_authority_user_mutations.py | 3 ++- accounts/schema/types.py | 3 ++- summaries/views.py | 13 ++++++++++--- 3 files changed, 14 insertions(+), 5 deletions(-) diff --git a/accounts/schema/mutations/admin_authority_user_mutations.py b/accounts/schema/mutations/admin_authority_user_mutations.py index ae91057..60c30f1 100644 --- a/accounts/schema/mutations/admin_authority_user_mutations.py +++ b/accounts/schema/mutations/admin_authority_user_mutations.py @@ -278,5 +278,6 @@ def mutate(root, info, id): else: raise GraphQLError("Permission denied.") - delete_user.delete() + delete_user.is_active = False + delete_user.save(update_fields=("is_active",)) return {"success": True} diff --git a/accounts/schema/types.py b/accounts/schema/types.py index 60c9632..bba4ebd 100644 --- a/accounts/schema/types.py +++ b/accounts/schema/types.py @@ -184,7 +184,8 @@ def get_queryset(cls, queryset, info): ) elif user.is_authority_role_in([AuthorityUser.Role.REPORTER]): raise GraphQLError("Permission denied") - queryset.prefetch_related("authority") + queryset = queryset.filter(is_active=True).prefetch_related("authority") + return queryset diff --git a/summaries/views.py b/summaries/views.py index c79380e..3dea029 100644 --- a/summaries/views.py +++ b/summaries/views.py @@ -45,7 +45,11 @@ def export_inactive_reporter_xls(request): AuthorityUser.objects.exclude(id__in=exclude) .annotate(Count("username")) .order_by("username") - .filter(authority__in=sub_authorities, role=AuthorityUser.Role.REPORTER) + .filter( + authority__in=sub_authorities, + role=AuthorityUser.Role.REPORTER, + is_active=True, + ) .values("username", "first_name", "last_name", "telephone", "authority__name") ) @@ -130,7 +134,9 @@ def export_reporter_performance_xls(request): rows = ( AuthorityUser.objects.filter( - authority__in=sub_authorities, role=AuthorityUser.Role.REPORTER + authority__in=sub_authorities, + role=AuthorityUser.Role.REPORTER, + is_active=True, ) .annotate( zero_reports=Subquery(zero_reports), @@ -369,7 +375,8 @@ def export_zero_report_xls(request): .order_by("-day") .filter( reported_by__in=AuthorityUser.objects.filter( - authority__in=sub_authorities + authority__in=sub_authorities, + is_active=True, # , role=AuthorityUser.Role.REPORTER ), )