Browse files

[ADD]Added server action for power email

  • Loading branch information...
1 parent c276ee0 commit 570e59342dc2ffae773d8a8d9e8b8e2205a862f4 Sharoon Thomas committed Aug 4, 2009
Showing with 65 additions and 6 deletions.
  1. +2 −1 __init__.py
  2. +1 −0 __terp__.py
  3. +3 −2 poweremail_core.py
  4. +2 −2 poweremail_engines.py
  5. +1 −0 poweremail_mailbox.py
  6. +56 −1 poweremail_template.py
View
3 __init__.py
@@ -26,4 +26,5 @@
import poweremail_core
import poweremail_template
import poweremail_send_wizard
-import poweremail_mailbox
+import poweremail_mailbox
+import poweremail_serveraction
View
1 __terp__.py
@@ -43,6 +43,7 @@
'poweremail_template_view.xml',
'poweremail_send_wizard.xml',
'poweremail_mailbox_view.xml',
+ 'poweremail_serveraction_view.xml'
],
"demo_xml" : ['poweremail_data.xml'],
"installable": True,
View
5 poweremail_core.py
@@ -211,8 +211,9 @@ def send_mail(self,cr,uid,ids,to=[],cc=[],bcc=[],subject="",body_text="",body_ht
if to:
while (type(to)==type([])) and (False in to):
to = to.remove(False)
- msg['To'] = ",".join(map(str,to))
- toadds = to
+ if (type(to)==type([])):
+ msg['To'] = ",".join(map(str,to))
+ toadds = to
if cc:
while (type(cc)==type([])) and (False in cc):
cc = cc.remove(False)
View
4 poweremail_engines.py
@@ -45,9 +45,9 @@ def fixup(m):
return re.sub("(?s)<[^>]*>|&#?\w+;", fixup, text)
def parsevalue(self,cr,uid,id,message,templateid,context):
- #id: ID of the template's MODEL record to be used
+ #id: ID of the template's model's record to be used
#message: the complete text including placeholders
- #template: the template id of the template
+ #templateid: the template id of the template
#context: TODO
#print cr,uid,id,message,templateid,context
if message:
View
1 poweremail_mailbox.py
@@ -25,6 +25,7 @@
from osv import osv, fields
import time
+import poweremail_engines
class poweremail_mailbox(osv.osv):
_name="poweremail.mailbox"
View
57 poweremail_template.py
@@ -24,7 +24,6 @@
#########################################################################
from osv import osv, fields
import netsvc
-import re
import poweremail_engines
class poweremail_templates(osv.osv):
@@ -170,6 +169,62 @@ def _auto_compute(self,cr,uid,ids,model_object_field,sub_model_object_field,null
#self.write(cr,uid,ids,{'copyvalue':obj_not})
return {'value':{'copyvalue':obj_not}}
+ def get_value(self,cr,uid,recid,message={},template=None):
+ if message:
+ return self.engine.parsevalue(cr,uid,recid,message,template,{})
+ else:
+ return ""
+
+ def generate_mail(self,cr,uid,id,recid,context={}):
+ #Generates an email an saves to outbox given the template id & record ID of a record in template's model
+ #id: ID of template to be used
+ #recid: record id for the mail
+ #Context: not currently used
+ logger = netsvc.Logger()
+ try:
+ self.engine = self.pool.get("poweremail.engines")
+ template = self.browse(cr,uid,id)
+ vals = {
+ 'pem_from': template.object_name.model,
+ 'pem_to':self.get_value(cr,uid,recid,template.def_to,template.id),
+ 'pem_cc':self.get_value(cr,uid,recid,template.def_cc,template.id),
+ 'pem_bcc':self.get_value(cr,uid,recid,template.def_bcc,template.id),
+ 'pem_subject':self.get_value(cr,uid,recid,template.def_subject,template.id),
+ 'pem_body_text':self.get_value(cr,uid,recid,template.def_body_text,template.id),
+ 'pem_body_html':self.get_value(cr,uid,recid,template.def_body_html,template.id),
+ 'pem_account_id' :template.enforce_from_account,#This is a mandatory field when automatic emails are sent
+ 'state':'na',
+ 'mail_type':'multipart/alternative' #Options:'multipart/mixed','multipart/alternative','text/plain','text/html'
+ }
+ if template.use_sign:
+ sign = self.pool.get('res.users').read(cr,uid,uid,['signature'])['signature']
+ if vals['pem_body_text']:
+ vals['pem_body_text']+=sign
+ if vals['pem_body_html']:
+ vals['pem_body_html']+=sign
+ #Create partly the mail and later update attachments
+ mail_id = self.pool.get('poweremail.mailbox').create(cr,uid,vals)
+ if template.report_template:
+ reportname = 'report.' + self.pool.get('ir.actions.report.xml').read(cr,uid,template.report_template.id,['report_name'])['report_name']
+ service = netsvc.LocalService(reportname)
+ (result, format) = service.create(cr, uid, [id], {}, {})
+ att_obj = self.pool.get('ir.attachment')
+ new_att_vals={
+ 'name':vals['pem_subject'] + ' (Email Attachment)',
+ 'datas':base64.b64encode(result),
+ 'datas_fname':str(self.get_value(template.file_name) or 'Report') + "." + format,
+ 'description':vals['pem_body_text'] or "No Description",
+ 'res_model':'poweremail.mailbox',
+ 'res_id':mail_id
+ }
+ attid = att_obj.create(cr,uid,new_att_vals)
+ if attid:
+ self.pool.get('poweremail.mailbox').write(cr,uid,mail_id,{'pem_attachments_ids':[[6, 0, [attid]]],'mail_type':'multipart/mixed','folder':'outbox'})
+ return mail_id
+ except Exception,error:
+ logger.notifyChannel(_("Power Email"), netsvc.LOG_ERROR, _("Email Generation failed, Reason:%s")% (error))
+ return False
+
poweremail_templates()
class poweremail_preview(osv.osv_memory):

0 comments on commit 570e593

Please sign in to comment.