@@ -51,18 +51,10 @@ def create_or_open(file_name, initial_template_file_name, args):
5151 :param args: from console to determine path to save the files
5252 """
5353 file = None
54- if not os .path .isfile (
55- os .path .join (
56- args ['django_application_folder' ],
57- file_name
58- )
59- ):
54+ if not os .path .isfile (file_name ):
6055 # If file_name does not exists, create
6156 file = codecs .open (
62- os .path .join (
63- args ['django_application_folder' ],
64- file_name
65- ),
57+ file_name ,
6658 'w+' ,
6759 encoding = 'UTF-8'
6860 )
@@ -72,10 +64,7 @@ def create_or_open(file_name, initial_template_file_name, args):
7264 else :
7365 # If file exists, just load the file
7466 file = codecs .open (
75- os .path .join (
76- args ['django_application_folder' ],
77- file_name
78- ),
67+ file_name ,
7968 'a+' ,
8069 encoding = 'UTF-8'
8170 )
@@ -90,7 +79,10 @@ def generic_insert_module(module_name, args, **kwargs):
9079 :paran **kwargs: Args to be rendered in template
9180 """
9281 file = create_or_open (
93- '{}.py' .format (module_name ),
82+ os .path .join (
83+ args ['django_application_folder' ],
84+ '{}.py' .format (module_name ),
85+ ),
9486 os .path .join (
9587 BASE_TEMPLATES_DIR ,
9688 '{}_initial.py.tmpl' .format (module_name )
@@ -152,27 +144,8 @@ def sanity_check(args):
152144 print ("Model does not exists" )
153145 sys .exit (1 )
154146
155- # Validate views are created
156-
157- views_file_name = os .path .join (
158- args ['django_application_folder' ],
159- 'views' ,
160- "{}.py" .format (args ['simplified_view_file_name' ])
161- )
162147
163- if functools .reduce (
164- operator .and_ ,
165- map (
166- check_class_in_file ,
167- (views_file_name ,)* len (VIEW_CLASSES ),
168- VIEW_CLASSES
169- )
170- ):
171- print ("Al views already created" )
172- sys .exit (1 )
173-
174-
175- def generic_insert_with_folder (folder_name , file_name , template_name , args ):
148+ def generic_insert_with_folder (folder_name , file_name , template_name , checking_classes ,args ):
176149 """
177150 In general if we need to put a file on a folder, we use this method
178151 """
@@ -192,15 +165,28 @@ def generic_insert_with_folder(folder_name, file_name, template_name, args):
192165 ),
193166 'w+'
194167 )
195-
168+ full_file_name = os .path .join (
169+ args ['django_application_folder' ],
170+ folder_name ,
171+ '{}.py' .format (file_name )
172+ )
196173 view_file = create_or_open (
197- os .path .join (
198- folder_name ,
199- '{}.py' .format (file_name )
200- ),
174+ full_file_name ,
201175 '' ,
202176 args
203177 )
178+
179+ if functools .reduce (
180+ operator .and_ ,
181+ map (
182+ check_class_in_file ,
183+ (full_file_name ,)* len (VIEW_CLASSES ),
184+ checking_classes
185+ )
186+ ):
187+ print ("All classes {} already on file {}" .format (", " .join (checking_classes ), file_name ))
188+ return
189+
204190 # Load content from template
205191 render_template_with_args_in_file (
206192 view_file ,
@@ -274,9 +260,22 @@ def execute_from_command_line():
274260
275261 sanity_check (args )
276262
277- generic_insert_with_folder ("views" , simplified_file_name , "view.py.tmpl" , args )
263+ generic_insert_with_folder (
264+ "views" ,
265+ simplified_file_name ,
266+ "view.py.tmpl" ,
267+ VIEW_CLASSES ,
268+ args
269+ )
278270 # Seems like tests also has the same logic
279- generic_insert_with_folder ("tests" , "test_{}" .format (simplified_file_name ), "tests.py.tmpl" , args )
271+ permission_class_name = "{}Permission" .format (args ["model_name" ])
272+ generic_insert_with_folder (
273+ "tests" ,
274+ "test_{}" .format (simplified_file_name ),
275+ "tests.py.tmpl" ,
276+ [permission_class_name ],
277+ args
278+ )
280279
281280 modules_to_inject = [
282281 'conf' ,
@@ -315,6 +314,7 @@ def execute_from_command_line():
315314 render_template_with_args_in_file (
316315 create_or_open (
317316 os .path .join (
317+ args ['django_application_folder' ],
318318 BASE_TEMPLATES_DIR ,
319319 'urls.py'
320320 ),
0 commit comments