odoo lead creation
This commit is contained in:
parent
b98a507f65
commit
483f076d1a
15 changed files with 404 additions and 27 deletions
105
hub/services/odoo.py
Normal file
105
hub/services/odoo.py
Normal file
|
@ -0,0 +1,105 @@
|
|||
import odoorpc
|
||||
import logging
|
||||
from django.conf import settings
|
||||
from urllib.parse import urlparse
|
||||
|
||||
# Set up logging
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
class OdooAPI:
|
||||
def __init__(self):
|
||||
self.odoo = None
|
||||
self._connect()
|
||||
|
||||
def _connect(self):
|
||||
"""Establish connection to Odoo with detailed error logging"""
|
||||
try:
|
||||
# Parse URL to get host
|
||||
url = settings.ODOO_CONFIG["url"]
|
||||
parsed_url = urlparse(url)
|
||||
host = parsed_url.netloc or parsed_url.path # If no netloc, use path
|
||||
|
||||
# Log connection attempt
|
||||
logger.info(f"Attempting to connect to Odoo at {host}")
|
||||
logger.debug(
|
||||
f"Connection parameters: HOST={host}, DB={settings.ODOO_CONFIG['db']}, "
|
||||
f"USER={settings.ODOO_CONFIG['username']}"
|
||||
)
|
||||
|
||||
# Try to establish connection
|
||||
self.odoo = odoorpc.ODOO(host, port=443, protocol="jsonrpc+ssl")
|
||||
|
||||
# Try to login
|
||||
logger.info("Connection established, attempting login...")
|
||||
self.odoo.login(
|
||||
settings.ODOO_CONFIG["db"],
|
||||
settings.ODOO_CONFIG["username"],
|
||||
settings.ODOO_CONFIG["password"],
|
||||
)
|
||||
logger.info("Successfully logged into Odoo")
|
||||
|
||||
# Test the connection by making a simple API call
|
||||
version_info = self.odoo.version
|
||||
logger.info(f"Connected to Odoo version: {version_info}")
|
||||
|
||||
except odoorpc.error.RPCError as e:
|
||||
logger.error(f"RPC Error connecting to Odoo: {str(e)}")
|
||||
logger.debug("Full RPC error details:", exc_info=True)
|
||||
raise
|
||||
except odoorpc.error.InternalError as e:
|
||||
logger.error(f"Internal Odoo error: {str(e)}")
|
||||
logger.debug("Full internal error details:", exc_info=True)
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error connecting to Odoo: {str(e)}")
|
||||
logger.debug("Full error details:", exc_info=True)
|
||||
raise
|
||||
|
||||
def create_lead(self, lead):
|
||||
"""Create a lead in Odoo with detailed logging"""
|
||||
try:
|
||||
logger.info(
|
||||
f"Attempting to create lead for {lead.name} from {lead.company}"
|
||||
)
|
||||
|
||||
# Prepare lead data
|
||||
lead_data = {
|
||||
"name": f"Interest in {lead.service.name}",
|
||||
"contact_name": lead.name,
|
||||
"partner_name": lead.company,
|
||||
"email_from": lead.email,
|
||||
"phone": lead.phone,
|
||||
"description": f"""
|
||||
Service: {lead.service.name}
|
||||
Provider: {lead.service.cloud_provider.name}
|
||||
Categories: {', '.join(cat.name for cat in lead.service.categories.all())}
|
||||
""",
|
||||
"type": "lead",
|
||||
}
|
||||
|
||||
logger.debug(f"Prepared lead data: {lead_data}")
|
||||
|
||||
# Ensure we have a valid connection
|
||||
if not self.odoo:
|
||||
logger.warning("No active Odoo connection, attempting to reconnect")
|
||||
self._connect()
|
||||
|
||||
# Get the CRM lead model
|
||||
Lead = self.odoo.env["crm.lead"]
|
||||
logger.debug("Successfully got CRM lead model")
|
||||
|
||||
# Create the lead
|
||||
odoo_lead_id = Lead.create(lead_data)
|
||||
logger.info(f"Successfully created lead in Odoo with ID: {odoo_lead_id}")
|
||||
|
||||
return odoo_lead_id
|
||||
|
||||
except odoorpc.error.RPCError as e:
|
||||
logger.error(f"RPC Error creating lead: {str(e)}")
|
||||
logger.debug("Full RPC error details:", exc_info=True)
|
||||
raise
|
||||
except Exception as e:
|
||||
logger.error(f"Unexpected error creating lead: {str(e)}")
|
||||
logger.debug("Full error details:", exc_info=True)
|
||||
raise
|
Loading…
Add table
Add a link
Reference in a new issue