Exemplo n.º 1
0
    def create_with_attachment(self, cr, uid, ids, data, original_doc, context=None):
        import base64
        from report.pyPdf import PdfFileReader, PdfFileWriter
        o = PdfFileWriter()

        pool = pooler.get_pool(cr.dbname)
        quizz_id = ids[0]

        if data.get('model', '') == 'training.participation':
            participation = pool.get('training.participation').browse(cr, uid, data['id'])
            quizz_id = participation.questionnaire_id.id

        quizz_proxy = pool.get('training.exam.questionnaire')
        quizz = quizz_proxy.browse(cr, uid, quizz_id)

        def write_orig_document():
            r = PdfFileReader(StringIO.StringIO(original_doc[0]))
            for p in range(r.getNumPages()):
                o.addPage(r.getPage(p))

        def generate_watermark_pdf(docid, basenum, npages):
            watermark_report = netsvc.LocalService('report.training.participation.watermark.qrcode')
            report_ids = [ '%s-%s%03d' % (docid, basenum, n+1) for n in range(npages) ]
            (result, format) = watermark_report.create(cr, uid, report_ids, {}, context=context)
            return PdfFileReader(StringIO.StringIO(result))

        attach_count = 0
        orig_doc_written = False
        for attach in quizz.attachment_ids:
            if attach.position == 'after' and not orig_doc_written:
                write_orig_document()
                orig_doc_written = True

            # 1 = annex and store
            # 2 = detect qrcode but don't store in the DMS
            basenum = '%1s%02d' % (attach.store_in_dms and 1 or 2, attach_count)

            attach_pdf_data = base64.decodestring(attach.attachment_id.datas)
            attach_pdf = PdfFileReader(StringIO.StringIO(attach_pdf_data))
            waterm_pdf = generate_watermark_pdf(ids[0], basenum, attach_pdf.getNumPages())

            for attach_page in range(attach_pdf.getNumPages()):
                docp = attach_pdf.getPage(attach_page)
                docp_wm = waterm_pdf.getPage(attach_page)
                docp.mergePage(docp_wm)
                o.addPage(docp)

            attach_count += 1
        if not orig_doc_written:
            write_orig_document()

        s = StringIO.StringIO()
        o.write(s)
        return (s.getvalue(), original_doc[1])
