~openerp-commiter/openobject-addons/trunk-extra-addons

« back to all changes in this revision

Viewing changes to partner_spam/wizard/wizard_sms.py

  • Committer: Fabien Pinckaers
  • Date: 2008-11-12 06:43:12 UTC
  • Revision ID: fp@tinyerp.com-20081112064312-fp85io97i1e95tuz
moved

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
##############################################################################
 
2
#
 
3
# Copyright (c) 2004-2006 TINY SPRL. (http://tiny.be) All Rights Reserved.
 
4
#                    Fabien Pinckaers <fp@tiny.Be>
 
5
# Copyright (c) 2008 Zikzakmedia S.L. (http://zikzakmedia.com) All Rights Reserved.
 
6
#                    Jordi Esteve <jesteve@zikzakmedia.com>
 
7
#
 
8
# WARNING: This program as such is intended to be used by professional
 
9
# programmers who take the whole responsability of assessing all potential
 
10
# consequences resulting from its eventual inadequacies and bugs
 
11
# End users who are looking for a ready-to-use solution with commercial
 
12
# garantees and support are strongly adviced to contract a Free Software
 
13
# Service Company
 
14
#
 
15
# This program is Free Software; you can redistribute it and/or
 
16
# modify it under the terms of the GNU General Public License
 
17
# as published by the Free Software Foundation; either version 2
 
18
# of the License, or (at your option) any later version.
 
19
#
 
20
# This program is distributed in the hope that it will be useful,
 
21
# but WITHOUT ANY WARRANTY; without even the implied warranty of
 
22
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 
23
# GNU General Public License for more details.
 
24
#
 
25
# You should have received a copy of the GNU General Public License
 
26
# along with this program; if not, write to the Free Software
 
27
# Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111-1307, USA.
 
28
#
 
29
##############################################################################
 
30
 
 
31
import wizard
 
32
import pooler
 
33
import tools
 
34
 
 
35
sms_send_form = '''<?xml version="1.0"?>
 
36
<form string="%s">
 
37
        <separator string="%s" colspan="4"/>
 
38
        <field name="app_id"/>
 
39
        <newline/>
 
40
        <field name="user"/>
 
41
        <field name="password"/>
 
42
        <newline/>
 
43
        <field name="email_http"/>
 
44
        <field name="email"/>
 
45
        <separator colspan="4"/>
 
46
        <label string="Message:"/>
 
47
        <field name="default"/>
 
48
        <field name="text" nolabel="1" colspan="4"/>
 
49
 
 
50
        <separator string="The following tags can be included in the message. They will be replaced to the corresponding values of each partner contact:" colspan="4"/>
 
51
        <label string="[[partner_id]] -> Partner name" colspan="2"/>
 
52
        <label string="[[name]] -> Contact name" colspan="2"/>
 
53
        <label string="[[function]] -> Function" colspan="2"/>
 
54
        <label string="[[title]] -> Title" colspan="2"/>
 
55
        <label string="[[street]] -> Street" colspan="2"/>
 
56
        <label string="[[street2]] -> Street 2" colspan="2"/>
 
57
        <label string="[[zip]] -> Zip code" colspan="2"/>
 
58
        <label string="[[city]] -> City" colspan="2"/>
 
59
        <label string="[[state_id]] -> State" colspan="2"/>
 
60
        <label string="[[country_id]] -> Country" colspan="2"/>
 
61
        <label string="[[email]] -> Email" colspan="2"/>
 
62
        <label string="[[phone]] -> Phone" colspan="2"/>
 
63
        <label string="[[fax]] -> Fax" colspan="2"/>
 
64
        <label string="[[mobile]] -> Mobile" colspan="2"/>
 
65
        <label string="[[birthdate]] -> Birthday" colspan="2"/>
 
66
</form>''' % ('SMS - Gateway: clickatell','Bulk SMS send')
 
67
 
 
68
sms_send_fields = {
 
69
        'app_id': {'string':'API ID', 'type':'char', 'required':True},
 
70
        'user': {'string':'Login', 'type':'char', 'required':True},
 
71
        'password': {'string':'Password', 'type':'char', 'required':True},
 
72
        'email_http': {'string':'Email connection', 'type':'boolean', 'help':'HTTP or Email connection to clickatell gateway'},
 
73
        'email': {'string':"Sender's email", 'type':'char', 'help':'Only required for Email connection to clickatell gateway', 'size':128},
 
74
        'default': {'string':'Only send to default addresses', 'type':'boolean'},
 
75
        'text': {'string':'SMS Message', 'type':'text', 'required':True}
 
76
}
 
77
 
 
78
sms_done_form = '''<?xml version="1.0"?>
 
79
<form string="SMS">
 
80
        <field name="sms_sent"/>
 
81
</form>'''
 
82
 
 
83
sms_done_fields = {
 
84
        'sms_sent': {'string':'Quantity of SMS sent', 'type':'integer', 'readonly': True},
 
85
}
 
86
 
 
87
 
 
88
def _sms_send(cr, uid, data, context, adr):
 
89
        import re
 
90
        # change the [[field]] tags with the partner address values
 
