Debian Codegraph @

django_rq/settings.py

raw
1 from operator import itemgetter
2 from typing import Any, cast
3
4 from django.conf import settings
5 from django.core.exceptions import ImproperlyConfigured
6
7 SHOW_ADMIN_LINK = getattr(settings, 'RQ_SHOW_ADMIN_LINK', True)
8
9 NAME = getattr(settings, 'RQ_NAME', 'default')
10 BURST: bool = getattr(settings, 'RQ_BURST', False)
11
12
13 def get_queues_list() -> list[dict[str, Any]]:
14 """
15 Build QUEUES_LIST from current RQ_QUEUES setting.
16
17 Returns a list of queue configurations sorted by name.
18 This ensures deterministic ordering across all calls.
19 """
20 queues = getattr(settings, 'RQ_QUEUES', {})
21 return [{'name': queue_name, 'connection_config': config} for queue_name, config in sorted(queues.items())]
22
23
24 def get_queues_map() -> dict[str, int]:
25 """
26 Build QUEUES_MAP from current RQ_QUEUES setting.
27
28 Returns a dict mapping queue name to its index in QUEUES_LIST.
29 """
30 queues_list = get_queues_list()
31 return {q['name']: idx for idx, q in enumerate(queues_list)}
32
33
34 def __getattr__(name):
35 """
36 Provide dynamic access to QUEUES, QUEUES_LIST and QUEUES_MAP.
37
38 This ensures settings changes (e.g., via override_settings in tests) are
39 respected, and maintains backwards compatibility.
40 """
41 if name == 'QUEUES':
42 queues = getattr(settings, 'RQ_QUEUES', None)
43 if queues is None:
44 raise ImproperlyConfigured("You have to define RQ_QUEUES in settings.py")
45 return cast(dict[str, Any], queues)
46 elif name == 'QUEUES_LIST':
47 import warnings
48
49 warnings.warn(
50 "Importing QUEUES_LIST directly is deprecated. Use get_queues_list() instead.",
51 DeprecationWarning,
52 stacklevel=2,
53 )
54 return get_queues_list()
55 elif name == 'QUEUES_MAP':
56 import warnings
57
58 warnings.warn(
59 "Importing QUEUES_MAP directly is deprecated. Use get_queues_map() instead.",
60 DeprecationWarning,
61 stacklevel=2,
62 )
63 return get_queues_map()
64 raise AttributeError(f"module {__name__!r} has no attribute {name!r}")
65
66
67 # Get exception handlers
68 EXCEPTION_HANDLERS: list[str] = getattr(settings, 'RQ_EXCEPTION_HANDLERS', [])
69
70
71 def get_api_token() -> str:
72 """Return the API token from Django settings."""
73 return getattr(settings, 'RQ_API_TOKEN', '')