2fc58310e9ba4eabc6391e37fbe1237f17db78b8
[odoo/odoo.git] / addons / email_template / email_template_engines.py
1 # To change this template, choose Tools | Templates
2 # and open the template in the editor.
3 from osv import fields,osv
4 import pooler
5 import netsvc
6 import re
7
8 class email_template_engines(osv.osv):
9     _name = "email_template.engines"
10     _description = "Email Template Engine"
11
12 #    def __init__(self):
13 #        print "Started Engine"
14
15     def check(self):
16         print "Start self check"
17         
18     def strip_html(self,text):
19         #Removes HTML, Have to check if still relevent
20         if text:
21             def fixup(m):
22                 text = m.group(0)
23                 if text[:1] == "<":
24                     return "" # ignore tags
25                 if text[:2] == "&#":
26                     try:
27                         if text[:3] == "&#x":
28                             return unichr(int(text[3:-1], 16))
29                         else:
30                             return unichr(int(text[2:-1]))
31                     except ValueError:
32                         pass
33                 elif text[:1] == "&":
34                     import htmlentitydefs
35                     entity = htmlentitydefs.entitydefs.get(text[1:-1])
36                     if entity:
37                         if entity[:2] == "&#":
38                             try:
39                                 return unichr(int(entity[2:-1]))
40                             except ValueError:
41                                 pass
42                         else:
43                             return unicode(entity, "iso-8859-1")
44                 return text # leave as is
45             return re.sub("(?s)<[^>]*>|&#?\w+;", fixup, text)
46
47     def parsevalue(self,cr,uid,id,message,templateid,context):
48         #id: ID of the template's model's record to be used
49         #message: the complete text including placeholders
50         #templateid: the template id of the template
51         #context: TODO
52         #print cr,uid,id,message,templateid,context
53         if message:
54             logger = netsvc.Logger()
55             def merge(match):
56                 template = self.pool.get("email.template").browse(cr,uid,templateid,context)
57                 obj_pool = self.pool.get(template.object_name.model)
58                 obj = obj_pool.browse(cr, uid, id, context)
59                 exp = str(match.group()[2:-2]).strip()
60                 #print "level 1:",exp
61                 exp_spl = exp.split('/')
62                 #print "level 2:",exp_spl
63                 try:
64                     result = eval(exp_spl[0], {'object':obj,})
65                 except:
66                     result = "Rendering Error"
67                 #print "result:",result
68                 try:
69                     if result in (None, False):
70                         if len(exp_spl)>1:
71                             return exp_spl[1]
72                         else:
73                             return 'Not Available'
74                     return str(result)
75                 except:
76                     return "Rendering Error"
77             if message:
78                 com = re.compile('(\[\[.+?\]\])')
79                 retmessage = com.sub(merge, message)
80             else:
81                 retmessage=""
82             return retmessage
83
84 email_template_engines()