# -*- coding: utf-8 -*- # Part of Odoo. See LICENSE file for full copyright and licensing details. from odoo import fields, models, api, _ from odoo.exceptions import UserError class ResCompany(models.Model): _inherit = 'res.company' google_drive_crm_folder_id = fields.Char( string='Google Drive CRM Folder ID', help='ID del folder en Google Drive para documentos del CRM' ) google_drive_crm_folder_name = fields.Char( string='Google Drive CRM Folder Name', help='Nombre del folder en Google Drive para documentos del CRM', readonly=True ) google_drive_crm_enabled = fields.Boolean( string='Enable Google Drive CRM Integration', default=False, help='Habilitar integración con Google Drive para CRM' ) google_drive_crm_stage_id = fields.Many2one( 'crm.stage', string='CRM Stage for Google Drive Folder Creation', help='Etapa del CRM en la que se creará automáticamente la carpeta en Google Drive' ) google_drive_crm_field_id = fields.Many2one( 'ir.model.fields', string='Google Drive Field', domain=[('model', '=', 'crm.lead')], help='Campo opcional de crm.lead que contiene información de Google Drive' ) @api.onchange('google_drive_crm_folder_id') def _onchange_google_drive_crm_folder_id(self): """Update folder name when folder ID changes""" if self.google_drive_crm_folder_id: # TODO: Implement Google Drive API call to get folder name # For now, just clear the name self.google_drive_crm_folder_name = False def action_test_google_drive_connection(self): """Test Google Drive connection for this company using the new service""" self.ensure_one() if not self.google_drive_crm_enabled: raise UserError(_('Google Drive CRM Integration is not enabled for this company')) if not self.google_drive_crm_folder_id: raise UserError(_('Please set a Google Drive CRM Folder ID first')) try: # Use the new Google Drive service drive_service = self.env['google.drive.service'] # Validate the folder ID using the new service validation = drive_service.validate_folder_id(self.google_drive_crm_folder_id) if not validation.get('valid'): raise UserError(_('Google Drive connection test failed: %s') % validation.get('error')) folder_name = validation.get('name', 'Unknown') folder_type = validation.get('type', 'Unknown') # Update the folder name in the record self.google_drive_crm_folder_name = folder_name return { 'type': 'ir.actions.client', 'tag': 'display_notification', 'params': { 'title': _('Success'), 'message': _('Google Drive connection test successful! Folder "%s" (%s) is accessible.') % (folder_name, folder_type), 'type': 'success', 'sticky': False, } } except Exception as e: raise UserError(_('Google Drive connection test failed: %s') % str(e)) def action_open_google_drive_folder(self): """Open Google Drive folder in browser using the new service""" self.ensure_one() if not self.google_drive_crm_folder_id: raise UserError(_('No Google Drive CRM folder configured')) try: # Use the new Google Drive service drive_service = self.env['google.drive.service'] # Get the folder URL using the new service folder_url = drive_service.get_folder_url(self.google_drive_crm_folder_id) if not folder_url: raise UserError(_('Could not generate folder URL')) return { 'type': 'ir.actions.act_url', 'url': folder_url, 'target': 'new', } except Exception as e: raise UserError(_('Failed to open Google Drive folder: %s') % str(e)) def action_create_google_drive_folder(self): """Create a test folder in Google Drive using the new service""" self.ensure_one() if not self.google_drive_crm_enabled: raise UserError(_('Google Drive CRM Integration is not enabled for this company')) if not self.google_drive_crm_folder_id: raise UserError(_('Please set a Google Drive CRM Folder ID first')) try: from datetime import datetime # Use the new Google Drive service drive_service = self.env['google.drive.service'] # Create folder using the new service folder_name = f"CRM Folder - {self.name} - {datetime.now().strftime('%Y-%m-%d %H:%M')}" result = drive_service.create_folder( name=folder_name, parent_folder_id=self.google_drive_crm_folder_id, description="Test folder created by Odoo CRM integration using new service" ) if not result.get('success'): raise UserError(_('Failed to create folder: %s') % result.get('error')) return { 'type': 'ir.actions.client', 'tag': 'display_notification', 'params': { 'title': _('Success'), 'message': _('Google Drive folder created successfully: %s') % result.get('folder_name'), 'type': 'success', 'sticky': False, } } except Exception as e: raise UserError(_('Failed to create Google Drive folder: %s') % str(e)) def action_list_google_drive_folders(self): """List folders in Google Drive using the new service""" self.ensure_one() if not self.google_drive_crm_enabled: raise UserError(_('Google Drive CRM Integration is not enabled for this company')) if not self.google_drive_crm_folder_id: raise UserError(_('Please set a Google Drive CRM Folder ID first')) try: # Use the new Google Drive service drive_service = self.env['google.drive.service'] # List folders using the new service result = drive_service.list_folders( parent_folder_id=self.google_drive_crm_folder_id, include_shared_drives=True ) if not result.get('success'): raise UserError(_('Failed to list folders: %s') % result.get('error')) folders = result.get('folders', []) if not folders: message = _('No folders found in the specified Google Drive location.') else: folder_names = [folder.get('name', 'Unknown') for folder in folders] message = _('Found %d folders: %s') % (len(folders), ', '.join(folder_names)) return { 'type': 'ir.actions.client', 'tag': 'display_notification', 'params': { 'title': _('Google Drive Folders'), 'message': message, 'type': 'info', 'sticky': False, } } except Exception as e: raise UserError(_('Failed to list Google Drive folders: %s') % str(e)) # CRM Calendar Sync methods removed