Coverage for src/django_global_search/admin.py: 100%

1"""Global Search Admin."""

2

3 from django.contrib import admin

4 from django.urls import path

5

6 from django_global_search.settings import GlobalSearchAdminSiteSettings

7

8

9 class GlobalSearchAdminSiteMixin:

10 """Global Search Admin Site Mixin."""

11

12 def get_global_search_settings(self):

13 """Get Global Search Settings."""

14 return GlobalSearchAdminSiteSettings.from_admin_site(self)

15

16 def get_urls(self):

17 """Get admin URLs with global search."""

18 from django_global_search.views import GlobalSearchView

19

20 urls = super().get_urls()

21

22 custom_urls = [

23 path(

24 "global-search/",

25 self.admin_view(GlobalSearchView.as_view(admin_site=self)),

26 name="global_search",

27 ),

28 ]

29 return custom_urls + urls

30

31

32 def inject_default_admin_site():

33 """Inject GlobalSearchAdminSiteMixin into default AdminSite."""

34 # Check if AdminSite already has the mixin

35 if issubclass(admin.site.__class__, GlobalSearchAdminSiteMixin):

36 return

37

38 # Create a new class based on the current class

39 current_class = admin.site.__class__

40 new_class = type(

41 f"{current_class.__name__}WithGlobalSearch",

42 (GlobalSearchAdminSiteMixin, current_class),

43 {},

44 )

45

46 # Replace admin.site.__class__ with the new class

47 admin.site.__class__ = new_class