~zaber/openobject-addons/zaber-custom

« back to all changes in this revision

Viewing changes to email_whitelist/email_whitelist.py

  • Committer: Amy Tsai
  • Date: 2013-07-25 00:02:53 UTC
  • Revision ID: amy@zaber.com-20130725000253-uu803qjzg9wzc8bt
new email whitelist module, improved sanity checks and interface

email whitelist - checks all emails with whitelist before sending
inventory processing - added last run date to each runs
partner - fixed suppliers without contact
purchase - changed the threshold for old dates in active orphaned stock moves

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
from dateutil.relativedelta import relativedelta
 
2
from osv import fields,osv,orm
 
3
from tools import config
 
4
from tools.translate import _
 
5
import netsvc
 
6
 
 
7
AVAILABLE_EMAIL_FIELDS = [
 
8
    ('rcp', 'Receiver'),
 
9
    ('subject', 'Subject'),
 
10
    ('body', 'Body')
 
11
]
 
12
 
 
13
OPERATORS = [
 
14
    ('isEmpty',  'is Empty'),
 
15
    ('isNotEmpty', 'is not Empty'),
 
16
    ('contains', 'contains'),
 
17
    ('notContains', 'doesn\'t contain'),
 
18
    ('endsWith', 'ends with'),
 
19
    ('startsWith', 'starts with')
 
20
]
 
21
 
 
22
class mail_message(osv.osv):
 
23
    _inherit = "mail.message"
 
24
    
 
25
    def send(self, cr, uid, ids, auto_commit=False, context=None):
 
26
        ew_obj = self.pool.get('email.whitelist')
 
27
        ew_ids = list()
 
28
        for message in self.browse(cr, uid, ids, context=context):
 
29
            if ew_obj.check_whitelist(cr, uid, ids, msg=message):
 
30
            # email passed whitelist, add it to send list
 
31
                ew_ids.append(message['id'])
 
32
            else:
 
33
            # email contain un-whitelist items, cancel it
 
34
                message.write({'state':'cancel'})
 
35
        return super(mail_message,self).send(cr, uid, ew_ids, auto_commit, context)
 
36
 
 
37
 
 
38
class email_whitelist(osv.osv):
 
39
    _name = 'email.whitelist'
 
40
    _columns = {
 
41
      'field': fields.selection(AVAILABLE_EMAIL_FIELDS, 'Field'),
 
42
      'op': fields.selection(OPERATORS, 'Comparator'),
 
43
      'value': fields.text('Value')
 
44
    }
 
45
    
 
46
    def check_whitelist(self, cr, uid, ids, context=None, msg=None):
 
47
        """
 
48
        Parses the user inputs and return a true or false
 
49
        if the email passes the whitelist or not.
 
50
        """
 
51
        
 
52
        if context is None:
 
53
            context = {}
 
54
        ew_ids = self.search(cr, uid, [])
 
55
        if not ew_ids:
 
56
            # No whitelists, send nothing
 
57
            return False
 
58
            
 
59
        # starting whitelists checking"
 
60
        ew_msg = dict()  
 
61
        
 
62
        for ew in self.browse(cr,uid,ew_ids,context):
 
63
            if ew.field != "rcp":
 
64
                if ew.field == "body":
 
65
                    if msg.body_text:
 
66
                        param = "body_text"
 
67
                    else:
 
68
                        param = "body_html"
 
69
                else:
 
70
                    param = ew.field
 
71
                if eval('self.'+ew.op+'("""'+msg[param]+'""", "'.strip()+ew.value+'")'):
 
72
                    continue
 
73
                else:
 
74
                    return False
 
75
            else:
 
76
                tags = ['email_to', 'email_cc', 'email_bcc', 'reply_to']
 
77
                for tag in tags:
 
78
                    rcps = list()
 
79
                    for r in msg[tag].split(','):
 
80
                        if not eval('self.'+ew.op+'("'+r.strip()+'", "'+ew.value+'")'):
 
81
                        # found recipient not in whitelist, cancel email
 
82
                            return False
 
83
        return True
 
84
        
 
85
    def isEmpty(self, string, value):
 
86
        if string:
 
87
            return False
 
88
        else: 
 
89
            return True
 
90
            
 
91
    def isNotEmpty(self, string, value):
 
92
        return not self.isEmpty(string, value)
 
93
            
 
94
    def contains(self, string, value):
 
95
        if string.lower().find(value.lower()) > -1:
 
96
            return True
 
97
        else: 
 
98
            return False
 
99
            
 
100
    def notContains(self, string, value):
 
101
        return not self.contains(string, value)
 
102
            
 
103
    def endsWith(self, string, value):
 
104
        return string.lower().endswith(value.lower())
 
105
        
 
106
    def startsWith(self, string, value):
 
107
        return string.lower().startswith(value.lower())
 
108
        
 
109
email_whitelist()