Skip to content

Commit

Permalink
fix(Besoins): Admin : filtre vraiment la liste des utilisateurs sur l…
Browse files Browse the repository at this point in the history
…es bizdev (#1242)
  • Loading branch information
raphodn authored Jun 5, 2024
1 parent 20ee3d1 commit 8a46774
Show file tree
Hide file tree
Showing 2 changed files with 15 additions and 5 deletions.
5 changes: 0 additions & 5 deletions lemarche/tenders/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
15 changes: 15 additions & 0 deletions lemarche/users/admin.py
Original file line number Diff line number Diff line change
@@ -1,3 +1,5 @@
import re

from ckeditor.widgets import CKEditorWidget
from django.contrib import admin
from django.contrib.auth.admin import UserAdmin
Expand Down Expand Up @@ -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
Expand Down

0 comments on commit 8a46774

Please sign in to comment.