1
1
import os
2
2
3
- from django .test import override_settings
3
+ from django .conf import settings
4
+ from django .utils import six
5
+
6
+ try :
7
+ from django .test import override_settings
8
+ except ImportError :
9
+ # Django < 1.7
10
+ from django .test .utils import override_settings
4
11
5
12
6
13
def _ (path ):
@@ -11,3 +18,51 @@ def _(path):
11
18
class pipeline_settings (override_settings ):
12
19
def __init__ (self , ** kwargs ):
13
20
self .options = {'PIPELINE' : kwargs }
21
+
22
+
23
+ # Django < 1.7 (copy-pasted from Django 1.7)
24
+ class modify_settings (override_settings ):
25
+ """
26
+ Like override_settings, but makes it possible to append, prepend or remove
27
+ items instead of redefining the entire list.
28
+ """
29
+ def __init__ (self , * args , ** kwargs ):
30
+ if args :
31
+ # Hack used when instantiating from SimpleTestCase._pre_setup.
32
+ assert not kwargs
33
+ self .operations = args [0 ]
34
+ else :
35
+ assert not args
36
+ self .operations = list (kwargs .items ())
37
+
38
+ def save_options (self , test_func ):
39
+ if test_func ._modified_settings is None :
40
+ test_func ._modified_settings = self .operations
41
+ else :
42
+ # Duplicate list to prevent subclasses from altering their parent.
43
+ test_func ._modified_settings = list (
44
+ test_func ._modified_settings ) + self .operations
45
+
46
+ def enable (self ):
47
+ self .options = {}
48
+ for name , operations in self .operations :
49
+ try :
50
+ # When called from SimpleTestCase._pre_setup, values may be
51
+ # overridden several times; cumulate changes.
52
+ value = self .options [name ]
53
+ except KeyError :
54
+ value = list (getattr (settings , name , []))
55
+ for action , items in operations .items ():
56
+ # items my be a single value or an iterable.
57
+ if isinstance (items , six .string_types ):
58
+ items = [items ]
59
+ if action == 'append' :
60
+ value = value + [item for item in items if item not in value ]
61
+ elif action == 'prepend' :
62
+ value = [item for item in items if item not in value ] + value
63
+ elif action == 'remove' :
64
+ value = [item for item in value if item not in items ]
65
+ else :
66
+ raise ValueError ("Unsupported action: %s" % action )
67
+ self .options [name ] = value
68
+ super (modify_settings , self ).enable ()
0 commit comments