Skip to content

Commit 226b6f8

Browse files
committed
Download zip file
1 parent d731741 commit 226b6f8

4 files changed

+58
-24
lines changed

som_switching/demo/som_switching_demo_data.xml

+1-1
Original file line numberDiff line numberDiff line change
@@ -30,4 +30,4 @@
3030
<field name="ref2">R1-003</field>
3131
</record>
3232
</data>
33-
</openerp>
33+
</openerp>

som_switching/tests/tests_wizard_reexport_log.py

-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,5 @@
11
# -*- coding: utf-8 -*-
22

3-
from destral import testing
43
from destral.transaction import Transaction
54
from giscedata_switching.tests.common_tests import TestSwitchingImport
65

som_switching/wizard/giscedata_switching_log_reexport_wizard.py

+55-22
Original file line numberDiff line numberDiff line change
@@ -13,56 +13,89 @@ def _get_default_msg(self, cursor, uid, context=None):
1313
log_ids = context.get('active_ids', [])
1414
return _(u"Es reexportaràn {} arxius".format(len(log_ids)))
1515

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+
1633
def reexport_files(self, cursor, uid, ids, context=None):
1734
if context is None:
18-
context = {}
35+
context={}
1936
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)
2240

2341
if not log_ids:
2442
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')
2947

3048
context.update({
3149
'update_logs': True
3250
})
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=[]
3554
for log in log_vals:
3655
if log['status'] == 'correcte':
3756
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(
4160
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)])
4362
try:
44-
step_id = step_obj.search(cursor, uid,
63+
step_id=step_obj.search(cursor, uid,
4564
[('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)
4768
except Exception, e:
48-
e_string = str(e)
69+
e_string=str(e)
4970
if not e_string:
50-
e_string = e.value
71+
e_string=e.value
5172
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)))
5374
if failed_files:
5475
info += _(u"S'han produït els següents errors en els arxius exportats:\n")
5576
for failed_f in failed_files:
5677
info += _(u"\t- Fitxer {} -> Error: {}\n".format(failed_f[0], failed_f[1]))
5778

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)
5990

60-
_columns = {
91+
_columns={
6192
'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),
6396
}
6497

65-
_defaults = {
98+
_defaults={
6699
'state': lambda *a: 'init',
67100
'msg': _get_default_msg,
68101
}

som_switching/wizard/giscedata_switching_log_reexport_wizard_view.xml

+2
Original file line numberDiff line numberDiff line change
@@ -8,12 +8,14 @@
88
<field name="arch" type="xml">
99
<form string="Reexportar fitxers dels logs">
1010
<field name="state" invisible="1"/>
11+
<field name="filename" invisible="1"/>
1112
<field name="msg" nolabel="1" readonly="1" height="200" width="600"/>
1213
<group col="2" colspan="8" attrs="{'invisible': [('state', '!=', 'init')]}">
1314
<button string="Cancel·lar" special="cancel" icon="gtk-close" colspan="1"/>
1415
<button string="Reexportar fitxers" name="reexport_files" type="object" icon="gtk-go-forward" colspan="1" />
1516
</group>
1617
<group col="2" colspan="8" attrs="{'invisible': [('state', '=', 'init')]}">
18+
<field name="report" width="500" filename="filename_report"/>
1719
<button string="Tancar" special="cancel" icon="gtk-close" colspan="1"/>
1820
</group>
1921
</form>

0 commit comments

Comments
 (0)