def generate_document_job(cr, uid, obj_id, context): pool = pooler.get_pool(cr.dbname) camp_doc_object = pool.get('dm.campaign.document') camp_doc = camp_doc_object.browse(cr, uid, obj_id) ms_id = camp_doc.mail_service_id type_id = pool.get('dm.campaign.document.type').search( cr, uid, [('code', '=', 'pdf')]) type_id = type_id and type_id[0] or '' sorting_name = '' offer_doc_obj = pool.get('dm.offer.document').browse( cr, uid, camp_doc.document_id.id) printer_tray_id = '' if offer_doc_obj.printer_tray_model_id: printer_tray_id = pool.get('dm.printer_tray').search( cr, uid, [('printer_tray_model_id', '=', offer_doc_obj.printer_tray_model_id.id)]) if printer_tray_id: printer_tray_id = printer_tray_id[0] printer_tray_obj = pool.get('dm.printer_tray').browse( cr, uid, printer_tray_id) printer_id = printer_tray_obj.printer_id.id if not printer_tray_id: printer_tray_id = ms_id.default_printer_tray_id.id printer_id = ms_id.default_printer_id.id camp_doc_object.write(cr, uid, [obj_id], { 'printer_tray_id': printer_tray_id, 'printer_id': printer_id }) if ms_id.sorting_rule_id.by_customer_country: sorting_name = str(camp_doc.address_id.country_id.id) if ms_id.sorting_rule_id.by_offer_step: v = str(camp_doc.document_id.step_id.id) sorting_name = sorting_name and sorting_name + '_' + v or v if ms_id.sorting_rule_id.by_page_qty: attach_obj = pool.get('ir.attachment') pattern = re.compile(r"/Count\s+(\d+)") attach_id = attach_obj.search( cr, uid, [('res_id', '=', camp_doc.id), ('res_model', '=', 'dm.campaign.document')]) if attach_id: attach = attach_obj.browse(cr, uid, attach_id[0]) datas = base64.decodestring(attach.datas) v = str(pattern.findall(datas) and pattern.findall(datas)[0] or 0) sorting_name = sorting_name and sorting_name + '_' + v or v if ms_id.sorting_rule_id.by_product: if 'workitem_id' in context: res = pool.get('dm.workitem').browse(cr, uid, context['workitem_id']) if res.sale_order_id: product = [ '%d:%s' % (line.product_id.id, line.product_uom_qty) for line in res.sale_order_id.order_line ] product.sort() v = '_'.join(product) sorting_name = sorting_name and sorting_name + '_' + v or v if ms_id.sorting_rule_id.by_trademark: if 'segment_id' in context: res = pool.get('dm.campaign.proposition.segment').browse( cr, uid, context['segment_id']) if res.campaign_id and res.campaign_id.trademark_id: v = str(res.campaign_id.trademark_id.id) sorting_name = sorting_name and sorting_name + '_' + v or v if ms_id.sorting_rule_id.by_dealer: if 'segment_id' in context: res = pool.get('dm.campaign.proposition.segment').browse( cr, uid, context['segment_id']) if res.campaign_id and res.campaign_id.dealer_id: v = str(res.campaign_id.dealer_id.id) sorting_name = sorting_name and sorting_name + '_' + v or v if sorting_name: camp_doc_job_obj = pool.get('dm.campaign.document.job') job_ids = camp_doc_job_obj.search( cr, uid, [('state', '=', 'pending'), ('sorting_rule_id', '=', ms_id.sorting_rule_id.id), ('sorting_name', '=', sorting_name)]) job_id = '' for j_id in camp_doc_job_obj.browse(cr, uid, job_ids): if not ms_id.sorting_rule_id.qty_limit or ms_id.sorting_rule_id.qty_limit == 0: job_id = j_id.id elif len(j_id.campaign_document_ids ) < ms_id.sorting_rule_id.qty_limit: job_id = j_id.id if not job_id: job_vals = { 'name': (camp_doc.segment_id and camp_doc.segment_id.name or '') + str(sorting_name), 'user_id': ms_id.user_id.id, 'sorting_rule_id': ms_id.sorting_rule_id.id, 'sorting_name': sorting_name, 'use_front_recap': ms_id.front_job_recap and True or False, 'bottom_job_recap': ms_id.bottom_job_recap and True or False, 'campaign_document_ids': [[4, obj_id]], } job_id = camp_doc_job_obj.create(cr, uid, job_vals) if ms_id.front_job_recap or ms_id.bottom_job_recap: camp_vals = { 'segment_id': camp_doc.segment_id.id or False, 'name': camp_doc.document_id.step_id.code + "_" + str(camp_doc.address_id.id), 'type_id': type_id, 'mail_service_id': ms_id.id, 'address_id': camp_doc.address_id.id, 'campaign_document_job': job_id, 'printer_tray_id': printer_tray_id, 'printer_id': printer_id, } if not 'address_id' in context: context['address_id'] = camp_doc.address_id.id if not 'document_id' in context: context['document_id'] = camp_doc.document_id.id if not 'store_document' in context: context['store_document'] = ms_id.store_document if not 'workitem_id' in context: context['workitem_id'] = camp_doc.workitem_id.id if not 'step_id' in context: context['step_id'] = camp_doc.workitem_id.step_id.id if not 'segment_id' in context: context['segment_id'] = camp_doc.workitem_id.segment_id.id # reports for the front and bottom job should be saved as # attachment of the campaign document ? if ms_id.front_job_recap: camp_vals['document_id'] = ms_id.front_job_recap.id new_camp_id = camp_doc_object.create(cr, uid, camp_vals) report_data = generate_openoffice_reports( cr, uid, 'pdf', ms_id.front_job_recap.id, new_camp_id, context) ms_status = pool.get('dm.workitem')._check_sysmsg( cr, uid, report_data, context) camp_doc_object.write( cr, uid, [new_camp_id], { 'state': ms_status['state'], 'error_msg': ms_status['msg'], }) if ms_id.bottom_job_recap: camp_vals['document_id'] = ms_id.bottom_job_recap.id new_camp_id = camp_doc_object.create(cr, uid, camp_vals) report_data = generate_openoffice_reports( cr, uid, 'pdf', ms_id.bottom_job_recap.id, new_camp_id, context) ms_status = pool.get('dm.workitem')._check_sysmsg( cr, uid, report_data, context) camp_doc_object.write( cr, uid, [new_camp_id], { 'state': ms_status['state'], 'error_msg': ms_status['msg'], }) else: camp_doc_job_obj.write(cr, uid, job_id, {'campaign_document_ids': [[4, obj_id]]}) return {'code': 'doc_done', 'ids': camp_doc.id}
def generate_document_job(cr, uid, obj_id, context): pool = pooler.get_pool(cr.dbname) camp_doc_object = pool.get('dm.campaign.document') camp_doc = camp_doc_object.browse(cr,uid,obj_id) ms_id = camp_doc.mail_service_id type_id = pool.get('dm.campaign.document.type').search(cr, uid, [('code', '=', 'pdf')]) type_id = type_id and type_id[0] or '' sorting_name = '' offer_doc_obj = pool.get('dm.offer.document').browse(cr,uid,camp_doc.document_id.id) printer_tray_id = '' if offer_doc_obj.printer_tray_model_id: printer_tray_id = pool.get('dm.printer_tray').search(cr, uid, [('printer_tray_model_id','=',offer_doc_obj.printer_tray_model_id.id)]) if printer_tray_id : printer_tray_id = printer_tray_id[0] printer_tray_obj = pool.get('dm.printer_tray').browse(cr, uid, printer_tray_id) printer_id = printer_tray_obj.printer_id.id if not printer_tray_id: printer_tray_id = ms_id.default_printer_tray_id.id printer_id = ms_id.default_printer_id.id camp_doc_object.write(cr, uid, [obj_id], {'printer_tray_id': printer_tray_id, 'printer_id': printer_id}) if ms_id.sorting_rule_id.by_customer_country: sorting_name = str(camp_doc.address_id.country_id.id) if ms_id.sorting_rule_id.by_offer_step: v = str(camp_doc.document_id.step_id.id) sorting_name = sorting_name and sorting_name+'_'+v or v if ms_id.sorting_rule_id.by_page_qty: attach_obj = pool.get('ir.attachment') pattern = re.compile(r"/Count\s+(\d+)") attach_id = attach_obj.search(cr, uid,[('res_id', '=', camp_doc.id), ('res_model', '=', 'dm.campaign.document')]) if attach_id: attach = attach_obj.browse(cr, uid, attach_id[0]) datas = base64.decodestring(attach.datas) v = str(pattern.findall(datas) and pattern.findall(datas)[0] or 0) sorting_name = sorting_name and sorting_name+'_'+v or v if ms_id.sorting_rule_id.by_product: if 'workitem_id' in context: res = pool.get('dm.workitem').browse(cr,uid,context['workitem_id']) if res.sale_order_id: product =['%d:%s'%(line.product_id.id,line.product_uom_qty) for line in res.sale_order_id.order_line] product.sort() v = '_'.join(product) sorting_name = sorting_name and sorting_name+'_'+v or v if ms_id.sorting_rule_id.by_trademark: if 'segment_id' in context: res = pool.get('dm.campaign.proposition.segment').browse(cr,uid,context['segment_id']) if res.campaign_id and res.campaign_id.trademark_id: v = str(res.campaign_id.trademark_id.id) sorting_name = sorting_name and sorting_name+'_'+v or v if ms_id.sorting_rule_id.by_dealer: if 'segment_id' in context: res = pool.get('dm.campaign.proposition.segment').browse(cr,uid,context['segment_id']) if res.campaign_id and res.campaign_id.dealer_id: v = str(res.campaign_id.dealer_id.id) sorting_name = sorting_name and sorting_name+'_'+v or v if sorting_name: camp_doc_job_obj = pool.get('dm.campaign.document.job') job_ids = camp_doc_job_obj.search(cr, uid, [('state', '=', 'pending'),('sorting_rule_id', '=', ms_id.sorting_rule_id.id),('sorting_name','=',sorting_name)]) job_id = '' for j_id in camp_doc_job_obj.browse(cr, uid, job_ids): if not ms_id.sorting_rule_id.qty_limit or ms_id.sorting_rule_id.qty_limit ==0 : job_id = j_id.id elif len(j_id.campaign_document_ids) < ms_id.sorting_rule_id.qty_limit: job_id = j_id.id if not job_id : job_vals = { 'name': (camp_doc.segment_id and camp_doc.segment_id.name or '') + str(sorting_name), 'user_id': ms_id.user_id.id, 'sorting_rule_id': ms_id.sorting_rule_id.id, 'sorting_name': sorting_name, 'use_front_recap': ms_id.front_job_recap and True or False, 'bottom_job_recap': ms_id.bottom_job_recap and True or False, 'campaign_document_ids': [[4,obj_id]], } job_id = camp_doc_job_obj.create(cr,uid,job_vals) if ms_id.front_job_recap or ms_id.bottom_job_recap: camp_vals={ 'segment_id': camp_doc.segment_id.id or False, 'name': camp_doc.document_id.step_id.code + "_" + str(camp_doc.address_id.id), 'type_id': type_id, 'mail_service_id': ms_id.id, 'address_id': camp_doc.address_id.id, 'campaign_document_job': job_id, 'printer_tray_id': printer_tray_id, 'printer_id': printer_id, } if not 'address_id' in context : context['address_id'] = camp_doc.address_id.id if not 'document_id' in context : context['document_id'] = camp_doc.document_id.id if not 'store_document' in context : context['store_document'] = ms_id.store_document if not 'workitem_id' in context : context['workitem_id'] = camp_doc.workitem_id.id if not 'step_id' in context : context['step_id'] = camp_doc.workitem_id.step_id.id if not 'segment_id' in context : context['segment_id'] = camp_doc.workitem_id.segment_id.id # reports for the front and bottom job should be saved as # attachment of the campaign document ? if ms_id.front_job_recap: camp_vals['document_id'] = ms_id.front_job_recap.id new_camp_id = camp_doc_object.create(cr,uid,camp_vals) report_data = generate_openoffice_reports(cr, uid, 'pdf', ms_id.front_job_recap.id, new_camp_id, context) ms_status = pool.get('dm.workitem')._check_sysmsg(cr, uid, report_data, context) camp_doc_object.write(cr, uid, [new_camp_id], {'state': ms_status['state'], 'error_msg': ms_status['msg'],}) if ms_id.bottom_job_recap: camp_vals['document_id'] = ms_id.bottom_job_recap.id new_camp_id = camp_doc_object.create(cr,uid,camp_vals) report_data = generate_openoffice_reports(cr, uid, 'pdf', ms_id.bottom_job_recap.id, new_camp_id, context) ms_status = pool.get('dm.workitem')._check_sysmsg(cr, uid, report_data, context) camp_doc_object.write(cr, uid, [new_camp_id], {'state': ms_status['state'], 'error_msg': ms_status['msg'],}) else: camp_doc_job_obj.write(cr, uid, job_id, {'campaign_document_ids': [[4,obj_id]]}) return {'code':'doc_done','ids': camp_doc.id}
def send_email(cr, uid, obj, context): """ Set context values """ context['workitem_id'] = obj.workitem_id.id context['address_id'] = obj.workitem_id.address_id.id context['step_id'] = obj.workitem_id.step_id.id context['document_id'] = obj.document_id.id context['segment_id'] = obj.workitem_id.segment_id.id """ Get Emailmvision connection infos """ ev_host = obj.mail_service_id.ev_host ev_service = obj.mail_service_id.ev_service if obj.document_id.ev_template: ev_encrypt = obj.document_id.ev_template.ev_encrypt ev_random = obj.document_id.ev_template.ev_random else: ev_encrypt = obj.mail_service_id.ev_template.ev_encrypt ev_random = obj.mail_service_id.ev_template.ev_random email_dest = obj.address_id.email or '' email_reply = obj.segment_id.campaign_id.trademark_id.email or '' email_subject = merge_message(cr, uid, obj.document_id.subject or '', context) name_from = obj.segment_id.campaign_id.trademark_id.name or '' name_reply = obj.segment_id.campaign_id.trademark_id.name or '' pool = pooler.get_pool(cr.dbname) message = [] if obj.mail_service_id.store_document: ir_att_obj = pool.get('ir.attachment') ir_att_ids = ir_att_obj.search(cr, uid, [ ('res_model','=','dm.campaign.document'), ('res_id','=',obj.id), ('file_type','=','html')]) for attach in ir_att_obj.browse(cr, uid, ir_att_ids): message.append(base64.decodestring(attach.datas)) else: if obj.document_id.editor == 'internal': msg = generate_internal_reports(cr, uid, 'html2html', \ obj.document_id.id, False, context) elif obj.document_id.editor == 'oord': msg = generate_openoffice_reports(cr, uid, 'html2html', \ obj.document_id.id, False, context) if msg in ('plugin_error','plugin_missing','wrong_report_type') : return {'code':msg,'ids':[obj.id]} message.extend(msg) for msg in message: root = etree.HTML(msg) body = root.find('body') html_content = ''.join([ etree.tostring(x) for x in body.getchildren()]) text_content = "html content only" ''' Composing XML ''' msg = etree.Element("MultiSendRequest") sendrequest = etree.SubElement(msg, "sendrequest") dyn = etree.SubElement(sendrequest, "dyn") dynentry1 = etree.SubElement(dyn, "entry") dynkey1 = etree.SubElement(dynentry1, "key") dynkey1.text = "EMAIL_DEST" dynvalue1 = etree.SubElement(dynentry1, "value") dynvalue1.text = email_dest dynentry2 = etree.SubElement(dyn, "entry") dynkey2 = etree.SubElement(dynentry2, "key") dynkey2.text = "SUBJECT" dynvalue2 = etree.SubElement(dynentry2, "value") dynvalue2.text = email_subject dynentry3 = etree.SubElement(dyn, "entry") dynkey3 = etree.SubElement(dynentry3, "key") dynkey3.text = "EMAIL_REPLY" dynvalue3 = etree.SubElement(dynentry3, "value") dynvalue3.text = email_reply dynentry4 = etree.SubElement(dyn, "entry") dynkey4 = etree.SubElement(dynentry4, "key") dynkey4.text = "NAME_FROM" dynvalue4 = etree.SubElement(dynentry4, "value") dynvalue4.text = name_from dynentry5 = etree.SubElement(dyn, "entry") dynkey5 = etree.SubElement(dynentry5, "key") dynkey5.text = "NAME_REPLY" dynvalue5 = etree.SubElement(dynentry5, "value") dynvalue5.text = name_reply content = etree.SubElement(sendrequest, "content") entry1 = etree.SubElement(content, "entry") key1 = etree.SubElement(entry1, "key") key1.text = "1" value1 = etree.SubElement(entry1, "value") value1.text = etree.CDATA(html_content) entry2 = etree.SubElement(content, "entry") key2 = etree.SubElement(entry2, "key") key2.text = "2" value2 = etree.SubElement(entry2, "value") value2.text = text_content email = etree.SubElement(sendrequest, "email") email.text = email_dest encrypt = etree.SubElement(sendrequest, "encrypt") encrypt.text = ev_encrypt random = etree.SubElement(sendrequest, "random") random.text = ev_random senddate = etree.SubElement(sendrequest, "senddate") senddate.text = time.strftime('%Y-%m-%dT%H:%M:%S') synchrotype = etree.SubElement(sendrequest, "synchrotype") synchrotype.text = "NOTHING" # print("<?xml version=\"1.0\" encoding=\"UTF-8\"?>" + "\n" + etree.tostring(msg, method="xml", encoding='utf-8', pretty_print=True)) xml_msg = "<?xml version=\"1.0\" encoding=\"UTF-8\"?>" xml_msg += etree.tostring(msg, encoding="utf-8") ''' Sending to Emailvision NMSXML API ''' try: ev_api = httplib.HTTP( ev_host +":80") ev_api.putrequest("POST", "/" + ev_service) ev_api.putheader("Content-type", "text/xml; charset=\"UTF-8\"") ev_api.putheader("Content-length", str(len(xml_msg))) ev_api.endheaders() ev_api.send(xml_msg) ''' Get Emailvision Reply ''' statuscode, statusmessage, header = ev_api.getreply() res = ev_api.getfile().read() except: return {'code':'emv_no_connect'} if statuscode != 200: error_msg = ["This document cannot be sent to Emailvision NMS API " ,"Status Code : " + str(statuscode) ,"Status Message : " + statusmessage ,"Header : " + str(header) ,"Result : " + res] error_msg = '\n'.join(error_msg) return {'code':'emv_doc_error'} return {'code':'emv_doc_sent'}