1
- from django .core .management .base import BaseCommand , CommandError
1
+ from django .core .management .base import BaseCommand
2
2
from dynamic_preferences .exceptions import NotFoundInRegistry
3
3
from dynamic_preferences .models import GlobalPreferenceModel
4
- from dynamic_preferences .registries import preference_models , global_preferences_registry
5
-
6
- import logging
7
- logger = logging .getLogger (__name__ )
4
+ from dynamic_preferences .registries import (global_preferences_registry ,
5
+ preference_models )
8
6
9
7
10
8
def delete_preferences (queryset ):
11
9
"""
12
- Delete preferences objects if they are not present in registry. Return a list of deleted objects
10
+ Delete preferences objects if they are not present in registry.
11
+ Return a list of deleted objects
13
12
"""
14
13
deleted = []
15
14
16
- # Iterate through preferences. If an error is raised when accessing preference object, just delete it
15
+ # Iterate through preferences. If an error is raised when accessing
16
+ # preference object, just delete it
17
17
for p in queryset :
18
18
try :
19
- pref = p .registry .get (section = p .section , name = p .name , fallback = False )
19
+ p .registry .get (
20
+ section = p .section , name = p .name , fallback = False )
20
21
except NotFoundInRegistry :
21
22
p .delete ()
22
23
deleted .append (p )
@@ -25,26 +26,42 @@ def delete_preferences(queryset):
25
26
26
27
27
28
class Command (BaseCommand ):
28
- help = "Find and delete preferences from database if they don't exist in registries. Create preferences that are " \
29
- "not present in database"
29
+ help = (
30
+ "Find and delete preferences from database if they don't exist in "
31
+ "registries. Create preferences that are not present in database"
32
+ )
30
33
31
34
def handle (self , * args , ** options ):
32
35
33
36
# Create needed preferences
34
37
# Global
35
- logger . info ( ' Creating missing global preferences...' )
38
+ self . stdout . write ( " Creating missing global preferences..." )
36
39
manager = global_preferences_registry .manager ()
37
40
manager .all ()
38
41
39
42
deleted = delete_preferences (GlobalPreferenceModel .objects .all ())
40
- logger .info ("Deleted {0} global preferences" .format (len (deleted )))
43
+ message = "Deleted {deleted} global preferences" .format (
44
+ deleted = len (deleted )
45
+ )
46
+ self .stdout .write (message )
41
47
42
48
for preference_model , registry in preference_models .items ():
43
49
deleted = delete_preferences (preference_model .objects .all ())
44
- logger .info ("Deleted {0} {1} preferences" .format (len (deleted ), preference_model .__name__ ))
50
+ message = (
51
+ "Deleted {deleted} {model} preferences" .format (
52
+ deleted = len (deleted ),
53
+ model = preference_model .__name__ ,
54
+ )
55
+ )
56
+ self .stdout .write (message )
45
57
if not hasattr (preference_model , 'get_instance_model' ):
46
58
continue
47
59
48
- logger .info ('Creating missing preferences for {0} model...' .format (preference_model .get_instance_model ().__name__ ))
60
+ message = (
61
+ "Creating missing preferences for {model} model..." .format (
62
+ model = preference_model .get_instance_model ().__name__ ,
63
+ )
64
+ )
65
+ self .stdout .write (message )
49
66
for instance in preference_model .get_instance_model ().objects .all ():
50
67
instance .preferences .all ()
0 commit comments