bemade-addons/odoo_to_odoo_sync/models/sync_project.py

126 lines
4.4 KiB
Python
Raw Normal View History

feat: Complete bidirectional sync system with legacy cleanup BREAKING CHANGE: Legacy API key system (sync.api.key) completely removed Major Features: - Complete bidirectional synchronization system between Odoo instances - Legacy API key system removal and migration to built-in Odoo API keys - Full protocol support: XML-RPC, JSON-RPC, and OdooRPC with API token authentication - Bidirectional project sync with assign/receive wizard system - Auto-sync wizard for intelligent field selection - Comprehensive test suite for sync workflows Detailed Changes: odoo_to_odoo_sync (base module): - Removed legacy sync.api.key and sync.api.key.log models entirely - Added sync_project and sync_project_config_wizard models - Enhanced sync_instance with proper Odoo 18 API key format support - Added protocol selection (XML-RPC, JSON-RPC, OdooRPC) with API token auth - Implemented comprehensive authentication with debug logging - Added test_sync_workflow.py for complete workflow testing - Updated security access rules for new models - Enhanced error handling and state management odoo_to_odoo_bemade (server module): - Created OdooToBemadeInstance model for enhanced instance management - Added assign project wizard for server-side project distribution - Implemented project key and API token generation system - Added automatic sync model creation for projects and tasks - Enhanced sync_instance with bidirectional sync capabilities - Updated views and security access for new functionality - Removed legacy API key references and views odoo_to_odoo_bemade_customer (client module): - Added project model with Bemade sync flags (is_bemade_project, bemade_project_key) - Created receive project wizard for client-side project acquisition - Implemented multi-protocol support (XML-RPC, JSON-RPC, OdooRPC) - Added project sync validation and configuration system - Enhanced security access rules for client operations - Updated views for project management interface Authentication & Protocol Support: - Fixed Odoo 18 API key format: {'scope': 'rpc', 'key': api_token} - Added comprehensive debug logging for authentication flow - Enhanced error handling with detailed state management - Maintained backward compatibility with fallback attempts - Updated all protocol implementations (XML-RPC, JSON-RPC, OdooRPC) Testing & Validation: - Added complete test suite for bidirectional sync workflow - Implemented comprehensive authentication testing - Added protocol-specific test cases - Enhanced error state validation UI/UX Improvements: - Added intuitive wizards for project assign/receive operations - Implemented auto-sync wizard with field selection modes (Full, Required, Balanced) - Enhanced form views with radio button selections - Added descriptive text and user guidance Security: - Updated security access rules for all new models - Enhanced authentication token handling - Added proper model access controls Migration Notes: - Legacy API key system completely removed - no migration path needed - Uses built-in Odoo API key functionality - All existing configurations remain compatible Files Added/Modified: - Multiple new model files for project sync functionality - Wizard implementations for user workflows - Enhanced test coverage - Updated security rules and access controls - New view definitions for UI components
2025-08-17 08:20:24 -04:00
# Copyright 2025 Bemade
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0.html)
import logging
import secrets
from odoo import api, fields, models, _
from odoo.exceptions import UserError
_logger = logging.getLogger(__name__)
class SyncProject(models.Model):
_name = 'sync.project'
_description = 'Synchronization Project'
_inherit = ['mail.thread', 'mail.activity.mixin']
name = fields.Char(string='Project Name', required=True)
active = fields.Boolean(string='Active', default=True)
# Project identification
is_sync_project = fields.Boolean(string='Synchronization Project', default=False)
is_client_project = fields.Boolean(string='Client Project', default=False)
# API Token management
client_key = fields.Char(string='Client Key', readonly=True)
api_token = fields.Char(string='API Token', readonly=True)
# Connection details
remote_url = fields.Char(string='Remote URL')
remote_database = fields.Char(string='Remote Database')
remote_username = fields.Char(string='Remote Username')
# Sync configuration
sync_instance_id = fields.Many2one(
'odoo.sync.instance', string='Sync Instance',
help='The sync instance associated with this project'
)
sync_model_ids = fields.One2many(
'odoo.sync.model', 'project_id', string='Synchronized Models',
help='Models configured for synchronization with this project'
)
state = fields.Selection([
('draft', 'Draft'),
('configured', 'Configured'),
('connected', 'Connected'),
('syncing', 'Syncing'),
('error', 'Error'),
], string='Status', default='draft', tracking=True)
last_sync_date = fields.Datetime(string='Last Sync Date')
error_message = fields.Text(string='Error Message')
@api.onchange('is_client_project')
def _onchange_is_client_project(self):
"""Generate API token when project is marked as client project."""
if self.is_client_project and not self.api_token:
self.api_token = self._generate_api_token()
self.client_key = self._generate_client_key()
elif not self.is_client_project:
self.api_token = False
self.client_key = False
def _generate_api_token(self):
"""Generate a secure API token."""
return secrets.token_urlsafe(32)
def _generate_client_key(self):
"""Generate a unique client key."""
return f"{self.id or 'new'}-{secrets.token_urlsafe(8)}"
def action_generate_api_token(self):
"""Manually generate or regenerate API token."""
for record in self:
if not record.is_client_project:
raise UserError(_("Only client projects can have API tokens."))
record.api_token = record._generate_api_token()
record.client_key = record._generate_client_key()
return True
def action_revoke_api_token(self):
"""Revoke the API token."""
for record in self:
record.api_token = False
record.client_key = False
record.sync_instance_id = False
return True
def action_configure_sync(self):
"""Open the sync configuration wizard."""
self.ensure_one()
return {
'type': 'ir.actions.act_window',
'name': _('Configure Sync'),
'res_model': 'sync.project.config.wizard',
'view_mode': 'form',
'target': 'new',
'context': {
'default_project_id': self.id,
}
}
def action_test_connection(self):
"""Test the connection to the remote instance."""
self.ensure_one()
if not self.sync_instance_id:
raise UserError(_("Please configure a sync instance first."))
try:
self.sync_instance_id.test_connection()
self.state = 'connected'
self.error_message = False
except Exception as e:
self.state = 'error'
self.error_message = str(e)
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': _('Connection Test'),
'message': _('Connection test completed.'),
'type': 'success' if self.state == 'connected' else 'danger',
}
}