| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159 |
- # -*- coding: utf-8 -*-
- # Part of Odoo. See LICENSE file for full copyright and licensing details.
- import requests
- import json
- from odoo import fields, models, api, _
- from odoo.exceptions import UserError
- class ResConfigSettings(models.TransientModel):
- _inherit = 'res.config.settings'
- # Google API Configuration
- google_api_enabled = fields.Boolean(
- string='Google API Integration',
- config_parameter='google_api.enabled',
- help='Enable integration with Google Drive and Google Calendar'
- )
-
- # Google API Credentials (shared for all services)
- google_api_client_id = fields.Char(
- string='Google API Client ID',
- config_parameter='google_api.client_id',
- help='Client ID for Google APIs (Drive, Calendar, etc.)'
- )
- google_api_client_secret = fields.Char(
- string='Google API Client Secret',
- config_parameter='google_api.client_secret',
- help='Client Secret for Google APIs (Drive, Calendar, etc.)'
- )
- def action_test_google_api_connection(self):
- """Test Google API connection by checking if credentials are valid"""
- self.ensure_one()
-
- if not self.google_api_enabled:
- raise UserError(_('Google API Integration is not enabled'))
-
- if not self.google_api_client_id or not self.google_api_client_secret:
- raise UserError(_('Please provide both Client ID and Client Secret'))
-
- try:
- # Test 1: Verify credentials format
- if len(self.google_api_client_id) < 10:
- raise UserError(_('Client ID appears to be invalid (too short)'))
-
- if len(self.google_api_client_secret) < 10:
- raise UserError(_('Client Secret appears to be invalid (too short)'))
-
- # Test 2: Try to reach Google APIs (basic connectivity test)
- google_api_url = "https://www.googleapis.com"
- response = requests.get(google_api_url, timeout=10)
-
- if response.status_code not in [200, 404]: # 404 is expected for root URL
- raise UserError(_('Cannot reach Google APIs. Please check your internet connection.'))
-
- # Test 3: Check if Drive API is available
- drive_api_url = "https://www.googleapis.com/drive/v3/about"
- drive_response = requests.get(drive_api_url, timeout=10)
-
- # Drive API requires authentication, so 401 is expected
- if drive_response.status_code not in [401, 403]:
- raise UserError(_('Google Drive API is not accessible'))
-
- # Test 4: Check if Calendar API is available
- calendar_api_url = "https://www.googleapis.com/calendar/v3/users/me/calendarList"
- calendar_response = requests.get(calendar_api_url, timeout=10)
-
- # Calendar API requires authentication, so 401 is expected
- if calendar_response.status_code not in [401, 403]:
- raise UserError(_('Google Calendar API is not accessible'))
-
- # Test 5: Validate OAuth2 endpoints
- oauth_auth_url = "https://accounts.google.com/o/oauth2/auth"
- oauth_response = requests.get(oauth_auth_url, timeout=10)
-
- if oauth_response.status_code not in [200, 405]: # 405 Method Not Allowed is expected for GET
- raise UserError(_('Google OAuth2 endpoints are not accessible'))
-
- # All tests passed
- return {
- 'type': 'ir.actions.client',
- 'tag': 'display_notification',
- 'params': {
- 'title': _('Success'),
- 'message': _('Google API connection test successful! All APIs are accessible.'),
- 'type': 'success',
- 'sticky': False,
- }
- }
-
- except requests.exceptions.Timeout:
- raise UserError(_('Connection timeout. Please check your internet connection.'))
- except requests.exceptions.ConnectionError:
- raise UserError(_('Connection error. Please check your internet connection.'))
- except Exception as e:
- raise UserError(_('Connection test failed: %s') % str(e))
- def action_validate_credentials(self):
- """Validate that the credentials are properly formatted"""
- self.ensure_one()
-
- if not self.google_api_client_id:
- raise UserError(_('Client ID is required'))
-
- if not self.google_api_client_secret:
- raise UserError(_('Client Secret is required'))
-
- # Basic format validation
- if not self.google_api_client_id.endswith('.apps.googleusercontent.com'):
- raise UserError(_('Client ID should end with .apps.googleusercontent.com'))
-
- if len(self.google_api_client_secret) < 20:
- raise UserError(_('Client Secret appears to be too short'))
-
- return {
- 'type': 'ir.actions.client',
- 'tag': 'display_notification',
- 'params': {
- 'title': _('Success'),
- 'message': _('Credentials format is valid!'),
- 'type': 'success',
- 'sticky': False,
- }
- }
- def action_show_oauth_redirect_uris(self):
- """Show the redirect URIs that need to be configured in Google Cloud Console"""
- self.ensure_one()
-
- base_url = self.env['ir.config_parameter'].sudo().get_param('web.base.url')
- redirect_uri = f"{base_url}/web/google_oauth_callback"
-
- return {
- 'type': 'ir.actions.client',
- 'tag': 'display_notification',
- 'params': {
- 'title': _('OAuth Redirect URI'),
- 'message': _('Configure this redirect URI in Google Cloud Console:\n\n%s\n\nCopy this URL and add it to your OAuth 2.0 client configuration.') % redirect_uri,
- 'type': 'info',
- 'sticky': True,
- }
- }
- @api.model
- def set_values(self):
- super().set_values()
- IrConfigParameter = self.env['ir.config_parameter'].sudo()
-
- # Set our custom parameters
- IrConfigParameter.set_param('google_api.client_id', self.google_api_client_id or '')
- IrConfigParameter.set_param('google_api.client_secret', self.google_api_client_secret or '')
- IrConfigParameter.set_param('google_api.enabled', self.google_api_enabled)
-
- # Also set the parameters expected by google.service for compatibility
- if self.google_api_client_id:
- IrConfigParameter.set_param('google_drive_client_id', self.google_api_client_id)
- if self.google_api_client_secret:
- IrConfigParameter.set_param('google_drive_client_secret', self.google_api_client_secret)
|