[IMP] access rights improvements
[odoo/odoo.git] / addons / marketing_campaign_mailchimp / marketing_campaign_mailchimp.py
1 # -*- coding: utf-8 -*-
2 ##############################################################################
3 #
4 #    OpenERP, Open Source Management Solution
5 #    Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
6 #
7 #    This program is free software: you can redistribute it and/or modify
8 #    it under the terms of the GNU Affero General Public License as
9 #    published by the Free Software Foundation, either version 3 of the
10 #    License, or (at your option) any later version.
11 #
12 #    This program is distributed in the hope that it will be useful,
13 #    but WITHOUT ANY WARRANTY; without even the implied warranty of
14 #    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15 #    GNU Affero General Public License for more details.
16 #
17 #    You should have received a copy of the GNU Affero General Public License
18 #    along with this program.  If not, see <http://www.gnu.org/licenses/>.
19 #
20 ##############################################################################
21
22 from osv import fields, osv
23
24 import urllib2
25 import simplejson as json
26
27 mailchimp_url = 'http://%s.api.mailchimp.com/1.2'
28
29 class marketing_campaign_mailchimp_account(osv.osv): #{{{
30     _name = "marketing.campaign.mailchimp.account"
31     
32     _columns = {
33             'name': fields.char('Account Name', size=64),
34             'username': fields.char('Username', size=64, required=True),
35             'password': fields.char('Password', size=64, required=True),
36             'apikey': fields.char('API Key', size=128),
37             'data_center': fields.selection([('us1', 'US1'), ('us2', 'US2'), 
38                                             ('uk1', 'UK1')], 'Data Center'),
39             'state': fields.selection([('draft', 'Draft'), ('approved', 'Approved'), 
40                            ('cancelled', 'Cancelled')], 'State', readonly=True)
41         }
42     
43     _defaults = {
44             'state': lambda *a: 'draft'
45         }
46     
47     def get_response(self, cr, uid, mailchimp_id, method, params={}):
48         mailchimp_account = self.browse(cr, uid, mailchimp_id)
49         params['output'] = 'json'
50         if method == 'login':
51                 params['username'] = mailchimp_account.username
52                 params['password'] = mailchimp_account.password
53         else :
54                 params['apikey'] = mailchimp_account.apikey
55         params = '&'.join(map(lambda x : '%s=%s' %(x, params[x]), params))
56         url = mailchimp_url%mailchimp_account.data_center+ '/?method=%s'%method
57         req = urllib2.Request(url, params)
58         handle = urllib2.urlopen(req)
59         response = json.loads(handle.read())
60         return response 
61     
62     def button_approve(self, cr, uid, ids, context):
63         acc_obj = self.browse(cr, uid, ids)[0]
64         vals = {}
65         if not acc_obj.apikey:
66             method = 'login'
67         else:
68             method = 'ping'
69         response = self.get_response(cr, uid, acc_obj.id, method)
70         if 'error' not in response:
71             if method == 'login' :
72                 vals['apikey'] = response
73             vals['state'] = 'approved'
74             self.write(cr, uid, ids, vals)
75         else : 
76             raise osv.except_osv('Error!!!',
77                             "Can't approved accoutnt : %s"%response['error'])
78         return True
79     
80     def button_cancel(self, cr, uid, ids, context):
81         self.write(cr, uid, ids, {'state': 'cancelled'})
82         return True
83     
84 marketing_campaign_mailchimp_account() #}}}
85
86 #class marketing_campaign_mailchimp_list(osv.osv_memory): #{{{
87 #    _name = "marketing.campaign.mailchimp.list"
88 #    
89 #    _columns = {
90 #            'name': fields.char('Name', size=64),
91 #            'mailchimp_account_id': fields.many2one('marketing.campaign.mailchimp.account', 'Account'),
92 #            'list_id': fields.char('List Id', size=64,),
93 #        }
94 #    
95 #    _defaults = {
96 #            'state': lambda *a: 'draft'
97 #        }
98 #    
99 #marketing_campaign_mailchimp_list() #}}}
100
101 class marketing_campaign_segment(osv.osv): #{{{
102     _inherit = "marketing.campaign.segment"
103     
104     _columns = {
105             'synchro': fields.boolean('Mailchimp Synchro'),
106             'mailchimp_account_id': fields.many2one(
107                             'marketing.campaign.mailchimp.account', 'Account'),
108             'mailchimp_list': fields.char('List', size=64),
109         }
110     
111     def onchange_mailchimp(self, cr, uid, ids, mailchimp_account_id):
112         if mailchimp_account_id:
113             return {'value':{'mailchimp_list':''}}
114         return {'value':{}}
115     
116     def onchange_mailchimp_list(self, cr, uid, ids, mailchimp_account_id, 
117                                                                 mailchimp_list):
118         if mailchimp_account_id and mailchimp_list:
119             lists = self.pool.get('marketing.campaign.mailchimp.account').get_response(cr,
120                                              uid, mailchimp_account_id, 'lists')
121             list_names = [list['name'] for list in lists]
122             if mailchimp_list not in list_names:
123                 raise osv.except_osv('Error!!!',"Lists doesn't exists")
124         else :
125             return {}
126         return {'value':{}}
127 marketing_campaign_segment() #}}}
128
129
130
131 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: