[IMP] sync_google_contact: Improved updating records based on last modified date/time of a google contact while importing.
bzr revid: uco@tinyerp.com-20110329070822-d2ffzvl4cirdg0j3
This commit is contained in:
@@ -30,6 +30,7 @@
|
||||
'depends': ['base','google_base_account'],
|
||||
'init_xml': [],
|
||||
'update_xml': [
|
||||
'sync_google_contact_view.xml',
|
||||
'wizard/google_contact_import_view.xml'
|
||||
],
|
||||
'demo_xml': [],
|
||||
|
||||
@@ -24,6 +24,10 @@ from osv import fields,osv,orm
|
||||
class res_partner_address(osv.osv):
|
||||
_inherit = "res.partner.address"
|
||||
|
||||
_columns = {
|
||||
'last_modification_date': fields.datetime('Last Modification Date', readonly=True, help="Last modification date of google contact."),
|
||||
}
|
||||
|
||||
def unlink(self, cr, uid, ids, context=None):
|
||||
model_obj = self.pool.get('ir.model.data')
|
||||
model_ids = model_obj.search(cr, uid, [('res_id','in',ids),('model','=','res.partner.address'),('module','=','sync_google_contact')], context=context)
|
||||
|
||||
@@ -0,0 +1,21 @@
|
||||
<?xml version="1.0"?>
|
||||
<openerp>
|
||||
<data>
|
||||
|
||||
<record model="ir.ui.view" id="view_partner_google_form">
|
||||
<field name="name">res.partner.google.form</field>
|
||||
<field name="type">form</field>
|
||||
<field name="model">res.partner.address</field>
|
||||
<field name="inherit_id" ref="base.view_partner_address_form1"/>
|
||||
<field name="arch" type="xml">
|
||||
<field name="email" position="after">
|
||||
<group colspan="2" col="2">
|
||||
<separator string="Google Information" colspan="4"/>
|
||||
<field name="last_modification_date" colspan="4"/>
|
||||
</group>
|
||||
</field>
|
||||
</field>
|
||||
</record>
|
||||
|
||||
</data>
|
||||
</openerp>
|
||||
@@ -18,10 +18,14 @@
|
||||
# along with this program. If not, see <http://www.gnu.org/licenses/>.
|
||||
#
|
||||
##############################################################################
|
||||
import time
|
||||
import datetime
|
||||
import dateutil
|
||||
from dateutil.tz import *
|
||||
from dateutil.parser import *
|
||||
from pytz import timezone
|
||||
import pytz
|
||||
|
||||
from osv import fields,osv
|
||||
from tools.translate import _
|
||||
import tools
|
||||
try:
|
||||
import gdata
|
||||
import gdata.contacts.service
|
||||
@@ -29,6 +33,10 @@ try:
|
||||
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'))
|
||||
|
||||
from osv import fields,osv
|
||||
from tools.translate import _
|
||||
import tools
|
||||
|
||||
class google_contact_import(osv.osv_memory):
|
||||
_inherit = 'google.login'
|
||||
_name = 'google.login.contact'
|
||||
@@ -138,9 +146,10 @@ class synchronize_google_contact(osv.osv_memory):
|
||||
addresses = []
|
||||
partner_ids = []
|
||||
contact_ids=[]
|
||||
|
||||
while contact:
|
||||
for entry in contact.entry:
|
||||
data = self._retreive_data(entry)
|
||||
data = self._retreive_data(entry, context=context)
|
||||
google_id = data.pop('id')
|
||||
model_data = {
|
||||
'name': google_id,
|
||||
@@ -152,6 +161,9 @@ class synchronize_google_contact(osv.osv_memory):
|
||||
data_ids = model_obj.search(cr, uid, [('model','=','res.partner.address'), ('name','=', google_id)])
|
||||
if data_ids:
|
||||
contact_ids = [model_obj.browse(cr, uid, data_ids[0], context=context).res_id]
|
||||
address = addresss_obj.browse(cr, uid, contact_ids[0], context=context)
|
||||
if address.last_modification_date > data.get('last_modification_date'):
|
||||
data['last_modification_date'] = address.last_modification_date
|
||||
elif data['email']:
|
||||
contact_ids = addresss_obj.search(cr, uid, [('email', 'ilike', data['email'])])
|
||||
|
||||
@@ -183,7 +195,7 @@ class synchronize_google_contact(osv.osv_memory):
|
||||
else:
|
||||
return addresses
|
||||
|
||||
def _retreive_data(self, entry):
|
||||
def _retreive_data(self, entry, context=None):
|
||||
data = {}
|
||||
data['id'] = entry.id.text
|
||||
name = tools.ustr(entry.title.text)
|
||||
@@ -192,7 +204,19 @@ class synchronize_google_contact(osv.osv_memory):
|
||||
data['name'] = name
|
||||
emails = ','.join(email.address for email in entry.email)
|
||||
data['email'] = emails
|
||||
|
||||
|
||||
if 'tz' in context and context['tz']:
|
||||
time_zone = context['tz']
|
||||
else:
|
||||
time_zone = 'Asia/Kolkata'
|
||||
au_tz = timezone(time_zone)
|
||||
|
||||
if entry.updated:
|
||||
google_updated = entry.updated.text
|
||||
utime = dateutil.parser.parse(google_updated)
|
||||
au_dt = au_tz.normalize(utime.astimezone(au_tz))
|
||||
updated_dt = datetime.datetime(*au_dt.timetuple()[:6]).strftime('%Y-%m-%d %H:%M:%S')
|
||||
data['last_modification_date'] = updated_dt
|
||||
|
||||
if entry.phone_number:
|
||||
for phone in entry.phone_number:
|
||||
@@ -211,17 +235,18 @@ class synchronize_google_contact(osv.osv_memory):
|
||||
name = str((addr.name or addr.partner_id and addr.partner_id.name or '').encode('utf-8'))
|
||||
|
||||
if not name:
|
||||
vals['name']=data.get('name','')
|
||||
vals['name'] = data.get('name','')
|
||||
if not addr.email:
|
||||
vals['email']=data.get('email','')
|
||||
vals['email'] = data.get('email','')
|
||||
if not addr.mobile:
|
||||
vals['mobile']=data.get('mobile','')
|
||||
vals['mobile'] = data.get('mobile','')
|
||||
if not addr.phone:
|
||||
vals['phone']=data.get('phone','')
|
||||
vals['phone'] = data.get('phone','')
|
||||
if not addr.fax:
|
||||
vals['fax']=data.get('fax','')
|
||||
|
||||
addresss_obj.write(cr,uid,contact_ids,vals,context=context)
|
||||
vals['fax'] = data.get('fax','')
|
||||
vals['last_modification_date'] = data.get('last_modification_date')
|
||||
|
||||
addresss_obj.write(cr, uid, contact_ids, vals, context=context)
|
||||
return {'type': 'ir.actions.act_window_close'}
|
||||
|
||||
synchronize_google_contact()
|
||||
|
||||
Reference in New Issue
Block a user