Exemplo n.º 2
0
 def create_source_pdf(self, cr, uid, ids, data, report_xml, context=None):
     if not context:
         context={}
     pool = pooler.get_pool(cr.dbname)
     attach = report_xml.attachment
     aeroo_ooo = self._aeroo_ooo_test(cr) # Detect report_aeroo_ooo module
     context['aeroo_ooo'] = aeroo_ooo
     if attach or aeroo_ooo and report_xml.process_sep:
         objs = self.getObjects(cr, uid, ids, context)
         results = []
         for obj in objs:
             aname = attach and eval(attach, {'object':obj, 'time':time}) or False
             result = False
             if report_xml.attachment_use and aname and context.get('attachment_use', True):
                 aids = pool.get('ir.attachment').search(cr, uid, [('datas_fname','=',aname+'.pdf'),('res_model','=',self.table),('res_id','=',obj.id)])
                 if aids:
                     brow_rec = pool.get('ir.attachment').browse(cr, uid, aids[0])
                     if not brow_rec.datas:
                         continue
                     d = base64.decodestring(brow_rec.datas)
                     results.append((d,'pdf'))
                     continue
             result = self.create_single_pdf(cr, uid, [obj.id], data, report_xml, context)
             if not result:
                 return False
             try:
                 if attach and aname:
                     name = aname+'.'+result[1]
                     pool.get('ir.attachment').create(cr, uid, {
                         'name': aname,
                         'datas': base64.encodestring(result[0]),
                         'datas_fname': name,
                         'res_model': self.table,
                         'res_id': obj.id,
                         }, context=context
                     )
                     cr.commit()
             except Exception,e:
                  tb_s = reduce(lambda x, y: x+y, traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
                  netsvc.Logger().notifyChannel('report', netsvc.LOG_ERROR,str(e))
             results.append(result)
         if results:
             if results[0][1]=='pdf':
                 output = PdfFileWriter()
                 for r in results:
                     reader = PdfFileReader(StringIO(r[0]))
                     for page in range(reader.getNumPages()):
                         output.addPage(reader.getPage(page))
                 s = StringIO()
                 output.write(s)
                 return s.getvalue(), results[0][1]
Exemplo n.º 3
0
    def create_single_pdf(self, cr, uid, ids, data, report_xml, context=None):
        if isinstance(ids, (int, long)):
            ids = [ ids ]
        original_results = []
        original_records = self.getObjects(cr, uid, ids, context)
        final_document = PdfFileWriter()

        for record in original_records:
            # TODO: call "hook_before_document_generation"
            original_data, original_type = super(report_with_attachment, self).create_single_pdf(cr, uid, [record.id], data, report_xml, context)
            # TODO: call "hook_after_document_generation"
            self.append_raw_data_to_document(final_document, original_data)
            for data in self.get_attachment_data(cr, uid, record, context):
                self.append_raw_data_to_document(final_document, data)

        s = StringIO.StringIO()
        final_document.write(s)
        return (s.getvalue(), 'pdf')
Exemplo n.º 4
0
 def create_source_pdf(self, cr, uid, ids, data, report_xml, context=None):
     if not context:
         context={}
     pool = pooler.get_pool(cr.dbname)
     attach = report_xml.attachment
     aeroo_ooo = aeroo_ooo_test(cr) # Detect report_aeroo_ooo module
     context['aeroo_ooo'] = aeroo_ooo
     print_id = context.get('print_id', False)
     aeroo_print = self.active_prints[print_id] # Aeroo print object
     if attach or aeroo_ooo and report_xml.process_sep:
         objs = self.getObjects_mod(cr, uid, ids, report_xml.report_type, context)
         deferred = context.get('deferred_process')
         results = []
         for obj in objs:
             aeroo_print.start_time = time.time()
             if deferred:
                 deferred.progress_update()
             aname = attach and eval(attach, {'object':obj, 'time':time}) or False
             result = False
             if report_xml.attachment_use and aname and context.get('attachment_use', True):
                 #aids = pool.get('ir.attachment').search(cr, uid, [('datas_fname','=',aname+'.pdf'),('res_model','=',self.table),('res_id','=',obj.id)])
                 #if aids:
                 #    brow_rec = pool.get('ir.attachment').browse(cr, uid, aids[0])
                 #    if not brow_rec.datas:
                 #        continue
                 #    d = base64.decodestring(brow_rec.datas)
                 #    results.append((d,'pdf'))
                 #    continue
                 cr.execute("SELECT id, datas_fname FROM ir_attachment WHERE datas_fname ilike %s and res_model=%s and res_id=%s LIMIT 1", (aname+'.%',self.table,obj.id))
                 search_res = cr.dictfetchone()
                 if search_res:
                     brow_rec = pool.get('ir.attachment').browse(cr, uid, search_res['id'])
                     if not brow_rec.datas:
                         continue
                     d = base64.decodestring(brow_rec.datas)
                     extension = search_res['datas_fname'].split('.')[1]
                     results.append((d,extension))
                     continue
             result = self.create_single_pdf(cr, uid, [obj.id], data, report_xml, context)
             if not result:
                 return False
             try:
                 if attach and aname:
                     name = aname+'.'+result[1]
                     datas = base64.encodestring(result[0])
                     ctx = dict(context)
                     ctx.pop('default_type', None)
                     pool.get('ir.attachment').create(cr, uid, {
                         'name': aname,
                         'datas': datas,
                         'datas_fname': name,
                         'res_model': self.table,
                         'res_id': obj.id,
                         'type': 'binary'
                         }, context=ctx
                     )
                     cr.commit()
             except Exception,e:
                 tb_s = reduce(lambda x, y: x+y, traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
                 logger.log(logging.ERROR, str(e))
             results.append(result)
         if results and len(results)==1:
             return results[0]
         if results:
             not_pdf = filter(lambda r: r[1]!='pdf', results)
             if not_pdf:
                 raise osv.except_osv(_('Error!'), _('Unsupported combination of formats!'))
             #if results[0][1]=='pdf':
             output = PdfFileWriter()
             for r in results:
                 reader = PdfFileReader(StringIO(r[0]))
                 for page in range(reader.getNumPages()):
                     output.addPage(reader.getPage(page))
             s = StringIO()
             output.write(s)
             return s.getvalue(), results[0][1]
Exemplo n.º 5
0
 def create_source_pdf(self, cr, uid, ids, data, report_xml, context=None):
     if not context:
         context={}
     pool = pooler.get_pool(cr.dbname)
     attach = report_xml.attachment
     aeroo_ooo = _aeroo_ooo_test(cr) # Detect report_aeroo_ooo module
     context['aeroo_ooo'] = aeroo_ooo
     if attach or aeroo_ooo and report_xml.process_sep:
         objs = self.getObjects(cr, uid, ids, context)
         results = []
         for obj in objs:
             aname = attach and eval(attach, {'object':obj, 'time':time}) or False
             result = False
             if report_xml.attachment_use and aname and context.get('attachment_use', True):
                 #aids = pool.get('ir.attachment').search(cr, uid, [('datas_fname','=',aname+'.pdf'),('res_model','=',self.table),('res_id','=',obj.id)])
                 #if aids:
                 #    brow_rec = pool.get('ir.attachment').browse(cr, uid, aids[0])
                 #    if not brow_rec.datas:
                 #        continue
                 #    d = base64.decodestring(brow_rec.datas)
                 #    results.append((d,'pdf'))
                 #    continue
                 cr.execute("SELECT id, datas_fname FROM ir_attachment WHERE datas_fname ilike %s and res_model=%s and res_id=%s LIMIT 1", (aname+'.%',self.table,obj.id))
                 search_res = cr.dictfetchone()
                 if search_res:
                     brow_rec = pool.get('ir.attachment').browse(cr, uid, search_res['id'])
                     if not brow_rec.datas:
                         continue
                     d = base64.decodestring(brow_rec.datas)
                     extension = search_res['datas_fname'].split('.')[1]
                     results.append((d,extension))
                     continue
             result = self.create_single_pdf(cr, uid, [obj.id], data, report_xml, context)
             if not result:
                 return False
             try:
                 if attach and aname:
                     name = aname+'.'+result[1]
                     datas = base64.encodestring(result[0])
                     pool.get('ir.attachment').create(cr, uid, {
                         'name': aname,
                         'datas': datas,
                         'file_size': len(datas),
                         'datas_fname': name,
                         'res_model': self.table,
                         'res_id': obj.id,
                         }, context=context
                     )
                     cr.commit()
             except Exception,e:
                  tb_s = reduce(lambda x, y: x+y, traceback.format_exception(sys.exc_type, sys.exc_value, sys.exc_traceback))
                  netsvc.Logger().notifyChannel('report', netsvc.LOG_ERROR,str(e))
             results.append(result)
         if results:
             not_pdf = filter(lambda r: r[1]!='pdf', results)
             if not_pdf:
                 raise osv.except_osv(_('Error!'), _('Unsupported combination of formats!'))
             #if results[0][1]=='pdf':
             output = PdfFileWriter()
             for r in results:
                 reader = PdfFileReader(StringIO(r[0]))
                 for page in range(reader.getNumPages()):
                     output.addPage(reader.getPage(page))
             s = StringIO()
             output.write(s)
             return s.getvalue(), results[0][1]
Exemplo n.º 6
0
 def create_source_pdf(self, cr, uid, ids, data, report_xml, context=None):
     if not context:
         context = {}
     pool = pooler.get_pool(cr.dbname)
     attach = report_xml.attachment
     aeroo_ooo = aeroo_ooo_test(cr)  # Detect report_aeroo_ooo module
     context['aeroo_ooo'] = aeroo_ooo
     print_id = context.get('print_id', False)
     aeroo_print = self.active_prints[print_id]  # Aeroo print object
     if attach or aeroo_ooo and report_xml.process_sep:
         objs = self.getObjects_mod(cr, uid, ids, report_xml.report_type,
                                    context)
         deferred = context.get('deferred_process')
         results = []
         for obj in objs:
             aeroo_print.start_time = time.time()
             if deferred:
                 deferred.progress_update()
             aname = attach and eval(attach, {
                 'object': obj,
                 'time': time
             }) or False
             result = False
             if report_xml.attachment_use and aname and context.get(
                     'attachment_use', True):
                 # aids = pool.get('ir.attachment').search(cr, uid, [('datas_fname','=',aname+'.pdf'),('res_model','=',self.table),('res_id','=',obj.id)])
                 # if aids:
                 #    brow_rec = pool.get('ir.attachment').browse(cr, uid, aids[0])
                 #    if not brow_rec.datas:
                 #        continue
                 #    d = base64.decodestring(brow_rec.datas)
                 #    results.append((d,'pdf'))
                 #    continue
                 cr.execute(
                     "SELECT id, datas_fname FROM ir_attachment WHERE datas_fname ilike %s AND res_model=%s AND res_id=%s AND active LIMIT 1",
                     (aname + '.%', self.table, obj.id))
                 search_res = cr.dictfetchone()
                 if search_res:
                     brow_rec = pool.get('ir.attachment').browse(
                         cr, uid, search_res['id'], context)
                     if not brow_rec.datas:
                         continue
                     d = base64.decodestring(brow_rec.datas)
                     extension = search_res['datas_fname'].split('.')[1]
                     results.append((d, extension))
                     continue
             result = self.create_single_pdf(cr, uid, [obj.id], data,
                                             report_xml, context)
             if not result:
                 return False
             try:
                 if attach and aname:
                     name = aname + '.' + result[1]
                     datas = base64.encodestring(result[0])
                     ctx = dict(context)
                     ctx.pop('default_type', None)
                     pool.get('ir.attachment').create(
                         cr, uid, {
                             'name': aname,
                             'datas': datas,
                             'datas_fname': name,
                             'res_model': self.table,
                             'res_id': obj.id,
                             'type': 'binary'
                         })
                     cr.commit()
             except Exception, e:
                 tb_s = reduce(
                     lambda x, y: x + y,
                     traceback.format_exception(sys.exc_type, sys.exc_value,
                                                sys.exc_traceback))
                 _logger.error(_("Report generation error!") + '\n' + tb_s)
             results.append(result)
         if results and len(results) == 1:
             return results[0]
         if results:
             not_pdf = filter(lambda r: r[1] != 'pdf', results)
             if not_pdf:
                 raise osv.except_osv(
                     _('Error!'), _('Unsupported combination of formats!'))
             # if results[0][1]=='pdf':
             output = PdfFileWriter()
             for r in results:
                 reader = PdfFileReader(StringIO(r[0]))
                 for page in range(reader.getNumPages()):
                     output.addPage(reader.getPage(page))
             s = StringIO()
             output.write(s)
             return s.getvalue(), results[0][1]
    def create_with_attachment(self,
                               cr,
                               uid,
                               ids,
                               data,
                               original_doc,
                               context=None):
        import base64
        from report.pyPdf import PdfFileReader, PdfFileWriter
        o = PdfFileWriter()

        pool = pooler.get_pool(cr.dbname)
        quizz_id = ids[0]

        if data.get('model', '') == 'training.participation':
            participation = pool.get('training.participation').browse(
                cr, uid, data['id'])
            quizz_id = participation.questionnaire_id.id

        quizz_proxy = pool.get('training.exam.questionnaire')
        quizz = quizz_proxy.browse(cr, uid, quizz_id)

        def write_orig_document():
            r = PdfFileReader(StringIO.StringIO(original_doc[0]))
            for p in range(r.getNumPages()):
                o.addPage(r.getPage(p))

        def generate_watermark_pdf(docid, basenum, npages):
            watermark_report = netsvc.LocalService(
                'report.training.participation.watermark.qrcode')
            report_ids = [
                '%s-%s%03d' % (docid, basenum, n + 1) for n in range(npages)
            ]
            (result, format) = watermark_report.create(cr,
                                                       uid,
                                                       report_ids, {},
                                                       context=context)
            return PdfFileReader(StringIO.StringIO(result))

        attach_count = 0
        orig_doc_written = False
        for attach in quizz.attachment_ids:
            if attach.position == 'after' and not orig_doc_written:
                write_orig_document()
                orig_doc_written = True

            # 1 = annex and store
            # 2 = detect qrcode but don't store in the DMS
            basenum = '%1s%02d' % (attach.store_in_dms and 1
                                   or 2, attach_count)

            attach_pdf_data = base64.decodestring(attach.attachment_id.datas)
            attach_pdf = PdfFileReader(StringIO.StringIO(attach_pdf_data))
            waterm_pdf = generate_watermark_pdf(ids[0], basenum,
                                                attach_pdf.getNumPages())

            for attach_page in range(attach_pdf.getNumPages()):
                docp = attach_pdf.getPage(attach_page)
                docp_wm = waterm_pdf.getPage(attach_page)
                docp.mergePage(docp_wm)
                o.addPage(docp)

            attach_count += 1
        if not orig_doc_written:
            write_orig_document()

        s = StringIO.StringIO()
        o.write(s)
        return (s.getvalue(), original_doc[1])