[REF] sync_google_contact: Refactored the changes of adding timestamp checking.

bzr revid: uco@tinyerp.com-20110329100641-gmle34qvuk18s87z
This commit is contained in:
Ujjvala Collins (OpenERP) 2011-03-29 15:36:41 +05:30
parent 83aca6da00
commit 51eb931727
4 changed files with 16 additions and 47 deletions

View File

@ -30,7 +30,6 @@
'depends': ['base','google_base_account'],
'init_xml': [],
'update_xml': [
'sync_google_contact_view.xml',
'wizard/google_contact_import_view.xml'
],
'demo_xml': [],

View File

@ -19,13 +19,13 @@
#
##############################################################################
from osv import fields,osv,orm
from osv import fields,osv
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."),
'write_date': fields.datetime('Date Modified', readonly=True, help="Modification date and time of address."),
}
def unlink(self, cr, uid, ids, context=None):

View File

@ -1,21 +0,0 @@
<?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>

View File

@ -18,13 +18,10 @@
# 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
try:
import gdata
@ -146,10 +143,15 @@ class synchronize_google_contact(osv.osv_memory):
addresses = []
partner_ids = []
contact_ids=[]
if 'tz' in context and context['tz']:
time_zone = context['tz']
else:
time_zone = 'Asia/Kolkata'
au_tz = timezone(time_zone)
while contact:
for entry in contact.entry:
data = self._retreive_data(entry, context=context)
data = self._retreive_data(entry)
google_id = data.pop('id')
model_data = {
'name': google_id,
@ -161,15 +163,18 @@ 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'])])
if contact_ids:
addresses.append(contact_ids[0])
self.update_contact(cr, uid, contact_ids, data, context=context)
address = addresss_obj.browse(cr, uid, contact_ids[0], context=context)
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')
if address.write_date < updated_dt:
self.update_contact(cr, uid, contact_ids, data, context=context)
res_id = contact_ids[0]
if not contact_ids:
#create or link to an existing partner only if it's a new contact
@ -195,7 +200,7 @@ class synchronize_google_contact(osv.osv_memory):
else:
return addresses
def _retreive_data(self, entry, context=None):
def _retreive_data(self, entry):
data = {}
data['id'] = entry.id.text
name = tools.ustr(entry.title.text)
@ -205,19 +210,6 @@ class synchronize_google_contact(osv.osv_memory):
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:
if phone.rel == gdata.contacts.REL_WORK:
@ -244,7 +236,6 @@ class synchronize_google_contact(osv.osv_memory):
vals['phone'] = data.get('phone','')
if not addr.fax:
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'}