File tree Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Expand file tree Collapse file tree 2 files changed +50
-1
lines changed Original file line number Diff line number Diff line change @@ -20,3 +20,26 @@ def get_values():
20
20
default_initial = ((name , options [0 ]) for name , options in settings .CONFIG .items ())
21
21
# Then update the mapping with actually values from the backend
22
22
return dict (default_initial , ** dict (config ._backend .mget (settings .CONFIG )))
23
+
24
+
25
+ def get_values_for_keys (keys ):
26
+ """
27
+ Retrieve values for specified keys from the backend.
28
+
29
+ :param keys: List of keys to retrieve.
30
+ :return: Dictionary with values for the specified keys.
31
+ :raises AttributeError: If any key is not found in the configuration.
32
+ """
33
+ if not isinstance (keys , (list , tuple , set )):
34
+ raise TypeError ('keys must be a list, tuple, or set of strings' )
35
+
36
+ # Prepare default initial mapping
37
+ default_initial = {name : options [0 ] for name , options in settings .CONFIG .items () if name in keys }
38
+
39
+ # Check if all keys are present in the default_initial mapping
40
+ missing_keys = [key for key in keys if key not in default_initial ]
41
+ if missing_keys :
42
+ raise AttributeError (f'"{ ", " .join (missing_keys )} " keys not found in configuration.' )
43
+
44
+ # Merge default values and backend values, prioritizing backend values
45
+ return dict (default_initial , ** dict (config ._backend .mget (keys )))
Original file line number Diff line number Diff line change 5
5
from django .test import TestCase
6
6
7
7
from constance .management .commands .constance import _set_constance_value
8
- from constance .utils import get_values
8
+ from constance .utils import get_values , get_values_for_keys
9
9
10
10
11
11
class UtilsTestCase (TestCase ):
@@ -62,3 +62,29 @@ def test_get_values(self):
62
62
},
63
63
},
64
64
)
65
+
66
+ def test_get_values_for_keys (self ):
67
+ self .assertEqual (
68
+ get_values_for_keys (['BOOL_VALUE' , 'CHOICE_VALUE' , 'LINEBREAK_VALUE' ]),
69
+ {
70
+ 'BOOL_VALUE' : True ,
71
+ 'CHOICE_VALUE' : 'yes' ,
72
+ 'LINEBREAK_VALUE' : 'Spam spam' ,
73
+ },
74
+ )
75
+
76
+ def test_get_values_for_keys_empty_keys (self ):
77
+ result = get_values_for_keys ([])
78
+ self .assertEqual (result , {})
79
+
80
+ def test_get_values_for_keys_throw_error_if_no_key (self ):
81
+ self .assertRaisesMessage (
82
+ AttributeError ,
83
+ '"OLD_VALUE, BOLD_VALUE" keys not found in configuration.' ,
84
+ get_values_for_keys ,
85
+ ['BOOL_VALUE' , 'OLD_VALUE' , 'BOLD_VALUE' ],
86
+ )
87
+
88
+ def test_get_values_for_keys_invalid_input_type (self ):
89
+ with self .assertRaises (TypeError ):
90
+ get_values_for_keys ('key1' )
You can’t perform that action at this time.
0 commit comments