From 8d79a13fc29255af10d3f3c68e737469f7d9b724 Mon Sep 17 00:00:00 2001 From: "Ujjvala Collins (OpenERP)" Date: Wed, 16 Feb 2011 16:45:32 +0530 Subject: [PATCH] [REF] sync_google_contact: Simplification of import contact wizard. Removed unnecessary files and dependancies. bzr revid: uco@tinyerp.com-20110216111532-cgf5f788gyiqx409 --- addons/google_base_account/wizard/google_login.py | 22 +-- addons/sync_google_contact/__init__.py | 1 - addons/sync_google_contact/__openerp__.py | 3 +- addons/sync_google_contact/sync_google_contact.py | 110 --------------- .../sync_google_contact_view.xml | 17 --- addons/sync_google_contact/test.py | 40 ------ .../wizard/google_contact_import.py | 140 ++++++++++---------- .../wizard/google_contact_import_view.xml | 13 +- 8 files changed, 87 insertions(+), 259 deletions(-) delete mode 100644 addons/sync_google_contact/sync_google_contact.py delete mode 100644 addons/sync_google_contact/sync_google_contact_view.xml delete mode 100644 addons/sync_google_contact/test.py diff --git a/addons/google_base_account/wizard/google_login.py b/addons/google_base_account/wizard/google_login.py index e60af43..23602be 100644 --- a/addons/google_base_account/wizard/google_login.py +++ b/addons/google_base_account/wizard/google_login.py @@ -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: diff --git a/addons/sync_google_contact/__init__.py b/addons/sync_google_contact/__init__.py index 202f8f9..f8a7a81 100644 --- a/addons/sync_google_contact/__init__.py +++ b/addons/sync_google_contact/__init__.py @@ -19,7 +19,6 @@ # ############################################################################## -import sync_google_contact import wizard # vim:expandtab:smartindent:tabstop=4:softtabstop=4:shiftwidth=4: diff --git a/addons/sync_google_contact/__openerp__.py b/addons/sync_google_contact/__openerp__.py index df4c1f5..4d0d98a 100644 --- a/addons/sync_google_contact/__openerp__.py +++ b/addons/sync_google_contact/__openerp__.py @@ -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': [], diff --git a/addons/sync_google_contact/sync_google_contact.py b/addons/sync_google_contact/sync_google_contact.py deleted file mode 100644 index da42618..0000000 --- a/addons/sync_google_contact/sync_google_contact.py +++ /dev/null @@ -1,110 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2010 Tiny SPRL (). -# -# 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 . -# -############################################################################## - -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: - diff --git a/addons/sync_google_contact/sync_google_contact_view.xml b/addons/sync_google_contact/sync_google_contact_view.xml deleted file mode 100644 index f7c077c..0000000 --- a/addons/sync_google_contact/sync_google_contact_view.xml +++ /dev/null @@ -1,17 +0,0 @@ - - - - - res.partner.address.google.form.inherited1 - res.partner.address - - form - - - - - - - - - diff --git a/addons/sync_google_contact/test.py b/addons/sync_google_contact/test.py deleted file mode 100644 index 896fd93..0000000 --- a/addons/sync_google_contact/test.py +++ /dev/null @@ -1,40 +0,0 @@ -# -*- coding: utf-8 -*- -############################################################################## -# -# OpenERP, Open Source Management Solution -# Copyright (C) 2004-2010 Tiny SPRL (). -# -# 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 . -# -############################################################################## - -# 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 - \ No newline at end of file diff --git a/addons/sync_google_contact/wizard/google_contact_import.py b/addons/sync_google_contact/wizard/google_contact_import.py index 3427f9a..764cffc 100644 --- a/addons/sync_google_contact/wizard/google_contact_import.py +++ b/addons/sync_google_contact/wizard/google_contact_import.py @@ -22,19 +22,29 @@ 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'),('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') partner_obj = self.pool.get('res.partner') user_obj = self.pool.get('res.users').browse(cr, uid, uid) @@ -43,78 +53,62 @@ class google_contact_import(osv.osv_memory): if not gmail_user or not gamil_pwd: raise osv.except_osv(_('Error'), _("Please specify the user and password !")) - google_obj = sync_google_contact.google_lib(gmail_user, gamil_pwd) - contact = google_obj._get_contact() + contact = gd_client.GetContactsFeed() partner_id = [] - partner_name = [] - group_links = {} + addresses = [] for obj in self.browse(cr, uid, ids, context=context): - if obj.create_partner == 'group': - groups = google_obj._get_contact_allGroups() - for grp in groups.entry: - partner_name.append(grp.title.text) - group_links[grp.title.text] = grp.id.text - elif obj.create_partner == 'gmail_user': - google_obj = sync_google_contact.google_lib(gmail_user, gamil_pwd) - contact = google_obj._get_contact() + if obj.create_partner: for user in contact.author: - partner_name.append(user.name.text) - - # Creating partner for selected option. - for name in partner_name: - partner_id = partner_obj.search(cr, uid, [('name','ilike',name)], context=context) + 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': name}, context=context)) - contact = google_obj._get_contact() - contact_ids = [] - link = group_links.get(name) - while contact: - for entry in contact.entry: - google_id = entry.id.text - contact_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) - data = { - 'name': contact_name, - 'phone': phone_numbers, - 'email': emails, - 'google_id': google_id, - 'partner_id': partner_id and partner_id[0] - } - if entry.group_membership_info and link: - for grp in entry.group_membership_info: - if grp.href == link: - addresss_obj.create(cr, uid, data, context=context) - else: - if obj.create_partner == 'gmail_user': - data.update({'partner_id': partner_id and partner_id[0]}) - else: - data.update({'partner_id': False}) - continue - contact_ids = addresss_obj.search(cr, uid, [('email','ilike',emails)]) - if not contact_ids: - addresss_obj.create(cr, uid, data, context=context) - if not contact: - break - next = contact.GetNextLink() - contact = None - if next: - contact = google_obj._get_contact(next.href) - partner_id = [] - if partner_id: - return { - 'name': _('Partner'), - 'domain': "[('id','in',"+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', - } - else: - return {'type': 'ir.actions.act_window_close'} + partner_id.append(partner_obj.create(cr, uid, {'name': partner_name}, context=context)) + while contact: + for entry in contact.entry: + 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) + data = { + 'name': name, + 'phone': phone_numbers, + 'email': emails, + 'partner_id': partner_id and partner_id[0] + } + 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: + break + next = contact.GetNextLink() + contact = None + if next: + 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() diff --git a/addons/sync_google_contact/wizard/google_contact_import_view.xml b/addons/sync_google_contact/wizard/google_contact_import_view.xml index af2c575..3706368 100644 --- a/addons/sync_google_contact/wizard/google_contact_import_view.xml +++ b/addons/sync_google_contact/wizard/google_contact_import_view.xml @@ -3,15 +3,18 @@ - synchronize.base.import.form - synchronize.base + google.import.contact.form + google.import.contact form
- - + + + + +