bemade-addons/odoo_to_odoo_bemade/models/sync_model_field.py

130 lines
4.6 KiB
Python
Raw Normal View History

# Copyright 2025 Bemade
# License LGPL-3.0 or later (https://www.gnu.org/licenses/lgpl-3.0.html)
"""Field Synchronization Configuration for Bemade.
This module extends the base field synchronization configuration to add
Bemade-specific functionality for defining how fields are synchronized
between Odoo instances.
"""
import logging
from odoo import api, fields, models
_logger = logging.getLogger(__name__)
class OdooToBemadeSyncModelField(models.Model):
"""Bemade synchronization field configuration.
Extends the base synchronization field configuration to handle
Bemade-specific requirements for field synchronization.
"""
_name = 'odoo.to.bemade.sync.model.field'
_description = 'Bemade Synchronized Field'
_inherit = 'odoo.sync.model.field'
# Add Bemade-specific fields here
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
transform_type = fields.Selection([
('direct', 'Direct'),
('function', 'Function'),
('computed', 'Computed'),
('relation', 'Relation')
], string='Transform Type', default='direct',
help='Type of transformation to apply to the field value')
bemade_transformation = fields.Selection([
('none', 'No Transformation'),
('prefix', 'Add Prefix'),
('suffix', 'Add Suffix'),
('replace', 'Find and Replace'),
('custom', 'Custom Python Code')
], string='Bemade Transformation', default='none',
help='Bemade-specific transformation to apply to the field value')
bemade_transform_value = fields.Char(
string='Transformation Value',
help='Value to use for the transformation (prefix, suffix, replacement, etc.)'
)
bemade_custom_code = fields.Text(
string='Custom Python Code',
help='Custom Python code to transform the field value'
)
@api.model
def create_field_mapping(self, sync_model_id, source_field, target_field=None, active=True, transform_type='direct'):
"""Create a field mapping for synchronization.
Args:
sync_model_id: ID of the sync model configuration
source_field: Name of the source field
target_field: Name of the target field (defaults to source_field)
active: Whether the mapping is active
transform_type: Type of transformation to apply
Returns:
The created field mapping record
"""
return self.create({
'sync_model_id': sync_model_id,
'source_field': source_field,
'target_field': target_field or source_field,
'active': active,
'transform_type': transform_type,
'bemade_transformation': 'none',
})
def apply_bemade_transformation(self, value, record):
"""Apply Bemade-specific transformations to the field value.
Args:
value: The original field value
record: The record being synchronized
Returns:
The transformed value
"""
self.ensure_one()
if not value or self.bemade_transformation == 'none':
return value
if self.bemade_transformation == 'prefix':
return f"{self.bemade_transform_value or ''}{value}"
elif self.bemade_transformation == 'suffix':
return f"{value}{self.bemade_transform_value or ''}"
elif self.bemade_transformation == 'replace' and self.bemade_transform_value:
# Format: "find:replace"
parts = self.bemade_transform_value.split(':')
if len(parts) >= 2:
find_val = parts[0]
replace_val = parts[1]
return str(value).replace(find_val, replace_val)
elif self.bemade_transformation == 'custom' and self.bemade_custom_code:
# Execute custom code with appropriate safeguards
try:
# Create a safe local execution environment
local_dict = {
'value': value,
'record': record,
'field': self,
}
# Execute the custom code
exec(self.bemade_custom_code, {'__builtins__': {}}, local_dict)
# Return the transformed value
if 'result' in local_dict:
return local_dict['result']
else:
_logger.warning("Custom transformation code did not set 'result' variable")
except Exception as e:
_logger.error("Error executing custom transformation: %s", str(e))
return value