website/hub/services/odoo.py

188 lines
7.8 KiB
Python

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"""
try:
# Parse URL to get host
url = settings.ODOO_CONFIG["url"]
parsed_url = urlparse(url)
host = parsed_url.netloc or parsed_url.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']}"
)
self.odoo = odoorpc.ODOO(host, port=443, protocol="jsonrpc+ssl")
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")
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, source="form"):
"""Create a lead in Odoo"""
try:
logger.info(f"Attempting to create lead for {lead.name}")
# Prepare service description
service_details = []
if hasattr(lead, "service") and lead.service:
service_details.append(f"Service: {lead.service.name}")
# Get provider name from offering if it exists
if (
hasattr(lead, "offering")
and lead.offering
and hasattr(lead.offering, "cloud_provider")
):
provider_name = lead.offering.cloud_provider.name
service_details.append(f"Provider: {provider_name}")
if hasattr(lead, "plan") and lead.plan:
service_details.append(f"Plan: {lead.plan.name}")
if source == "form":
service_details.append(f"Source: Servala Website")
elif source == "osbapi":
service_details.append(f"Source: OSB API")
elif source == "contact_form":
service_details.append(f"Source: Contact Form")
# Prepare lead name based on whether it's a service lead or generic contact
if hasattr(lead, "service") and lead.service:
lead_name = f"Servala - Interest in {lead.service.name}"
else:
lead_name = "Servala - Website Contact"
# Prepare lead data
lead_data = {
"name": lead_name,
"contact_name": lead.name,
"partner_name": (
lead.company if hasattr(lead, "company") and lead.company else ""
),
"email_from": lead.email,
"phone": lead.phone if hasattr(lead, "phone") and lead.phone else "",
"description": (
f"{lead.message}<br/><br/>" + "<br/>".join(service_details)
if lead.message
else "<br/>".join(service_details)
),
"type": "lead",
"campaign_id": settings.ODOO_CONFIG["campaign_id"],
"source_id": settings.ODOO_CONFIG["source_id"],
"medium_id": settings.ODOO_CONFIG["medium_id"],
"tag_ids": [(4, settings.ODOO_CONFIG["tag_id"])],
}
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()
# Create the lead
Lead = self.odoo.env["crm.lead"]
odoo_lead_id = Lead.create(lead_data)
logger.info(f"Successfully created lead in Odoo with ID: {odoo_lead_id}")
# Post message in chatter
if hasattr(lead, "service") and lead.service:
# For service-related leads
message_data = {
"body": f"""
<p>Thank you for your interest in the service <strong>{lead.service.name}</strong>.
We recorded the following information and will get back to you soon:</p><br/>
<p>Contact Details:</p>
<ul>
<li><strong>Name:</strong> {lead.name}</li>
<li><strong>Company:</strong> {lead.company if hasattr(lead, "company") and lead.company else "N/A"}</li>
<li><strong>Email:</strong> {lead.email}</li>
<li><strong>Phone:</strong> {lead.phone if hasattr(lead, "phone") and lead.phone else "N/A"}</li>
</ul>
<p>Service Details:<p>
<ul>
<li><strong>Service:</strong> {lead.service.name}</li>
{f'<li><strong>Provider:</strong> {provider_name}</li>' if provider_name else ''}
{f'<li><strong>Plan:</strong> {lead.plan.name}</li>' if lead.plan else ''}
</ul>
""",
"message_type": "comment",
"subtype_xmlid": "mail.mt_comment",
}
else:
# For contact form submissions
message_data = {
"body": f"""
<p>Thank you for contacting Servala by VSHN.<br/>
We recorded the following information and will get back to you soon:</p><br/>
<p>Contact Details:</p>
<ul>
<li><strong>Name:</strong> {lead.name}</li>
<li><strong>Email:</strong> {lead.email}</li>
{f'<li><strong>Phone:</strong> {lead.phone}</li>' if lead.phone else ''}
{f'<li><strong>Company:</strong> {lead.company}</li>' if lead.company else ''}
{f'<li><strong>Message:</strong> {lead.message}</li>' if lead.message else ''}
</ul>
""",
"message_type": "comment",
"subtype_xmlid": "mail.mt_comment",
}
# Post the message
self.odoo.env["crm.lead"].browse(odoo_lead_id).message_post(
body=message_data["body"],
message_type=message_data["message_type"],
subtype_xmlid=message_data["subtype_xmlid"],
)
logger.info(f"Successfully posted message to lead {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