91
        pattern = re.compile('\[\[\S+\]\]')
 
92
        fields = pattern.findall(data['form']['text'])
 
93
        texts = []
 
94
        for field in fields:
 
95
                text = getattr(adr, field[2:-2])
 
96
                if text and field[-5:-2]=='_id': # State or country
 
97
                        text = text.name
 
98
                texts.append(text or '')
 
99
        sms = pattern.sub('%s', data['form']['text'])
 
100
        sms = sms % tuple(texts)
 
101
        #print sms
 
102
 
 
103
        to = adr.mobile
 
104
        to = to.replace(' ','')
 
105
        if adr.country_id and adr.country_id.code in ('ES', 'CT') and to[:1] == '6': # Spain mobiles begin with 6
 
106
                to = '34'+to
 
107
        sms = ' '.join( sms.split('\n') ) # Conversion of '\n' to ' ' is necessary
 
108
        sms_sent = unicode(sms, 'utf-8').encode('latin1')
 
109
        if data['form']['email_http']:
 
110
                # Connection by Email
 
111
                text = 'user:' + data['form']['user'] + '\npassword:' + data['form']['password'] + '\napi_id:' + data['form']['app_id'] + '\nto:' + to + '\ntext:' + sms_sent
 
112
                #print text
 
113
                tools.email_send(data['form']['email'], ['sms@messaging.clickatell.com'], '', text)
 
114
        else:
 
115
                # Connection by http
 
116
                tools.sms_send(data['form']['user'], data['form']['password'], data['form']['app_id'], sms_sent, to)
 
117
 
 
118
        # Add a partner event
 
119
        c_id = pooler.get_pool(cr.dbname).get('res.partner.canal').search(cr ,uid, [('name','ilike','SMS'),('active','=',True)])
 
120
        c_id = c_id and c_id[0] or False
 
121
        pooler.get_pool(cr.dbname).get('res.partner.event').create(cr, uid,
 
122
                        {'name': 'SMS sent to ' + adr.mobile,
 
123
                         'partner_id': adr.partner_id.id,
 
124
                         'description': sms,
 
125
                         'canal_id': c_id,
 
126
                         'user_id': uid, })
 
127
 
 
128
 
 
129
def _sms_send_partner(self, cr, uid, data, context):
 
130
        nbr = 0
 
131
        criteria = [('partner_id','in',data['ids'])]
 
132
        if data['form']['default']: # Only default addresses
 
133
                criteria.append(('type','=','default'))
 
134
        adr_ids = pooler.get_pool(cr.dbname).get('res.partner.address').search(cr ,uid, criteria)
 
135
        addresses = pooler.get_pool(cr.dbname).get('res.partner.address').browse(cr, uid, adr_ids, context)
 
136
        for adr in addresses:
 
137
                if adr.mobile:
 
138
                        _sms_send(cr, uid, data, context, adr)
 
139
                        nbr += 1
 
140
        return {'sms_sent': nbr}
 
141
 
 
142
class part_sms(wizard.interface):
 
143
        states = {
 
144
                'init': {
 
145
                        'actions': [],
 
146
                        'result': {'type': 'form', 'arch':sms_send_form, 'fields': sms_send_fields, 'state':[('end','Cancel'), ('send','Send SMS')]}
 
147
                },
 
148
                'send': {
 
149
                        'actions': [_sms_send_partner],
 
150
                        'result': {'type': 'form', 'arch': sms_done_form, 'fields': sms_done_fields, 'state': [('end', 'End')] }
 
151
                }
 
152
        }
 
153
part_sms('res.partner.sms_send_2')
 
154
 
 
155
 
 
156
def _sms_send_partner_address(self, cr, uid, data, context):
 
157
        nbr = 0
 
158
        criteria = [('id','in',data['ids'])]
 
159
        if data['form']['default']: # Only default addresses
 
160
                criteria.append(('type','=','default'))
 
161
        adr_ids = pooler.get_pool(cr.dbname).get('res.partner.address').search(cr ,uid, criteria)
 
162
        addresses = pooler.get_pool(cr.dbname).get('res.partner.address').browse(cr, uid, adr_ids, context)
 
163
        for adr in addresses:
 
164
                if adr.mobile:
 
165
                        _sms_send(cr, uid, data, context, adr)
 
166
                        nbr += 1
 
167
        return {'sms_sent': nbr}
 
168
 
 
169
class part_sms_partner_address(wizard.interface):
 
170
        states = {
 
171
                'init': {
 
172
                        'actions': [],
 
173
                        'result': {'type': 'form', 'arch':sms_send_form, 'fields': sms_send_fields, 'state':[('end','Cancel'), ('send','Send SMS')]}
 
174
                },
 
175
                'send': {
 
176
                        'actions': [_sms_send_partner_address],
 
177
                        'result': {'type': 'form', 'arch': sms_done_form, 'fields': sms_done_fields, 'state': [('end', 'End')] }
 
178
                }
 
179
        }
 
180
part_sms_partner_address('res.partner.address.sms_send')
 
 
b'\\ No newline at end of file'