bemade-addons/odoo_to_odoo_bemade/wizards/assign_project_wizard.py
mathis 9df769be69 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

159 lines
6.2 KiB
Python

# -*- coding: utf-8 -*-
import uuid
import random
import string
from odoo import api, fields, models, _
from odoo.exceptions import UserError, ValidationError
class OdooToBemadeAssignProjectWizard(models.TransientModel):
_name = 'odoo.to.bemade.assign.project.wizard'
_description = 'Assign Project to Bemade Client'
project_id = fields.Many2one('project.project', string='Project', required=True)
client_instance_id = fields.Many2one('odoo.to.bemade.instance', string='Client Instance', required=True)
project_key = fields.Char(string='Project Key', readonly=True)
client_api_token = fields.Char(string='Client API Token', readonly=True)
sync_project_task = fields.Boolean(string='Sync Tasks', default=True)
sync_project_timesheet = fields.Boolean(string='Sync Timesheets', default=True)
protocol = fields.Selection([
('xmlrpc', 'XML-RPC'),
('jsonrpc', 'JSON-RPC'),
('odoorpc', 'OdooRPC')
], string='Protocol', default='jsonrpc', required=True)
@api.model
def default_get(self, fields_list):
"""Prefill defaults from system parameters and context.
- project_id from context (default_project_id)
- protocol from bemade.sync.default_connection_type
- client_instance_id from existing instance matching URL/DB/username
"""
res = super().default_get(fields_list)
ICP = self.env['ir.config_parameter'].sudo()
# Context default project
if 'project_id' in fields_list and self.env.context.get('default_project_id'):
res['project_id'] = self.env.context['default_project_id']
# Protocol default
proto = ICP.get_param('bemade.sync.default_connection_type') or 'jsonrpc'
if 'protocol' in fields_list and proto:
res.setdefault('protocol', proto)
# Try to preselect an instance based on defaults
url = ICP.get_param('bemade.sync.default_url')
database = ICP.get_param('bemade.sync.default_database')
username = ICP.get_param('bemade.sync.default_username')
domain = []
if url:
domain.append(('url', '=', url))
if database:
domain.append(('database', '=', database))
if username:
domain.append(('username', '=', username))
if 'client_instance_id' in fields_list and domain:
instance = self.env['odoo.to.bemade.instance'].search(domain, limit=1)
if instance:
res.setdefault('client_instance_id', instance.id)
return res
@api.onchange('project_id', 'client_instance_id')
def _onchange_generate_keys(self):
"""Generate unique project key and API token"""
if self.project_id and self.client_instance_id:
# Generate random 8-character project key
chars = string.ascii_uppercase + string.digits
self.project_key = ''.join(random.choice(chars) for _ in range(8))
# Generate UUID for API token
self.client_api_token = str(uuid.uuid4())
def action_assign_project(self):
"""Assign project to client and create sync configuration"""
self.ensure_one()
if not self.project_id:
raise UserError(_("Please select a project to assign"))
if not self.client_instance_id:
raise UserError(_("Please select a client instance"))
if not self.project_key or not self.client_api_token:
self._onchange_generate_keys()
# Create sync project
sync_project_vals = {
'name': f"{self.project_id.name} - {self.client_instance_id.name}",
'project_id': self.project_id.id,
'client_instance_id': self.client_instance_id.id,
'state': 'draft',
'is_client_project': True,
'client_key': self.project_key,
'client_api_token': self.client_api_token,
'remote_url': self.client_instance_id.url,
'remote_database': self.client_instance_id.database,
'remote_username': self.client_instance_id.username,
'remote_api_key': self.client_instance_id.api_key,
'protocol': self.protocol,
}
sync_project = self.env['sync.project'].create(sync_project_vals)
# Update project with bemade flags
self.project_id.write({
'is_bemade_project': True,
'bemade_sync_enabled': True,
'bemade_project_key': self.project_key,
})
# Create sync models for project and tasks
self._create_sync_model(sync_project, 'project.project')
if self.sync_project_task:
self._create_sync_model(sync_project, 'project.task')
if self.sync_project_timesheet and hasattr(self.env, 'account.analytic.line'):
self._create_sync_model(sync_project, 'account.analytic.line')
# Show success message
return {
'type': 'ir.actions.client',
'tag': 'display_notification',
'params': {
'title': _('Project Assigned'),
'message': _(f'Project {self.project_id.name} has been assigned to {self.client_instance_id.name} with key {self.project_key}'),
'sticky': False,
'next': {
'type': 'ir.actions.act_window',
'name': _('Sync Project'),
'res_model': 'sync.project',
'res_id': sync_project.id,
'view_mode': 'form',
'target': 'current',
}
}
}
def _create_sync_model(self, sync_project, model_name):
"""Create sync model configuration for the given model"""
model_vals = {
'name': f"{model_name} - {sync_project.name}",
'sync_project_id': sync_project.id,
'model_name': model_name,
'direction': 'bidirectional',
'state': 'draft',
}
sync_model = self.env['sync.model'].create(model_vals)
# Auto-populate fields based on model
if hasattr(sync_model, 'action_auto_sync_fields'):
sync_model.action_auto_sync_fields()
return sync_model