1
+ import io
1
2
import os
2
- import codecs
3
- from optparse import make_option
4
- from django import VERSION
5
3
from django .contrib .sites .models import Site
6
- from django .core .management .base import CommandError , NoArgsCommand
4
+ from django .core .management .base import CommandError , BaseCommand
5
+ from django .template .utils import get_app_template_dirs
6
+ from django .template .loader import _engine_list
7
7
try :
8
8
from django .utils .six import input as raw_input
9
9
except ImportError :
10
10
pass
11
11
12
- from dbtemplates .conf import settings
13
12
from dbtemplates .models import Template
14
13
15
14
ALWAYS_ASK , FILES_TO_DATABASE , DATABASE_TO_FILES = ('0' , '1' , '2' )
16
15
17
16
DIRS = []
17
+ for engine in _engine_list ():
18
+ DIRS .extend (engine .dirs )
19
+ app_template_dirs = get_app_template_dirs ('templates' )
18
20
19
- if VERSION [:2 ] < (1 , 8 ):
20
- from django .template .loaders .app_directories import app_template_dirs
21
- DIRS = settings .TEMPLATE_DIRS
22
- else :
23
- from django .template .utils import get_app_template_dirs
24
- from django .template .loader import _engine_list
25
- for engine in _engine_list ():
26
- DIRS .extend (engine .dirs )
27
- app_template_dirs = get_app_template_dirs ('templates' )
28
21
29
-
30
- class Command (NoArgsCommand ):
22
+ class Command (BaseCommand ):
31
23
help = "Syncs file system templates with the database bidirectionally."
32
- option_list = NoArgsCommand .option_list + (
33
- make_option ("-e" , "--ext" ,
34
- dest = "ext" , action = "store" , default = "html" ,
35
- help = "extension of the files you want to "
36
- "sync with the database [default: %default]" ),
37
- make_option ("-f" , "--force" ,
38
- action = "store_true" , dest = "force" , default = False ,
39
- help = "overwrite existing database templates" ),
40
- make_option ("-o" , "--overwrite" ,
41
- action = "store" , dest = "overwrite" , default = '0' ,
42
- help = "'0' - ask always, '1' - overwrite database "
43
- "templates from template files, '2' - overwrite "
44
- "template files from database templates" ),
45
- make_option ("-a" , "--app-first" ,
46
- action = "store_true" , dest = "app_first" , default = False ,
47
- help = "look for templates in applications "
48
- "directories before project templates" ),
49
- make_option ("-d" , "--delete" ,
50
- action = "store_true" , dest = "delete" , default = False ,
51
- help = "Delete templates after syncing" ))
52
24
53
- def handle_noargs (self , ** options ):
25
+ def add_arguments (self , parser ):
26
+ parser .add_argument (
27
+ "-e" , "--ext" ,
28
+ dest = "ext" , action = "store" , default = "html" ,
29
+ help = "extension of the files you want to "
30
+ "sync with the database [default: %default]" )
31
+ parser .add_argument (
32
+ "-f" , "--force" ,
33
+ action = "store_true" , dest = "force" , default = False ,
34
+ help = "overwrite existing database templates" )
35
+ parser .add_argument (
36
+ "-o" , "--overwrite" ,
37
+ action = "store" , dest = "overwrite" , default = '0' ,
38
+ help = "'0' - ask always, '1' - overwrite database "
39
+ "templates from template files, '2' - overwrite "
40
+ "template files from database templates" )
41
+ parser .add_argument (
42
+ "-a" , "--app-first" ,
43
+ action = "store_true" , dest = "app_first" , default = False ,
44
+ help = "look for templates in applications "
45
+ "directories before project templates" )
46
+ parser .add_argument (
47
+ "-d" , "--delete" ,
48
+ action = "store_true" , dest = "delete" , default = False ,
49
+ help = "Delete templates after syncing" )
50
+
51
+ def handle (self , ** options ):
54
52
extension = options .get ('ext' )
55
53
force = options .get ('force' )
56
54
overwrite = options .get ('overwrite' )
@@ -66,10 +64,6 @@ def handle_noargs(self, **options):
66
64
raise CommandError ("Please make sure to have the sites contrib "
67
65
"app installed and setup with a site object" )
68
66
69
- if not type (settings .TEMPLATE_DIRS ) in (tuple , list ):
70
- raise CommandError ("Please make sure settings.TEMPLATE_DIRS is a "
71
- "list or tuple." )
72
-
73
67
if app_first :
74
68
tpl_dirs = app_template_dirs + DIRS
75
69
else :
@@ -93,8 +87,8 @@ def handle_noargs(self, **options):
93
87
"database.\n Create it with '%s'?"
94
88
" (y/[n]): " "" % (name , path ))
95
89
if force or confirm .lower ().startswith ('y' ):
96
- t = Template ( name = name ,
97
- content = codecs . open ( path , "r" ) .read ())
90
+ with io . open ( path , encoding = 'utf-8' ) as f :
91
+ t = Template ( name = name , content = f .read ())
98
92
t .save ()
99
93
t .sites .add (site )
100
94
else :
@@ -111,21 +105,19 @@ def handle_noargs(self, **options):
111
105
if confirm in ('' , FILES_TO_DATABASE ,
112
106
DATABASE_TO_FILES ):
113
107
if confirm == FILES_TO_DATABASE :
114
- t .content = codecs .open (path , 'r' ).read ()
115
- t .save ()
116
- t .sites .add (site )
108
+ with io .open (path , encoding = 'utf-8' ) as f :
109
+ t .content = f .read ()
110
+ t .save ()
111
+ t .sites .add (site )
117
112
if delete :
118
113
try :
119
114
os .remove (path )
120
115
except OSError :
121
116
raise CommandError (
122
117
u"Couldn't delete %s" % path )
123
118
elif confirm == DATABASE_TO_FILES :
124
- f = codecs .open (path , 'w' , 'utf-8' )
125
- try :
119
+ with io .open (path , 'w' , encoding = 'utf-8' ) as f :
126
120
f .write (t .content )
127
- finally :
128
- f .close ()
129
121
if delete :
130
122
t .delete ()
131
123
break
0 commit comments