@@ -228,19 +228,17 @@ def export(self, with_valid=True):
228228 self .is_valid (raise_exception = True )
229229 language = get_language ()
230230 if self .data .get ('type' ) == 'csv' :
231- file = open (
231+ with open (
232232 os .path .join (PROJECT_DIR , "apps" , "knowledge" , 'template' ,
233233 f'csv_template_{ to_locale (language )} .csv' ),
234- "rb" )
235- content = file .read ()
236- file .close ()
234+ "rb" ) as file :
235+ content = file .read ()
237236 return HttpResponse (content , status = 200 , headers = {'Content-Type' : 'text/csv' ,
238237 'Content-Disposition' : 'attachment; filename="csv_template.csv"' })
239238 elif self .data .get ('type' ) == 'excel' :
240- file = open (os .path .join (PROJECT_DIR , "apps" , "knowledge" , 'template' ,
241- f'excel_template_{ to_locale (language )} .xlsx' ), "rb" )
242- content = file .read ()
243- file .close ()
239+ with open (os .path .join (PROJECT_DIR , "apps" , "knowledge" , 'template' ,
240+ f'excel_template_{ to_locale (language )} .xlsx' ), "rb" ) as file :
241+ content = file .read ()
244242 return HttpResponse (content , status = 200 , headers = {'Content-Type' : 'application/vnd.ms-excel' ,
245243 'Content-Disposition' : 'attachment; filename="excel_template.xlsx"' })
246244 else :
@@ -251,20 +249,18 @@ def table_export(self, with_valid=True):
251249 self .is_valid (raise_exception = True )
252250 language = get_language ()
253251 if self .data .get ('type' ) == 'csv' :
254- file = open (
252+ with open (
255253 os .path .join (PROJECT_DIR , "apps" , "knowledge" , 'template' ,
256254 f'table_template_{ to_locale (language )} .csv' ),
257- "rb" )
258- content = file .read ()
259- file .close ()
255+ "rb" ) as file :
256+ content = file .read ()
260257 return HttpResponse (content , status = 200 , headers = {'Content-Type' : 'text/csv' ,
261258 'Content-Disposition' : 'attachment; filename="csv_template.csv"' })
262259 elif self .data .get ('type' ) == 'excel' :
263- file = open (os .path .join (PROJECT_DIR , "apps" , "knowledge" , 'template' ,
264- f'table_template_{ to_locale (language )} .xlsx' ),
265- "rb" )
266- content = file .read ()
267- file .close ()
260+ with open (os .path .join (PROJECT_DIR , "apps" , "knowledge" , 'template' ,
261+ f'table_template_{ to_locale (language )} .xlsx' ),
262+ "rb" ) as file :
263+ content = file .read ()
268264 return HttpResponse (content , status = 200 , headers = {'Content-Type' : 'application/vnd.ms-excel' ,
269265 'Content-Disposition' : 'attachment; filename="excel_template.xlsx"' })
270266 else :
0 commit comments