1
1
import os
2
- from django .contrib .sites .models import Site
3
- from django .core .management .base import CommandError , BaseCommand
4
- from django .template .utils import get_app_template_dirs
5
- from django .template .loader import _engine_list
6
2
7
3
from dbtemplates .models import Template
4
+ from django .contrib .sites .models import Site
5
+ from django .core .management .base import BaseCommand , CommandError
6
+ from django .template .loader import _engine_list
7
+ from django .template .utils import get_app_template_dirs
8
8
9
- ALWAYS_ASK , FILES_TO_DATABASE , DATABASE_TO_FILES = ('0' , '1' , '2' )
9
+ ALWAYS_ASK , FILES_TO_DATABASE , DATABASE_TO_FILES = ("0" , "1" , "2" )
10
10
11
11
DIRS = []
12
12
for engine in _engine_list ():
13
13
DIRS .extend (engine .dirs )
14
14
DIRS = tuple (DIRS )
15
- app_template_dirs = get_app_template_dirs (' templates' )
15
+ app_template_dirs = get_app_template_dirs (" templates" )
16
16
17
17
18
18
class Command (BaseCommand ):
19
19
help = "Syncs file system templates with the database bidirectionally."
20
20
21
21
def add_arguments (self , parser ):
22
22
parser .add_argument (
23
- "-e" , "--ext" ,
24
- dest = "ext" , action = "store" , default = "html" ,
23
+ "-e" ,
24
+ "--ext" ,
25
+ dest = "ext" ,
26
+ action = "store" ,
27
+ default = "html" ,
25
28
help = "extension of the files you want to "
26
- "sync with the database [default: %(default)s]" )
29
+ "sync with the database [default: %(default)s]" ,
30
+ )
27
31
parser .add_argument (
28
- "-f" , "--force" ,
29
- action = "store_true" , dest = "force" , default = False ,
30
- help = "overwrite existing database templates" )
32
+ "-f" ,
33
+ "--force" ,
34
+ action = "store_true" ,
35
+ dest = "force" ,
36
+ default = False ,
37
+ help = "overwrite existing database templates" ,
38
+ )
31
39
parser .add_argument (
32
- "-o" , "--overwrite" ,
33
- action = "store" , dest = "overwrite" , default = '0' ,
40
+ "-o" ,
41
+ "--overwrite" ,
42
+ action = "store" ,
43
+ dest = "overwrite" ,
44
+ default = "0" ,
34
45
help = "'0' - ask always, '1' - overwrite database "
35
- "templates from template files, '2' - overwrite "
36
- "template files from database templates" )
46
+ "templates from template files, '2' - overwrite "
47
+ "template files from database templates" ,
48
+ )
37
49
parser .add_argument (
38
- "-a" , "--app-first" ,
39
- action = "store_true" , dest = "app_first" , default = False ,
50
+ "-a" ,
51
+ "--app-first" ,
52
+ action = "store_true" ,
53
+ dest = "app_first" ,
54
+ default = False ,
40
55
help = "look for templates in applications "
41
- "directories before project templates" )
56
+ "directories before project templates" ,
57
+ )
42
58
parser .add_argument (
43
- "-d" , "--delete" ,
44
- action = "store_true" , dest = "delete" , default = False ,
45
- help = "Delete templates after syncing" )
59
+ "-d" ,
60
+ "--delete" ,
61
+ action = "store_true" ,
62
+ dest = "delete" ,
63
+ default = False ,
64
+ help = "Delete templates after syncing" ,
65
+ )
46
66
47
67
def handle (self , ** options ):
48
- extension = options .get (' ext' )
49
- force = options .get (' force' )
50
- overwrite = options .get (' overwrite' )
51
- app_first = options .get (' app_first' )
52
- delete = options .get (' delete' )
68
+ extension = options .get (" ext" )
69
+ force = options .get (" force" )
70
+ overwrite = options .get (" overwrite" )
71
+ app_first = options .get (" app_first" )
72
+ delete = options .get (" delete" )
53
73
54
74
if not extension .startswith ("." ):
55
75
extension = f".{ extension } "
56
76
57
77
try :
58
78
site = Site .objects .get_current ()
59
79
except Exception :
60
- raise CommandError ("Please make sure to have the sites contrib "
61
- "app installed and setup with a site object" )
80
+ raise CommandError (
81
+ "Please make sure to have the sites contrib "
82
+ "app installed and setup with a site object"
83
+ )
62
84
63
85
if app_first :
64
86
tpl_dirs = app_template_dirs + DIRS
@@ -68,11 +90,14 @@ def handle(self, **options):
68
90
69
91
for templatedir in templatedirs :
70
92
for dirpath , subdirs , filenames in os .walk (templatedir ):
71
- for f in [f for f in filenames
72
- if f .endswith (extension ) and not f .startswith ("." )]:
93
+ for f in [
94
+ f
95
+ for f in filenames
96
+ if f .endswith (extension ) and not f .startswith ("." )
97
+ ]:
73
98
path = os .path .join (dirpath , f )
74
99
name = path .split (str (templatedir ))[1 ]
75
- if name .startswith ('/' ):
100
+ if name .startswith ("/" ):
76
101
name = name [1 :]
77
102
try :
78
103
t = Template .on_site .get (name__exact = name )
@@ -81,27 +106,35 @@ def handle(self, **options):
81
106
confirm = input (
82
107
"\n A '%s' template doesn't exist in the "
83
108
"database.\n Create it with '%s'?"
84
- " (y/[n]): " "" % (name , path ))
85
- if force or confirm .lower ().startswith ('y' ):
86
- with open (path , encoding = 'utf-8' ) as f :
109
+ " (y/[n]): "
110
+ "" % (name , path )
111
+ )
112
+ if force or confirm .lower ().startswith ("y" ):
113
+ with open (path , encoding = "utf-8" ) as f :
87
114
t = Template (name = name , content = f .read ())
88
115
t .save ()
89
116
t .sites .add (site )
90
117
else :
91
- while 1 :
118
+ while True :
92
119
if overwrite == ALWAYS_ASK :
93
- confirm = input (
120
+ _i = (
94
121
"\n %(template)s exists in the database.\n "
95
- "(1) Overwrite %(template)s with '%(path)s'\n "
96
- "(2) Overwrite '%(path)s' with %(template)s\n "
97
- "Type 1 or 2 or press <Enter> to skip: " %
98
- {'template' : t .__repr__ (), 'path' : path })
122
+ "(1) Overwrite %(template)s with '%(path)s'\n " # noqa
123
+ "(2) Overwrite '%(path)s' with %(template)s\n " # noqa
124
+ "Type 1 or 2 or press <Enter> to skip: "
125
+ % {"template" : t .__repr__ (), "path" : path }
126
+ )
127
+
128
+ confirm = input (_i )
99
129
else :
100
130
confirm = overwrite
101
- if confirm in ('' , FILES_TO_DATABASE ,
102
- DATABASE_TO_FILES ):
131
+ if confirm in (
132
+ "" ,
133
+ FILES_TO_DATABASE ,
134
+ DATABASE_TO_FILES ,
135
+ ):
103
136
if confirm == FILES_TO_DATABASE :
104
- with open (path , encoding = ' utf-8' ) as f :
137
+ with open (path , encoding = " utf-8" ) as f :
105
138
t .content = f .read ()
106
139
t .save ()
107
140
t .sites .add (site )
@@ -110,9 +143,10 @@ def handle(self, **options):
110
143
os .remove (path )
111
144
except OSError :
112
145
raise CommandError (
113
- f"Couldn't delete { path } " )
146
+ f"Couldn't delete { path } "
147
+ )
114
148
elif confirm == DATABASE_TO_FILES :
115
- with open (path , 'w' , encoding = ' utf-8' ) as f :
149
+ with open (path , "w" , encoding = " utf-8" ) as f : # noqa
116
150
f .write (t .content )
117
151
if delete :
118
152
t .delete ()
0 commit comments