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

1"""django-global-search settings."""

2

3 from dataclasses import dataclass

4

5 from django.conf import settings

6 from django.contrib.admin.sites import AdminSite

7

8

9@dataclass(frozen=True)

10 class GlobalSearchAdminSiteSettings:

11 """Global Search admin site settings."""

12

13 min_query_length: int

14 """Minimum query length."""

15 max_results_per_model: int

16 """Maximum results per model."""

17 search_timeout_ms: int

18 """Search timeout in milliseconds."""

19 excluded_models: list[str]

20 """Excluded models.

21

22 example: ['auth.user', 'auth.group']

23 """

24

25 @classmethod

26 def from_admin_site(cls, admin_site: AdminSite):

27 """Create GlobalSearchAdminSiteSettings from AdminSite."""

28 min_query_length = getattr(settings, "GLOBAL_SEARCH_MIN_QUERY_LENGTH", 2)

29 max_results_per_model = getattr(settings, "GLOBAL_SEARCH_MAX_RESULTS_PER_MODEL", 10)

30 search_timeout_ms = getattr(settings, "GLOBAL_SEARCH_TIMEOUT_MS", 20000)

31 excluded_models = getattr(settings, "GLOBAL_SEARCH_EXCLUDED_MODELS", [])

32

33 defaults = {

34 "min_query_length": min_query_length,

35 "max_results_per_model": max_results_per_model,

36 "search_timeout_ms": search_timeout_ms,

37 "excluded_models": excluded_models,

38 }

39

40 if hasattr(admin_site, "global_search_settings"):

41 defaults.update(admin_site.global_search_settings)

42

43 return cls(**defaults)

44

45

46@dataclass(frozen=True)

47 class GlobalSearchSettings:

48 """Global Search settings."""

49

50 inject_default_admin_site_enabled: bool

51 """Inject default admin site."""

52

53 @classmethod

54 def from_settings(cls):

55 """Create GlobalSearchSettings from settings."""

56 inject_default_admin_site_enabled = getattr(

57 settings, "GLOBAL_SEARCH_INJECT_DEFAULT_ADMIN_SITE_ENABLED", True

58 )

59

60 return cls(inject_default_admin_site_enabled=inject_default_admin_site_enabled)

61

62

63 global_search_settings = GlobalSearchSettings.from_settings()