|
@ -115,19 +115,29 @@ class ResCompany(models.Model): |
|
|
) |
|
|
) |
|
|
|
|
|
|
|
|
def action_import_google_contacts(self): |
|
|
def action_import_google_contacts(self): |
|
|
"""IMPORT Contacts FROM Google TO ODOO""" |
|
|
"""IMPORT Contacts FROM Google TO ODOO with pagination support""" |
|
|
url = ("https://people.googleapis.com/v1/people/me/" |
|
|
base_url = ("https://people.googleapis.com/v1/people/me/" |
|
|
"connections?personFields=names,addresses," |
|
|
"connections?personFields=names,addresses," |
|
|
"emailAddresses,phoneNumbers&pageSize=1000") |
|
|
"emailAddresses,phoneNumbers&pageSize=1000") |
|
|
headers = { |
|
|
headers = { |
|
|
'Authorization': f'Bearer {self.contact_company_access_token}', |
|
|
'Authorization': f'Bearer {self.contact_company_access_token}', |
|
|
'Content-Type': 'application/json' |
|
|
'Content-Type': 'application/json' |
|
|
} |
|
|
} |
|
|
response = requests.get(url, headers=headers) |
|
|
next_page_token = None |
|
|
if response.status_code == 200: |
|
|
partners = [] |
|
|
data = response.json().get('connections', []) |
|
|
|
|
|
partners = [] |
|
|
while True: |
|
|
for connection in data: |
|
|
url = base_url |
|
|
|
|
|
if next_page_token: |
|
|
|
|
|
url += f"&pageToken={next_page_token}" |
|
|
|
|
|
response = requests.get(url, headers=headers) |
|
|
|
|
|
if response.status_code != 200: |
|
|
|
|
|
error_message = f"Failed to import contact. Error: {response.text}" |
|
|
|
|
|
raise ValidationError(error_message) |
|
|
|
|
|
|
|
|
|
|
|
data = response.json() |
|
|
|
|
|
connections = data.get('connections', []) |
|
|
|
|
|
for connection in connections: |
|
|
cnt_rsr_name = connection.get('resourceName', '') |
|
|
cnt_rsr_name = connection.get('resourceName', '') |
|
|
etag = connection.get('etag', '') |
|
|
etag = connection.get('etag', '') |
|
|
names = connection.get('names', [{}])[0] |
|
|
names = connection.get('names', [{}])[0] |
|
@ -151,7 +161,8 @@ class ResCompany(models.Model): |
|
|
|
|
|
|
|
|
if addresses: |
|
|
if addresses: |
|
|
if country_code: |
|
|
if country_code: |
|
|
country_record = self.env['res.country'].search([('code', 'ilike', country_code)], limit=1) |
|
|
country_record = self.env['res.country'].search( |
|
|
|
|
|
[('code', 'ilike', country_code)], limit=1) |
|
|
country_id = country_record.id if country_record else False |
|
|
country_id = country_record.id if country_record else False |
|
|
else: |
|
|
else: |
|
|
country_id = False |
|
|
country_id = False |
|
@ -180,19 +191,23 @@ class ResCompany(models.Model): |
|
|
existing_partner.write(partner_vals) |
|
|
existing_partner.write(partner_vals) |
|
|
else: |
|
|
else: |
|
|
partners.append(partner_vals) |
|
|
partners.append(partner_vals) |
|
|
if partners: |
|
|
|
|
|
self.env['res.partner'].create(partners) |
|
|
if data.get('nextPageToken'): |
|
|
_logger.info("Contact imported successfully!") |
|
|
next_page_token = data['nextPageToken'] |
|
|
return { |
|
|
else: |
|
|
'type': 'ir.actions.client', |
|
|
break |
|
|
'tag': 'display_notification', |
|
|
|
|
|
'params': { |
|
|
if partners: |
|
|
'type': 'danger', |
|
|
self.env['res.partner'].create(partners) |
|
|
'sticky': False, |
|
|
_logger.info("Contacts imported successfully!") |
|
|
'message': _("Contact imported successfully!"), |
|
|
|
|
|
'next': {'type': 'ir.actions.act_window_close'}, |
|
|
return { |
|
|
} |
|
|
'type': 'ir.actions.client', |
|
|
|
|
|
'tag': 'display_notification', |
|
|
|
|
|
'params': { |
|
|
|
|
|
'type': 'success', |
|
|
|
|
|
'sticky': False, |
|
|
|
|
|
'message': _("Contacts imported successfully!"), |
|
|
|
|
|
'next': {'type': 'ir.actions.act_window_close'}, |
|
|
} |
|
|
} |
|
|
else: |
|
|
} |
|
|
error_message = f"Failed to import contact. Error: {response.text}" |
|
|
|
|
|
raise ValidationError(error_message) |
|
|
|
|
|