[NEW] mailchimp module for marketing campaign - only mailchimp account access

bzr revid: dsh@tinyerp.com-20100531145220-6j6mu3huol444535
This commit is contained in:
DSH (Open ERP) 2010-05-31 20:22:20 +05:30
parent f1274a2a72
commit 552c994c37
4 changed files with 283 additions and 0 deletions

View File

@ -0,0 +1,24 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
import marketing_campaign_mailchimp
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,41 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
{
"name" : "",
"version" : "1.1",
"depends" : ['marketing_campaign'],
"author" : "Tiny",
"category": 'Generic Modules/Marketing',
"description": """
""",
'website': 'http://www.openerp.com',
'init_xml': [],
'update_xml': [
'marketing_campaign_mailchimp_view.xml',
],
'demo_xml': [],
'installable': True,
'active': False,
}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,131 @@
# -*- coding: utf-8 -*-
##############################################################################
#
# OpenERP, Open Source Management Solution
# Copyright (C) 2004-2010 Tiny SPRL (<http://tiny.be>).
#
# This program is free software: you can redistribute it and/or modify
# it under the terms of the GNU Affero General Public License as
# published by the Free Software Foundation, either version 3 of the
# License, or (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
# GNU Affero General Public License for more details.
#
# You should have received a copy of the GNU Affero General Public License
# along with this program. If not, see <http://www.gnu.org/licenses/>.
#
##############################################################################
from osv import fields, osv
import urllib2
import simplejson as json
mailchimp_url = 'http://%s.api.mailchimp.com/1.2'
class marketing_campaign_mailchimp_account(osv.osv): #{{{
_name = "marketing.campaign.mailchimp.account"
_columns = {
'name': fields.char('Account Name', size=64),
'username': fields.char('Username', size=64, required=True),
'password': fields.char('Password', size=64, required=True),
'apikey': fields.char('API Key', size=128),
'data_center': fields.selection([('us1', 'US1'), ('us2', 'US2'),
('uk1', 'UK1')], 'Data Center'),
'state': fields.selection([('draft', 'Draft'), ('approved', 'Approved'),
('cancelled', 'Cancelled')], 'State', readonly=True)
}
_defaults = {
'state': lambda *a: 'draft'
}
def get_response(self, cr, uid, mailchimp_id, method, params={}):
mailchimp_account = self.browse(cr, uid, mailchimp_id)
params['output'] = 'json'
if method == 'login':
params['username'] = mailchimp_account.username
params['password'] = mailchimp_account.password
else :
params['apikey'] = mailchimp_account.apikey
params = '&'.join(map(lambda x : '%s=%s' %(x, params[x]), params))
url = mailchimp_url%mailchimp_account.data_center+ '/?method=%s'%method
req = urllib2.Request(url, params)
handle = urllib2.urlopen(req)
response = json.loads(handle.read())
return response
def button_approve(self, cr, uid, ids, context):
acc_obj = self.browse(cr, uid, ids)[0]
vals = {}
if not acc_obj.apikey:
method = 'login'
else:
method = 'ping'
response = self.get_response(cr, uid, acc_obj.id, method)
if 'error' not in response:
if method == 'login' :
vals['apikey'] = response
vals['state'] = 'approved'
self.write(cr, uid, ids, vals)
else :
raise osv.except_osv('Error!!!',
"Can't approved accoutnt : %s"%response['error'])
return True
def button_cancel(self, cr, uid, ids, context):
self.write(cr, uid, ids, {'state': 'cancelled'})
return True
marketing_campaign_mailchimp_account() #}}}
#class marketing_campaign_mailchimp_list(osv.osv_memory): #{{{
# _name = "marketing.campaign.mailchimp.list"
#
# _columns = {
# 'name': fields.char('Name', size=64),
# 'mailchimp_account_id': fields.many2one('marketing.campaign.mailchimp.account', 'Account'),
# 'list_id': fields.char('List Id', size=64,),
# }
#
# _defaults = {
# 'state': lambda *a: 'draft'
# }
#
#marketing_campaign_mailchimp_list() #}}}
class marketing_campaign_segment(osv.osv): #{{{
_inherit = "marketing.campaign.segment"
_columns = {
'synchro': fields.boolean('Mailchimp Synchro'),
'mailchimp_account_id': fields.many2one(
'marketing.campaign.mailchimp.account', 'Account'),
'mailchimp_list': fields.char('List', size=64),
}
def onchange_mailchimp(self, cr, uid, ids, mailchimp_account_id):
if mailchimp_account_id:
return {'value':{'mailchimp_list':''}}
return {'value':{}}
def onchange_mailchimp_list(self, cr, uid, ids, mailchimp_account_id,
mailchimp_list):
if mailchimp_account_id and mailchimp_list:
lists = self.pool.get('marketing.campaign.mailchimp.account').get_response(cr,
uid, mailchimp_account_id, 'lists')
list_names = [list['name'] for list in lists]
if mailchimp_list not in list_names:
raise osv.except_osv('Error!!!',"Lists doesn't exists")
else :
return {}
return {'value':{}}
marketing_campaign_segment() #}}}
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:

View File

@ -0,0 +1,87 @@
<?xml version="1.0"?>
<openerp>
<data>
<!--
================================================================
Marketing Segments Inherited View - mailchimp
================================================================
-->
<record model="ir.ui.view" id="view_marketing_campaign_segment_form_inherit">
<field name="name">marketing.campaign.segment.form.inherit</field>
<field name="model">marketing.campaign.segment</field>
<field name="type">form</field>
<field name="inherit_id" ref="marketing_campaign.view_marketing_campaign_segment_form"/>
<field name="arch" type="xml">
<field name="date_done" position="after">
<separator string="Mailchimp Synchronization" colspan="4"/>
<field name="synchro"/>
<group colspan="2" attrs="{'invisible':[('synchro','!=',True)]}">
<field name="mailchimp_account_id" attrs="{'required':[('synchro','=',True)]}" domain="[('state','=','approved')]" on_change="onchange_mailchimp(mailchimp_account_id)"/>
<field name="mailchimp_list" attrs="{'required':[('synchro','=',True)]}" on_change="onchange_mailchimp_list(mailchimp_account_id,mailchimp_list)"/>
</group>
</field>
</field>
</record>
<!--
================================================================
Mailchimp Account
================================================================
-->
<menuitem name="Tools" id="base.menu_tools" icon="STOCK_PREFERENCES" sequence="15"/>
<menuitem name="Mailchimps" id="menu_action_mailchimp" parent="base.menu_tools" />
<record model="ir.ui.view" id="view_marketing_campaign_mailchimp_account_tree">
<field name="name">marketing.campaign.mailchimp.account.tree</field>
<field name="model">marketing.campaign.mailchimp.account</field>
<field name="type">tree</field>
<field name="arch" type="xml">
<tree string="All Accounts">
<field name="name"/>
<field name="username" />
<field name="password"/>
<field name="data_center"/>
<field name="state"/>
</tree>
</field>
</record>
<record model="ir.ui.view" id="view_marketing_campaign_mailchimp_account_form">
<field name="name">marketing.campaign.mailchimp.account.form</field>
<field name="model">marketing.campaign.mailchimp.account</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Account">
<field name="name" select="1"/>
<newline/>
<field name="username" select="1"/>
<field name="password" select="1"/>
<field name="apikey" />
<field name="data_center"/>
<separator string="Status" colspan="4"/>
<group col="13" colspan="4">
<field name="state" nolabel="1"/>
<button name="button_approve" string="Approve" states="draft" type="object"/>
<button name="button_cancel" string="Cancel" states="draft,approved" type="object"/>
</group>
</form>
</field>
</record>
<record model="ir.actions.act_window" id="action_marketing_campaign_mailchimp_account">
<field name="name">All Mailchimp Accounts</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">marketing.campaign.mailchimp.account</field>
<field name="view_type">form</field>
<field name="view_mode">tree,form</field>
<field name="view_id" ref="view_marketing_campaign_mailchimp_account_tree"/>
</record>
<menuitem id="menu_action_marketing_campaign_mailchimp_account" parent="menu_action_mailchimp" action="action_marketing_campaign_mailchimp_account" sequence="40"/>
</data>
</openerp>