res_company.py 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. # -*- coding: utf-8 -*-
  2. # Part of Odoo. See LICENSE file for full copyright and licensing details.
  3. from odoo import fields, models, api, _
  4. from odoo.exceptions import UserError
  5. class ResCompany(models.Model):
  6. _inherit = 'res.company'
  7. google_drive_crm_folder_id = fields.Char(
  8. string='Google Drive CRM Folder ID',
  9. help='ID del folder en Google Drive para documentos del CRM'
  10. )
  11. google_drive_crm_folder_name = fields.Char(
  12. string='Google Drive CRM Folder Name',
  13. help='Nombre del folder en Google Drive para documentos del CRM',
  14. readonly=True
  15. )
  16. google_drive_crm_enabled = fields.Boolean(
  17. string='Enable Google Drive CRM Integration',
  18. default=False,
  19. help='Habilitar integración con Google Drive para CRM'
  20. )
  21. google_drive_crm_stage_id = fields.Many2one(
  22. 'crm.stage',
  23. string='CRM Stage for Google Drive Folder Creation',
  24. help='Etapa del CRM en la que se creará automáticamente la carpeta en Google Drive'
  25. )
  26. google_drive_crm_field_id = fields.Many2one(
  27. 'ir.model.fields',
  28. string='Google Drive Field',
  29. domain=[('model', '=', 'crm.lead')],
  30. help='Campo opcional de crm.lead que contiene información de Google Drive'
  31. )
  32. @api.onchange('google_drive_crm_folder_id')
  33. def _onchange_google_drive_crm_folder_id(self):
  34. """Update folder name when folder ID changes"""
  35. if self.google_drive_crm_folder_id:
  36. # TODO: Implement Google Drive API call to get folder name
  37. # For now, just clear the name
  38. self.google_drive_crm_folder_name = False
  39. def action_test_google_drive_connection(self):
  40. """Test Google Drive connection for this company using the new service"""
  41. self.ensure_one()
  42. if not self.google_drive_crm_enabled:
  43. raise UserError(_('Google Drive CRM Integration is not enabled for this company'))
  44. if not self.google_drive_crm_folder_id:
  45. raise UserError(_('Please set a Google Drive CRM Folder ID first'))
  46. try:
  47. # Use the new Google Drive service
  48. drive_service = self.env['google.drive.service']
  49. # Validate the folder ID using the new service
  50. validation = drive_service.validate_folder_id(self.google_drive_crm_folder_id)
  51. if not validation.get('valid'):
  52. raise UserError(_('Google Drive connection test failed: %s') % validation.get('error'))
  53. folder_name = validation.get('name', 'Unknown')
  54. folder_type = validation.get('type', 'Unknown')
  55. # Update the folder name in the record
  56. self.google_drive_crm_folder_name = folder_name
  57. return {
  58. 'type': 'ir.actions.client',
  59. 'tag': 'display_notification',
  60. 'params': {
  61. 'title': _('Success'),
  62. 'message': _('Google Drive connection test successful! Folder "%s" (%s) is accessible.') % (folder_name, folder_type),
  63. 'type': 'success',
  64. 'sticky': False,
  65. }
  66. }
  67. except Exception as e:
  68. raise UserError(_('Google Drive connection test failed: %s') % str(e))
  69. def action_open_google_drive_folder(self):
  70. """Open Google Drive folder in browser using the new service"""
  71. self.ensure_one()
  72. if not self.google_drive_crm_folder_id:
  73. raise UserError(_('No Google Drive CRM folder configured'))
  74. try:
  75. # Use the new Google Drive service
  76. drive_service = self.env['google.drive.service']
  77. # Get the folder URL using the new service
  78. folder_url = drive_service.get_folder_url(self.google_drive_crm_folder_id)
  79. if not folder_url:
  80. raise UserError(_('Could not generate folder URL'))
  81. return {
  82. 'type': 'ir.actions.act_url',
  83. 'url': folder_url,
  84. 'target': 'new',
  85. }
  86. except Exception as e:
  87. raise UserError(_('Failed to open Google Drive folder: %s') % str(e))
  88. def action_create_google_drive_folder(self):
  89. """Create a test folder in Google Drive using the new service"""
  90. self.ensure_one()
  91. if not self.google_drive_crm_enabled:
  92. raise UserError(_('Google Drive CRM Integration is not enabled for this company'))
  93. if not self.google_drive_crm_folder_id:
  94. raise UserError(_('Please set a Google Drive CRM Folder ID first'))
  95. try:
  96. from datetime import datetime
  97. # Use the new Google Drive service
  98. drive_service = self.env['google.drive.service']
  99. # Create folder using the new service
  100. folder_name = f"CRM Folder - {self.name} - {datetime.now().strftime('%Y-%m-%d %H:%M')}"
  101. result = drive_service.create_folder(
  102. name=folder_name,
  103. parent_folder_id=self.google_drive_crm_folder_id,
  104. description="Test folder created by Odoo CRM integration using new service"
  105. )
  106. if not result.get('success'):
  107. raise UserError(_('Failed to create folder: %s') % result.get('error'))
  108. return {
  109. 'type': 'ir.actions.client',
  110. 'tag': 'display_notification',
  111. 'params': {
  112. 'title': _('Success'),
  113. 'message': _('Google Drive folder created successfully: %s') % result.get('folder_name'),
  114. 'type': 'success',
  115. 'sticky': False,
  116. }
  117. }
  118. except Exception as e:
  119. raise UserError(_('Failed to create Google Drive folder: %s') % str(e))
  120. def action_list_google_drive_folders(self):
  121. """List folders in Google Drive using the new service"""
  122. self.ensure_one()
  123. if not self.google_drive_crm_enabled:
  124. raise UserError(_('Google Drive CRM Integration is not enabled for this company'))
  125. if not self.google_drive_crm_folder_id:
  126. raise UserError(_('Please set a Google Drive CRM Folder ID first'))
  127. try:
  128. # Use the new Google Drive service
  129. drive_service = self.env['google.drive.service']
  130. # List folders using the new service
  131. result = drive_service.list_folders(
  132. parent_folder_id=self.google_drive_crm_folder_id,
  133. include_shared_drives=True
  134. )
  135. if not result.get('success'):
  136. raise UserError(_('Failed to list folders: %s') % result.get('error'))
  137. folders = result.get('folders', [])
  138. if not folders:
  139. message = _('No folders found in the specified Google Drive location.')
  140. else:
  141. folder_names = [folder.get('name', 'Unknown') for folder in folders]
  142. message = _('Found %d folders: %s') % (len(folders), ', '.join(folder_names))
  143. return {
  144. 'type': 'ir.actions.client',
  145. 'tag': 'display_notification',
  146. 'params': {
  147. 'title': _('Google Drive Folders'),
  148. 'message': message,
  149. 'type': 'info',
  150. 'sticky': False,
  151. }
  152. }
  153. except Exception as e:
  154. raise UserError(_('Failed to list Google Drive folders: %s') % str(e))
  155. # CRM Calendar Sync methods removed