1 # -*- coding: utf-8 -*-
2 ##############################################################################
4 # OpenERP, Open Source Management Solution
5 # Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
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.
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.
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/>.
20 ##############################################################################
22 from osv import fields, osv
25 import simplejson as json
27 mailchimp_url = 'http://%s.api.mailchimp.com/1.2'
29 class marketing_campaign_mailchimp_account(osv.osv): #{{{
30 _name = "marketing.campaign.mailchimp.account"
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)
44 'state': lambda *a: 'draft'
47 def get_response(self, cr, uid, mailchimp_id, method, params={}):
48 mailchimp_account = self.browse(cr, uid, mailchimp_id)
49 params['output'] = 'json'
51 params['username'] = mailchimp_account.username
52 params['password'] = mailchimp_account.password
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())
62 def button_approve(self, cr, uid, ids, context):
63 acc_obj = self.browse(cr, uid, ids)[0]
65 if not acc_obj.apikey:
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)
76 raise osv.except_osv('Error!!!',
77 "Can't approved accoutnt : %s"%response['error'])
80 def button_cancel(self, cr, uid, ids, context):
81 self.write(cr, uid, ids, {'state': 'cancelled'})
84 marketing_campaign_mailchimp_account() #}}}
86 #class marketing_campaign_mailchimp_list(osv.osv_memory): #{{{
87 # _name = "marketing.campaign.mailchimp.list"
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,),
96 # 'state': lambda *a: 'draft'
99 #marketing_campaign_mailchimp_list() #}}}
101 class marketing_campaign_segment(osv.osv): #{{{
102 _inherit = "marketing.campaign.segment"
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),
111 def onchange_mailchimp(self, cr, uid, ids, mailchimp_account_id):
112 if mailchimp_account_id:
113 return {'value':{'mailchimp_list':''}}
116 def onchange_mailchimp_list(self, cr, uid, ids, mailchimp_account_id,
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")
127 marketing_campaign_segment() #}}}
131 # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: