[MERGE] Merge google_contact simple import wizard

bzr revid: sbh@tinyerp.com-20110216113646-39q71i7zg6u15yqe
This commit is contained in:
Sbh (OpenERP)
2011-02-16 17:06:46 +05:30
8 changed files with 80 additions and 213 deletions
@@ -24,8 +24,6 @@ from tools.translate import _
import gdata.contacts
import gdata.contacts.service
from sync_google_contact import sync_google_contact
class google_login(osv.osv_memory):
_description ='Google Contact'
_name = 'google.login'
@@ -35,24 +33,26 @@ class google_login(osv.osv_memory):
}
def check_login(self, cr, uid, ids, context=None):
if context==None:
context={}
data=self.read(cr,uid,ids)[0]
user=data['user']
password=data['password']
if context == None:
context = {}
data = self.read(cr, uid, ids)[0]
user = data['user']
password = data['password']
gd_client = gdata.contacts.service.ContactsService()
gd_client.email = user
gd_client.password = password
gd_client.source = 'OpenERP'
try:
gd_client.ProgrammaticLogin()
res={'gmail_user':user,
'gmail_password':password}
self.pool.get('res.users').write(cr,uid,uid,res,context=context)
res = {
'gmail_user': user,
'gmail_password': password
}
self.pool.get('res.users').write(cr, uid, uid, res, context=context)
except Exception, e:
raise osv.except_osv(_('Error!'),_('%s' % (e)))
return {}
return gd_client
google_login()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
-1
View File
@@ -19,7 +19,6 @@
#
##############################################################################
import sync_google_contact
import wizard
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
+1 -2
View File
@@ -27,10 +27,9 @@
'description': """The module adds google contact in partner address""",
'author': 'OpenERP SA',
'website': 'http://www.openerp.com',
'depends': ['base','sync_base','google_base_account',],
'depends': ['base','google_base_account'],
'init_xml': [],
'update_xml': [
'sync_google_contact_view.xml',
'wizard/google_contact_import_view.xml'
],
'demo_xml': [],
@@ -1,110 +0,0 @@
# -*- 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,orm
from tools.translate import _
try:
import gdata
from gdata import contacts
import gdata.contacts.service
import atom
except ImportError:
raise osv.except_osv(_('Google Contacts Import Error!'), _('Please install gdata-python-client from http://code.google.com/p/gdata-python-client/downloads/list'))
class google_lib(object):
def __init__(self, email, password):
super(google_lib, self).__init__()
self.contact = gdata.contacts.service.ContactsService()
self.contact.email = email
self.contact.password = password
self.contact.source = 'OpenERP'
try:
self.contact.ProgrammaticLogin()
except Exception, e:
raise osv.except_osv(_('Error!'),_('%s' % (e)))
def _get_contact(self, href=''):
if href:
feed = self.contact.GetContactsFeed(href)
else:
feed = self.contact.GetContactsFeed()
return feed
def _get_contact_allGroups(self):
""" fetch all allGroup."""
feed = self.contact.GetGroupsFeed()
return feed
def _create_contact(self,name,primary_email):
""" create a contact."""
new_contact = gdata.contacts.ContactEntry(title=atom.Title(text=name))
# Create a work email address for the contact and use as primary.
if primary_email:
new_contact.email.append(gdata.contacts.Email(address=primary_email,
primary='true', rel=gdata.contacts.REL_WORK))
entry = self.contact.CreateContact(new_contact)
return entry
def _delete_contact(self):
self.contact.DeleteContact(selected_entry.GetEditLink().href)
return True
class res_partner_address(osv.osv):
_inherit = "res.partner.address"
_columns = {
'sync_google':fields.boolean('Synchronize with Google'),
'google_id': fields.char('Google Contact Id', size=128, readonly=True),
}
def create(self, cr, uid, vals, context=None):
id =super(res_partner_address, self).create(cr, uid, vals, context=context)
vals.update({'ids':id})
if context is None:
context = {}
if vals.get('sync_google') :
self.sync_create(cr,uid,vals,context=context,synchronize=vals.get('sync_google'))
return id
def write(self, cr, uid, ids, vals, context=None, check=True, update_check=True):
if context is None:
context = {}
return super(res_partner_address, self).write(cr, uid, ids, vals, context=context)
def sync_create(self, cr, uid, vals, context=None,synchronize=True):
# we all more detail soon
user_obj=self.pool.get('res.users').browse(cr, uid, uid)
gmail_user=user_obj.gmail_user
gamil_pwd=user_obj.gmail_password
google_obj =google_lib(gmail_user, gamil_pwd)
name=vals.get('name')
email=vals.get('email')
contact = google_obj._create_contact(name,email)
openerp_id=vals['ids']
self.write(cr,uid,[openerp_id],{'google_id':contact.id.text},context=context)
return True
res_partner_address()
# vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4:
@@ -1,17 +0,0 @@
<?xml version="1.0"?>
<openerp>
<data>
<record model="ir.ui.view" id="view_partner_address_form_google_inherited1">
<field name='name'>res.partner.address.google.form.inherited1</field>
<field name='model'>res.partner.address</field>
<field name="inherit_id" ref="base.view_partner_address_form1"/>
<field name='type'>form</field>
<field name='arch' type='xml'>
<field name="email" position="after">
<field name="google_id"/>
<field name="sync_google"/>
</field>
</field>
</record>
</data>
</openerp>
-40
View File
@@ -1,40 +0,0 @@
# -*- 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/>.
#
##############################################################################
# demo script for geeting contact detail'
import sys
import getopt
import getpass
import atom
import gdata.contacts
import gdata.contacts.service
email='user name of gmail'
password='add the password'
gd_client = gdata.contacts.service.ContactsService()
gd_client.email = email
gd_client.password = password
gd_client.source = 'GoogleInc-ContactsPythonSample-1'
gd_client.ProgrammaticLogin()
feed= gd_client.GetContactsFeed()
next = feed.GetNextLink()
for i, entry in enumerate(feed.entry):
print entry.title.text
@@ -19,35 +19,53 @@
#
##############################################################################
from osv import fields,osv,orm
from osv import fields,osv
from tools.translate import _
from sync_google_contact import sync_google_contact
class google_contact_import(osv.osv_memory):
_name = "synchronize.base"
_inherit = 'synchronize.base'
_name = "google.import.contact"
_inherit = 'google.login'
_columns = {
'tools': fields.selection([('gmail','Gmail')],'Tools'),
'create_partner':fields.selection([('group','Group'),('email_address','Email address'),('gmail_user','Gmail user')], 'Create Partner'),
'create_partner': fields.boolean('Create Partner', help="It will create Partner for given gmail user otherwise only adds contacts in Partner Addresses.")
}
def default_get(self, cr, uid, fields, context=None):
res = super(google_contact_import, self).default_get(cr, uid, fields, context=context)
user_obj = self.pool.get('res.users').browse(cr, uid, uid)
if 'user' in fields:
res.update({'user': user_obj.gmail_user})
if 'password' in fields:
res.update({'password': user_obj.gmail_password})
return res
def import_contact(self, cr, uid, ids, context=None):
# Only see the result, we will change the code
gd_client = self.check_login(cr, uid, ids, context=context)
if not gd_client:
return {'type': 'ir.actions.act_window_close'}
addresss_obj = self.pool.get('res.partner.address')
user_obj=self.pool.get('res.users').browse(cr, uid, uid)
gmail_user=user_obj.gmail_user
gamil_pwd=user_obj.gmail_password
partner_obj = self.pool.get('res.partner')
user_obj = self.pool.get('res.users').browse(cr, uid, uid)
gmail_user = user_obj.gmail_user
gamil_pwd = user_obj.gmail_password
if not gmail_user or not gamil_pwd:
raise osv.except_osv(_('Error'), _("Please specify the user and password !"))
contact = gd_client.GetContactsFeed()
partner_id = []
addresses = []
for obj in self.browse(cr, uid, ids, context=context):
google_obj = sync_google_contact.google_lib(gmail_user, gamil_pwd)
contact = google_obj._get_contact()
addresses = []
if obj.create_partner:
for user in contact.author:
partner_name = user.name.text
partner_id = partner_obj.search(cr, uid, [('name','ilike',partner_name)], context=context)
if not partner_id:
partner_id.append(partner_obj.create(cr, uid, {'name': partner_name}, context=context))
while contact:
for entry in contact.entry:
google_id= entry.id.text
name = entry.title.text
phone_numbers = ','.join(phone_number.text for phone_number in entry.phone_number)
emails = ','.join(email.address for email in entry.email)
@@ -55,9 +73,9 @@ class google_contact_import(osv.osv_memory):
'name': name,
'phone': phone_numbers,
'email': emails,
'google_id':google_id
'partner_id': partner_id and partner_id[0]
}
contact_ids = addresss_obj.search(cr, uid, [('email','ilike',emails),])
contact_ids = addresss_obj.search(cr, uid, [('email','ilike',emails)])
if not contact_ids:
addresses.append(addresss_obj.create(cr, uid, data, context=context))
if not contact:
@@ -65,17 +83,32 @@ class google_contact_import(osv.osv_memory):
next = contact.GetNextLink()
contact = None
if next:
contact = google_obj._get_contact(next.href)
return {
'name': _('Contacts'),
'domain': "[('id','in',"+str(addresses)+")]",
'view_type': 'form',
'view_mode': 'tree,form',
'res_model': 'res.partner.address',
'context': context,
'views': [(False, 'tree'),(False, 'form')],
'type': 'ir.actions.act_window',
}
contact = gd_client.GetContactsFeed(next.href)
if partner_id:
partner_id = partner_id[0]
return {
'name': _('Partner'),
'domain': "[('id','=',"+str(partner_id)+")]",
'view_type': 'form',
'view_mode': 'tree,form',
'res_model': 'res.partner',
'context': context,
'views': [(False, 'tree'),(False, 'form')],
'type': 'ir.actions.act_window',
}
elif addresses:
return {
'name': _('Contacts'),
'domain': "[('id','in', ["+','.join(map(str,addresses))+"])]",
'view_type': 'form',
'view_mode': 'tree,form',
'res_model': 'res.partner.address',
'context': context,
'views': [(False, 'tree'),(False, 'form')],
'type': 'ir.actions.act_window',
}
else:
return {'type': 'ir.actions.act_window_close'}
google_contact_import()
@@ -3,15 +3,18 @@
<data>
<record model="ir.ui.view" id="view_google_contact_import_form">
<field name="name">synchronize.base.import.form</field>
<field name="model">synchronize.base</field>
<field name="name">google.import.contact.form</field>
<field name="model">google.import.contact</field>
<field name="type">form</field>
<field name="arch" type="xml">
<form string="Import Google Contacts">
<group colspan="2" col="2">
<field name="tools"/>
<field name="create_partner" />
<field name="user"/>
<field name="password" password="True"/>
</group>
<newline/>
<separator string="Do you want to create partner?" colspan="4"/>
<field name="create_partner" string="Yes/No"/>
<separator string="" colspan="4"/>
<group colspan="2" col="2">
<button special="cancel" string="Cancel" icon="gtk-cancel"/>
@@ -24,7 +27,7 @@
<record model="ir.actions.act_window" id="act_google_contact_import_form">
<field name="name">Import Google Contacts</field>
<field name="type">ir.actions.act_window</field>
<field name="res_model">synchronize.base</field>
<field name="res_model">google.import.contact</field>
<field name="view_type">form</field>
<field name="view_mode">form</field>
<field name="target">new</field>