83
94
self._number = start
84
95
self._interval = interval
87
curr_number = self._number
88
self._number += self._interval
97
def next(self, increment=True):
99
self._number += self._interval
102
return self._number + self._interval
91
104
def get_inc(self):
92
105
return self._number
95
return self._number-self._interval
107
def prev(self, decrement=True):
109
self._number -= self._interval
112
return self._number-self._interval
114
class AerooPrint(object):
115
print_ids = [] # static property
118
while(not print_id or print_id in self.print_ids):
119
print_id = randint(1, 99999)
120
self.print_ids.append(print_id)
126
self.start_total_time = 0
97
128
class Aeroo_report(report_sxw):
138
166
return super(Aeroo_report, self).getObjects(cr, uid, ids, context)
140
168
##### Counter functions #####
141
def _def_inc(self, name, start=0, interval=1):
142
self.counters[name] = Counter(name, start, interval)
144
def _get_inc(self, name):
145
return self.counters[name].get_inc()
147
def _prev(self, name):
148
return self.counters[name].prev()
150
def _next(self, name):
151
return self.counters[name].next()
169
def _def_inc(self, aeroo_print):
170
def def_inc(name, start=0, interval=1):
171
aeroo_print.counters[name] = Counter(name, start, interval)
174
def _get_inc(self, aeroo_print):
176
return aeroo_print.counters[name].get_inc()
179
def _prev(self, aeroo_print):
181
return aeroo_print.counters[name].prev()
184
def _next(self, aeroo_print):
186
return aeroo_print.counters[name].next()
152
188
#############################
154
def _epl_asimage(self, data):
190
def _epl_asimage(self, data, aeroo_print):
155
191
from PIL import Image
156
192
from math import ceil
170
206
for d in temp_data:
171
207
new_data += chr(ord(d)^255)
172
self.epl_images.append(new_data)
208
aeroo_print.epl_images.append(new_data)
175
def _epl2_gw(self, start_x, start_y, data):
178
size_x, size_y = self._epl_asimage(data)
179
return 'GW'+str(start_x)+','+str(start_y)+','+str(int(size_x/8))+','+str(size_y)+',<binary_data>'
211
def _epl2_gw(self, aeroo_print):
212
def epl2_gw(start_x, start_y, data):
215
size_x, size_y = self._epl_asimage(data, aeroo_print)
216
return 'GW'+str(start_x)+','+str(start_y)+','+str(int(size_x/8))+','+str(size_y)+',<binary_data>'
181
def _include_document(self, aeroo_ooo=False):
219
def _include_document(self, print_id, aeroo_ooo=False):
182
220
def include_document(data, silent=False):
183
221
if not aeroo_ooo:
184
222
return _("Error! Include document not available!")
185
223
import binascii, urllib2
186
dummy_fd, temp_file_name = tempfile.mkstemp(suffix='.odt', prefix='aeroo-report-')
187
temp_file = open(temp_file_name, 'wb')
224
temp_file = NamedTemporaryFile(suffix='.odt', prefix='aeroo-report-', delete=False)
188
225
if os.path.isfile(data):
189
226
fd = file(data, 'rb')
194
231
url_file = urllib2.urlopen(data)
195
232
data = url_file.read()
196
233
except urllib2.HTTPError, e:
197
os.unlink(temp_file_name)
234
os.unlink(temp_file.name)
198
235
error = _('HTTP Error %s! File not found:') % e.getcode() + ' %s' % data
199
236
except urllib2.URLError, e:
200
os.unlink(temp_file_name)
237
os.unlink(temp_file.name)
201
238
error = _('Error!')+' %s' % e
202
239
except IOError, e:
203
os.unlink(temp_file_name)
240
os.unlink(temp_file.name)
204
241
error = _('Error!')+' %s' % e
205
242
except Exception, e:
207
244
data = base64.decodestring(data)
208
245
except binascii.Error:
209
os.unlink(temp_file_name)
246
os.unlink(temp_file.name)
210
247
error = _('Error! File not found:')+' %s' % data
217
254
temp_file.write(data)
219
256
temp_file.close()
220
self.oo_subreports.append(temp_file_name)
221
return "<insert_doc('%s')>" % temp_file_name
257
#self.oo_subreports[print_id].append(temp_file.name)
258
self.active_prints[print_id].subreports.append(temp_file.name)
259
return "<insert_doc('%s')>" % temp_file.name
222
260
return include_document
224
def _subreport(self, cr, uid, output='odt', aeroo_ooo=False, context={}):
262
def _subreport(self, cr, uid, aeroo_print, output='odt', aeroo_ooo=False, context={}):
225
263
pool = pooler.get_pool(cr.dbname)
226
264
ir_obj = pool.get('ir.actions.report.xml')
227
265
#### for odt documents ####
230
268
return _("Error! Subreports not available!")
231
269
report_xml_ids = ir_obj.search(cr, uid, [('report_name', '=', name)], context=context)
232
270
if report_xml_ids:
271
service = netsvc.Service._services['report.%s' % name]
233
272
report_xml = ir_obj.browse(cr, uid, report_xml_ids[0], context=context)
234
273
data = {'model': obj._table_name, 'id': obj.id, 'report_type': 'aeroo', 'in_format': 'oo-odt'}
235
report, output = netsvc.Service._services['report.%s' % name].create_aeroo_report(cr, uid, \
274
### Get new printing object ###
275
sub_aeroo_print = AerooPrint()
276
service.active_prints[sub_aeroo_print.id] = sub_aeroo_print
277
context['print_id'] = sub_aeroo_print.id
278
###############################
279
sub_aeroo_print.start_time = time.time()
280
report, output = service.create_aeroo_report(cr, uid, \
236
281
[obj.id], data, report_xml, context=context, output='odt') # change for OpenERP 6.0 - Service class usage
238
dummy_fd, temp_file_name = tempfile.mkstemp(suffix='.odt', prefix='aeroo-report-')
239
temp_file = open(temp_file_name, 'wb')
283
### Delete printing object ###
284
AerooPrint.print_ids.remove(sub_aeroo_print.id)
285
del service.active_prints[sub_aeroo_print.id]
286
##############################
287
with NamedTemporaryFile(suffix='.odt', prefix='aeroo-report-', delete=False) as temp_file:
241
288
temp_file.write(report)
245
self.oo_subreports.append(temp_file_name)
247
return "<insert_doc('%s')>" % temp_file_name
290
#self.oo_subreports[print_id].append(temp_file.name)
291
aeroo_print.subreports.append(temp_file.name)
293
return "<insert_doc('%s')>" % temp_file.name
249
295
#### for text documents ####
250
296
def raw_subreport(name=None, obj=None):
301
347
def create_genshi_raw_report(self, cr, uid, ids, data, report_xml, context=None, output='raw', tmpl=False):
302
def preprocess(data):
303
self.epl_images.reverse()
304
while self.epl_images:
305
img = self.epl_images.pop()
348
def preprocess(data, aeroo_print):
349
aeroo_print.epl_images.reverse()
350
while aeroo_print.epl_images:
351
img = aeroo_print.epl_images.pop()
306
352
data = data.replace('<binary_data>', img, 1)
307
353
return data.replace('\n', '\r\n')
309
if not self.start_time:
310
self.start_time = time.time()
355
print_id = context.get('print_id', False)
356
aeroo_print = self.active_prints[print_id] # Aeroo print object
357
if not aeroo_print.start_time:
358
aeroo_print.start_time = time.time()
313
361
context = context.copy()
315
363
oo_parser = self.parser(cr, uid, self.name2, context=context)
316
364
oo_parser.localcontext.update(context)
317
365
oo_parser.set_context(objects, data, ids, report_xml.report_type)
318
#oo_parser.objects = objects
319
366
self.set_xml_data_fields(oo_parser.objects, oo_parser) # Get/Set XML
320
oo_parser.localcontext['objects'] = oo_parser.objects
367
#oo_parser.localcontext['objects'] = oo_parser.objects
321
368
oo_parser.localcontext['data'] = data
322
369
oo_parser.localcontext['user_lang'] = context.get('lang', False)
323
370
if len(objects)>0:
325
372
xfunc = ExtraFunctions(cr, uid, report_xml.id, oo_parser.localcontext)
326
373
oo_parser.localcontext.update(xfunc.functions)
327
374
file_data = tmpl or self.get_other_template(cr, uid, data, oo_parser) or report_xml.report_sxw_content # Get other Tamplate
328
if file_data=='False':
375
if not file_data or file_data=='False':
329
376
raise osv.except_osv(_('Error!'), _('No template found!'))
330
377
################################################
331
378
if not file_data:
332
self.logger("End process %s (%s), elapsed time: %s" % (self.name, self.table, time.time() - self.start_time), netsvc.LOG_INFO) # debug mode
379
self.logger("End process %s (%s), elapsed time: %s" % (self.name, self.table, time.time() - aeroo_print.start_time), netsvc.LOG_INFO) # debug mode
333
380
return False, output
335
oo_parser.localcontext['include_subreport'] = self._subreport(cr, uid, output='raw', aeroo_ooo=False, context=context)
336
oo_parser.localcontext['epl2_gw'] = self._epl2_gw
382
print_id = context.get('print_id', False)
383
aeroo_print = self.active_prints[print_id]
385
oo_parser.localcontext['include_subreport'] = self._subreport(cr, uid, aeroo_print, output='raw', aeroo_ooo=False, context=context)
386
oo_parser.localcontext['epl2_gw'] = self._epl2_gw(aeroo_print)
387
deferred = context.get('deferred_process')
388
oo_parser.localcontext['progress_update'] = deferred and deferred.progress_update or (lambda:True)
390
aeroo_print.epl_images = []
339
391
basic = NewTextTemplate(source=base64.decodestring(file_data))
341
data = preprocess(basic.generate(**oo_parser.localcontext).render().decode('utf8').encode(report_xml.charset))
393
if genshi_version<='0.6':
394
data = preprocess(basic.generate(**oo_parser.localcontext).render().decode('utf8').encode(report_xml.charset), aeroo_print)
396
data = preprocess(basic.generate(**oo_parser.localcontext).render().encode(report_xml.charset), aeroo_print)
342
397
#except Exception, e:
343
398
# self.logger(str(e), netsvc.LOG_ERROR)
344
399
# return False, output
346
401
if report_xml.content_fname:
347
402
output = report_xml.content_fname
348
self.logger("End process %s (%s), elapsed time: %s" % (self.name, self.table, time.time() - self.start_time), netsvc.LOG_INFO) # debug mode
403
self.logger("End process %s (%s), elapsed time: %s" % (self.name, self.table, time.time() - aeroo_print.start_time), netsvc.LOG_INFO) # debug mode
349
404
return data, output
406
def _generate_doc(self, DC, data, report_xml, print_id):
409
#subreports = self.oo_subreports.get(print_id)
410
aeroo_print = self.active_prints.get(print_id, False)
412
DC.insertSubreports(aeroo_print.subreports)
413
#self.oo_subreports = []
414
#del self.oo_subreports[print_id]
415
if report_xml.out_format.code=='oo-dbf':
416
data = DC.saveByStream(report_xml.out_format.filter_name, "78")
418
data = DC.saveByStream(report_xml.out_format.filter_name)
422
def _raise_exception(self, e, print_id):
423
tb_s = reduce(lambda x, y: x+y, traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
424
self.logger(_("Report generation error!")+'\n'+tb_s, netsvc.LOG_ERROR)
425
#subreports = self.oo_subreports.get(print_id, [])
426
aeroo_print = self.active_prints.get(print_id, [])
428
for sub_report in aeroo_print.subreports:
429
if os.path.isfile(sub_report):
430
os.unlink(sub_report)
431
raise Exception(_("Aeroo Reports: Error while generating the report."), e, str(e), _("For more reference inspect error logs."))
351
433
def create_aeroo_report(self, cr, uid, ids, data, report_xml, context=None, output='odt'):
352
434
""" Returns an aeroo report generated with aeroolib
359
441
context['model'] = data['model']
360
442
context['ids'] = ids
444
print_id = context.get('print_id', False)
445
aeroo_print = self.active_prints[print_id] # Aeroo print object
446
aeroo_print.subreports = []
447
#self.oo_subreports[print_id] = []
448
oo_parser = self.parser(cr, uid, self.name2, context=context)
449
if not report_xml.process_sep and getattr(oo_parser, 'single', False):
362
451
objects = self.getObjects_mod(cr, uid, ids, report_xml.report_type, context) or []
363
oo_parser = self.parser(cr, uid, self.name2, context=context)
364
452
oo_parser.localcontext.update(context)
365
453
oo_parser.set_context(objects, data, ids, report_xml.report_type)
367
455
#oo_parser.objects = objects
368
self.set_xml_data_fields(objects, oo_parser) # Get/Set XML
456
#self.set_xml_data_fields(objects, oo_parser) # Get/Set XML
370
oo_parser.localcontext['objects'] = oo_parser.objects
458
#oo_parser.localcontext['objects'] = oo_parser.objects
371
459
oo_parser.localcontext['data'] = data
372
460
oo_parser.localcontext['user_lang'] = context.get('lang', False)
374
462
oo_parser.localcontext['o'] = objects[0]
375
463
xfunc = ExtraFunctions(cr, uid, report_xml.id, oo_parser.localcontext)
376
464
oo_parser.localcontext.update(xfunc.functions)
381
469
style_io=self.get_styles_file(cr, uid, report_xml, company=company_id, context=context)
383
471
if report_xml.tml_source in ('file', 'database'):
384
if report_xml.report_sxw_content=='False':
472
if not report_xml.report_sxw_content or report_xml.report_sxw_content=='False':
385
473
raise osv.except_osv(_('Error!'), _('No template found!'))
386
474
file_data = base64.decodestring(report_xml.report_sxw_content)
388
476
file_data = self.get_other_template(cr, uid, data, oo_parser)
389
477
if not file_data and not report_xml.report_sxw_content:
390
self.logger("End process %s (%s), elapsed time: %s" % (self.name, self.table, time.time() - self.start_time), netsvc.LOG_INFO) # debug mode
478
self.logger("End process %s (%s), elapsed time: %s" % (self.name, self.table, time.time() - aeroo_print.start_time), netsvc.LOG_INFO) # debug mode
391
479
return False, output
393
481
# template_io = StringIO()
410
501
#basic = Template(source=template_io, serializer=serializer)
412
503
aeroo_ooo = context.get('aeroo_ooo', False)
413
oo_parser.localcontext['include_subreport'] = self._subreport(cr, uid, output='odt', aeroo_ooo=aeroo_ooo, context=context)
414
oo_parser.localcontext['include_document'] = self._include_document(aeroo_ooo)
504
oo_parser.localcontext['include_subreport'] = self._subreport(cr, uid, aeroo_print, output='odt', aeroo_ooo=aeroo_ooo, context=context)
505
oo_parser.localcontext['include_document'] = self._include_document(aeroo_ooo, print_id)
506
deferred = context.get('deferred_process')
507
oo_parser.localcontext['progress_update'] = deferred and deferred.progress_update or (lambda:True)
416
508
####### Add counter functons to localcontext #######
417
oo_parser.localcontext.update({'def_inc':self._def_inc,
418
'get_inc':self._get_inc,
509
oo_parser.localcontext.update({'def_inc':self._def_inc(aeroo_print),
510
'get_inc':self._get_inc(aeroo_print),
511
'prev':self._prev(aeroo_print),
512
'next':self._next(aeroo_print)})
422
514
user_name = pool.get('res.users').browse(cr, uid, uid, {}).name
423
515
model_id = pool.get('ir.model').search(cr, uid, [('model','=',context.get('active_model', data['model']) or data['model'])])[0]
440
532
except osv.except_osv, e:
442
534
except Exception, e:
443
tb_s = reduce(lambda x, y: x+y, traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
444
self.logger(_("Report generation error!")+'\n'+tb_s, netsvc.LOG_ERROR)
445
for sub_report in self.oo_subreports:
446
if os.path.isfile(sub_report):
447
os.unlink(sub_report)
448
raise Exception(_("Aeroo Reports: Error while generating the report."), e, str(e), _("For more reference inspect error logs."))
535
self._raise_exception(e, print_id)
450
537
######### OpenOffice extras #########
451
538
DC = netsvc.Service._services.get('openoffice')
452
if (output!=report_xml.in_format[3:] or self.oo_subreports):
539
#if (output!=report_xml.in_format[3:] or self.oo_subreports.get(print_id)):
540
if output!=report_xml.in_format[3:] or aeroo_print.subreports:
453
541
if aeroo_ooo and DC:
456
if self.oo_subreports:
457
DC.insertSubreports(self.oo_subreports)
458
self.oo_subreports = []
459
if report_xml.out_format.code=='oo-dbf':
460
data = DC.saveByStream(report_xml.out_format.filter_name, "78")
462
data = DC.saveByStream(report_xml.out_format.filter_name)
543
data = self._generate_doc(DC, data, report_xml, print_id)
465
544
except Exception, e:
466
545
self.logger(_("OpenOffice.org related error!")+'\n'+str(e), netsvc.LOG_ERROR)
467
if report_xml.fallback_false:
468
raise osv.except_osv(_('OpenOffice.org related error!'), str(e))
546
if DC._restart_ooo():
549
data = self._generate_doc(DC, data, report_xml, print_id)
551
self.logger(_("OpenOffice.org related error!")+'\n'+str(e), netsvc.LOG_ERROR)
552
if not report_xml.fallback_false:
553
output=report_xml.in_format[3:]
554
elif not report_xml.fallback_false:
470
555
output=report_xml.in_format[3:]
471
self.oo_subreports = []
556
aeroo_print.subreports = []
473
558
if report_xml.fallback_false:
474
559
if not aeroo_ooo:
518
604
pool = pooler.get_pool(cr.dbname)
519
605
attach = report_xml.attachment
520
aeroo_ooo = _aeroo_ooo_test(cr) # Detect report_aeroo_ooo module
606
aeroo_ooo = aeroo_ooo_test(cr) # Detect report_aeroo_ooo module
521
607
context['aeroo_ooo'] = aeroo_ooo
608
print_id = context.get('print_id', False)
609
aeroo_print = self.active_prints[print_id] # Aeroo print object
522
610
if attach or aeroo_ooo and report_xml.process_sep:
523
objs = self.getObjects(cr, uid, ids, context)
611
objs = self.getObjects_mod(cr, uid, ids, report_xml.report_type, context)
612
deferred = context.get('deferred_process')
615
aeroo_print.start_time = time.time()
617
deferred.progress_update()
526
618
aname = attach and eval(attach, {'object':obj, 'time':time}) or False
528
620
if report_xml.attachment_use and aname and context.get('attachment_use', True):
589
681
pool = pooler.get_pool(cr.dbname)
591
683
attach = report_xml.attachment
592
aeroo_ooo = _aeroo_ooo_test(cr) # Detect report_aeroo_ooo module
684
aeroo_ooo = aeroo_ooo_test(cr) # Detect report_aeroo_ooo module
593
685
context['aeroo_ooo'] = aeroo_ooo
686
print_id = context.get('print_id', False)
687
aeroo_print = self.active_prints[print_id] # Aeroo print object
594
688
if attach or aeroo_ooo and report_xml.process_sep:
595
689
objs = self.getObjects_mod(cr, uid, ids, report_xml.report_type, context)
690
deferred = context.get('deferred_process')
692
aeroo_print.start_time = time.time()
694
deferred.progress_update()
597
695
aname = attach and eval(attach, {'object':obj, 'time':time}) or False
599
697
if report_xml.attachment_use and aname and context.get('attachment_use', True):
644
742
raise osv.except_osv(_('Error!'), _('Unsupported combination of formats!'))
645
743
results.reverse()
646
744
data = results.pop()
647
DC.putDocument(data[0])
648
DC.joinDocuments([r[0] for r in results])
649
result = DC.saveByStream()
746
DC.putDocument(data[0])
747
DC.joinDocuments([r[0] for r in results])
748
result = DC.saveByStream()
651
750
return (result, data[1])
653
752
return self.create_single_pdf(cr, uid, ids, data, report_xml, context)
655
754
# override needed to intercept the call to the proper 'create' method
656
755
def create(self, cr, uid, ids, data, context=None):
657
self.start_time = time.time()
756
#### Get Aeroo print object ###
757
aeroo_print = AerooPrint()
758
aeroo_print.start_total_time = time.time()
759
aeroo_print.start_time = time.time()
760
self.active_prints[aeroo_print.id] = aeroo_print
761
context['print_id'] = aeroo_print.id
762
###############################
658
763
self.logger("Start process %s (%s)" % (self.name, self.table), netsvc.LOG_INFO) # debug mode
764
pool = pooler.get_pool(cr.dbname)
767
if 'tz' not in context:
768
context['tz'] = pool.get('res.users').browse(cr, uid, uid).context_tz
659
770
data.setdefault('model', context.get('active_model',False))
660
pool = pooler.get_pool(cr.dbname)
661
771
ir_obj = pool.get('ir.actions.report.xml')
662
772
name = self.name.startswith('report.') and self.name[7:] or self.name
663
773
report_xml_ids = ir_obj.search(cr, uid,
707
817
return super(Aeroo_report, self).create(cr, uid, ids, data, context)
709
819
raise Exception(_('Unknown report type: %s') % report_type)
710
return fnct(cr, uid, ids, data, report_xml, context)
820
res = fnct(cr, uid, ids, data, report_xml, context)
821
### Delete printing object ###
822
AerooPrint.print_ids.remove(aeroo_print.id)
823
del self.active_prints[aeroo_print.id]
824
##############################
825
self.logger("End total process %s (%s), total elapsed time: %s" % (self.name, self.table, time.time() - aeroo_print.start_total_time), netsvc.LOG_INFO) # debug mode
712
828
class ReportTypeException(Exception):
713
829
def __init__(self, value):