@@ -13,56 +13,89 @@ def _get_default_msg(self, cursor, uid, context=None):
13
13
log_ids = context .get ('active_ids' , [])
14
14
return _ (u"Es reexportaràn {} arxius" .format (len (log_ids )))
15
15
16
+ def _create_working_directory (self , cursor , uid , ids , context = None ):
17
+ """
18
+ Creates the working directory and returns it's name
19
+ """
20
+ temporal_folder_name = '/tmp/wizard_reexportar_log_{}' .format (
21
+ datetime .strftime (datetime .today (), '%Y%m%d%H%M%S.%f' )
22
+ os .makedirs (temporal_folder_name )
23
+ return temporal_folder_name
24
+
25
+ def _zip_xml_files (self , cursor , uid , ids , folder , generated_files ):
26
+ zip_path = "{}/{}" .format (folder , 'casos_exportats.zip' )
27
+ with zipfile .ZipFile (zip_path , mode = 'w' , compression = zipfile .ZIP_DEFLATED ) as zf :
28
+ for file in generated_files :
29
+ zf .writestr (file [0 ], file [1 ])
30
+
31
+ return zip_path
32
+
16
33
def reexport_files (self , cursor , uid , ids , context = None ):
17
34
if context is None :
18
- context = {}
35
+ context = {}
19
36
if not isinstance (ids , list ):
20
- ids = [ids ]
21
- log_ids = context .get ('active_ids' , False )
37
+ ids = [ids ]
38
+ wizard = self .browse (cursor , uid , ids [0 ], context = context )
39
+ log_ids = context .get ('active_ids' , False )
22
40
23
41
if not log_ids :
24
42
raise osv .except_osv ('Error' , _ ('No s\' han seleccionat ids' ))
25
- log_obj = self .pool .get ('giscedata.switching.log' )
26
- sw_obj = self .pool .get ('giscedata.switching' )
27
- step_obj = self .pool .get ('giscedata.switching.step' )
28
- proces_obj = self .pool .get ('giscedata.switching.proces' )
43
+ log_obj = self .pool .get ('giscedata.switching.log' )
44
+ sw_obj = self .pool .get ('giscedata.switching' )
45
+ step_obj = self .pool .get ('giscedata.switching.step' )
46
+ proces_obj = self .pool .get ('giscedata.switching.proces' )
29
47
30
48
context .update ({
31
49
'update_logs' : True
32
50
})
33
- failed_files = []
34
- log_vals = log_obj .read (cursor , uid , log_ids , ['status' , 'pas' , 'proces' , 'request_code' ])
51
+ failed_files = []
52
+ log_vals = log_obj .read (cursor , uid , log_ids , ['status' , 'pas' , 'proces' , 'request_code' ])
53
+ generated_files = []
35
54
for log in log_vals :
36
55
if log ['status' ] == 'correcte' :
37
56
continue
38
- sw_id = sw_obj .search (cursor , uid , [('codi_sollicitud' , '=' , log ['request_code' ])])
39
- proces_id = proces_obj .search (cursor , uid , [('name' , '=' , log ['proces' ])])
40
- pas_obj = self .pool .get ('giscedata.switching.{}.{}' .format (
57
+ sw_id = sw_obj .search (cursor , uid , [('codi_sollicitud' , '=' , log ['request_code' ])])
58
+ proces_id = proces_obj .search (cursor , uid , [('name' , '=' , log ['proces' ])])
59
+ pas_obj = self .pool .get ('giscedata.switching.{}.{}' .format (
41
60
log ['proces' ].lower (), log ['pas' ]))
42
- pas_id = pas_obj .search (cursor , uid , [('sw_id' , '=' , sw_id )])
61
+ pas_id = pas_obj .search (cursor , uid , [('sw_id' , '=' , sw_id )])
43
62
try :
44
- step_id = step_obj .search (cursor , uid ,
63
+ step_id = step_obj .search (cursor , uid ,
45
64
[('name' , '=' , log ['pas' ]), ('proces_id' , '=' , proces_id [0 ])])
46
- sw_obj .exportar_xml (cursor , uid , sw_id [0 ], step_id [0 ], pas_id [0 ], context = context )
65
+ xml = sw_obj .exportar_xml (
66
+ cursor , uid , sw_id [0 ], step_id [0 ], pas_id [0 ], context = context )
67
+ generated_files .append (xml )
47
68
except Exception , e :
48
- e_string = str (e )
69
+ e_string = str (e )
49
70
if not e_string :
50
- e_string = e .value
71
+ e_string = e .value
51
72
failed_files .append ((fname , e_string ))
52
- info = _ (u"S'han processat {} fitxers.\n " .format (len (log_vals )))
73
+ info = _ (u"S'han processat {} fitxers.\n " .format (len (log_vals )))
53
74
if failed_files :
54
75
info += _ (u"S'han produït els següents errors en els arxius exportats:\n " )
55
76
for failed_f in failed_files :
56
77
info += _ (u"\t - Fitxer {} -> Error: {}\n " .format (failed_f [0 ], failed_f [1 ]))
57
78
58
- self .write (cursor , uid , ids , {'state' : 'end' , 'msg' : info }, context = context )
79
+ folder = self ._create_working_directory (cursor , uid , ids )
80
+ zip_file = self ._zip_xml_files (cursor , uid , ids , folder , generated_files )
81
+ f = open (zip_file , 'rb' )
82
+ out = f .read ()
83
+ f .close ()
84
+
85
+ wizard .write ({
86
+ 'report' : base64 .encodestring (out ),
87
+ 'filename' : zip_file .split ('/' )[- 1 ],
88
+ })
89
+ wizard .write ({'state' : 'end' , 'msg' : info }, context = context )
59
90
60
- _columns = {
91
+ _columns = {
61
92
'state' : fields .selection ([('init' , 'Init' ), ('end' , 'End' )], 'State' ),
62
- 'msg' : fields .text ('Missatge' )
93
+ 'msg' : fields .text ('Missatge' ),
94
+ 'report' : fields .binary ('Fichero a descargar' ),
95
+ 'filename' : fields .char ('Nombre fichero exportado' , size = 256 ),
63
96
}
64
97
65
- _defaults = {
98
+ _defaults = {
66
99
'state' : lambda * a : 'init' ,
67
100
'msg' : _get_default_msg ,
68
101
}
0 commit comments