From 8a467745ba47ef64bff38f51a942fd6513c350e7 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Rapha=C3=ABl=20Odini?= Date: Wed, 5 Jun 2024 10:10:36 +0200 Subject: [PATCH] fix(Besoins): Admin : filtre vraiment la liste des utilisateurs sur les bizdev (#1242) --- lemarche/tenders/admin.py | 5 ----- lemarche/users/admin.py | 15 +++++++++++++++ 2 files changed, 15 insertions(+), 5 deletions(-) diff --git a/lemarche/tenders/admin.py b/lemarche/tenders/admin.py index 6b11130eb..1d9279fda 100644 --- a/lemarche/tenders/admin.py +++ b/lemarche/tenders/admin.py @@ -547,11 +547,6 @@ def get_readonly_fields(self, request, obj=None): readonly_fields.append("slug") return readonly_fields - def formfield_for_manytomany(self, db_field, request, **kwargs): - if db_field.name == "admins": - kwargs["queryset"] = User.objects.filter(kind=User.KIND_ADMIN) - return super().formfield_for_manytomany(db_field, request, **kwargs) - def save_model(self, request, obj: Tender, form, change): """ Set Tender author on create diff --git a/lemarche/users/admin.py b/lemarche/users/admin.py index bba91218b..ede5afe06 100644 --- a/lemarche/users/admin.py +++ b/lemarche/users/admin.py @@ -1,3 +1,5 @@ +import re + from ckeditor.widgets import CKEditorWidget from django.contrib import admin from django.contrib.auth.admin import UserAdmin @@ -328,6 +330,19 @@ def get_queryset(self, request): qs = qs.with_tender_stats() return qs + def get_search_results(self, request, queryset, search_term): + """ + We have a usecase where we want to return only admins + We need to match strings like: + - /admin/autocomplete/?app_label=tenders&model_name=tender&field_name=admins + - /admin/autocomplete/?term=raph&app_label=tenders&model_name=tender&field_name=admins + """ + queryset, use_distinct = super().get_search_results(request, queryset, search_term) + pattern = r"^\/admin\/autocomplete\/\?.*app_label=tenders&model_name=tender&field_name=admins$" + if re.search(pattern, request.get_full_path()): + queryset = queryset.filter(kind=User.KIND_ADMIN) + return queryset, use_distinct + def save_formset(self, request, form, formset, change): """ Set Note author on create