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