refactor all the things

This commit is contained in:
Tobias Brunner 2025-01-28 13:55:43 +01:00
parent 8ed39690f1
commit bb5cb708bd
No known key found for this signature in database
36 changed files with 1563 additions and 931 deletions

View file

@ -18,7 +18,7 @@ class OdooAPI:
# 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
host = parsed_url.netloc or parsed_url.path
# Log connection attempt
logger.info(f"Attempting to connect to Odoo at {host}")
@ -27,10 +27,8 @@ class OdooAPI:
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"],
@ -39,7 +37,6 @@ class OdooAPI:
)
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}")
@ -63,6 +60,25 @@ class OdooAPI:
f"Attempting to create lead for {lead.name} from {lead.company}"
)
# Get provider name from offering if it exists
provider_name = ""
if hasattr(lead, "offering") and lead.offering:
provider_name = lead.offering.cloud_provider.name
# Prepare service description
service_details = []
if lead.service:
service_details.append(f"Service: {lead.service.name}")
if provider_name:
service_details.append(f"Provider: {provider_name}")
if lead.service.categories.exists():
categories = ", ".join(
cat.name for cat in lead.service.categories.all()
)
service_details.append(f"Categories: {categories}")
if lead.plan:
service_details.append(f"Plan: {lead.plan.name}")
# Prepare lead data
lead_data = {
"name": f"Interest in {lead.service.name}",
@ -70,11 +86,7 @@ class OdooAPI:
"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())}
""",
"description": "\n".join(service_details),
"type": "lead",
}
@ -85,20 +97,18 @@ class OdooAPI:
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
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
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>
<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>
<h3>Contact Details:</h3>
<p>Contact Details:</p>
<ul>
<li><strong>Name:</strong> {lead.name}</li>
<li><strong>Company:</strong> {lead.company}</li>
@ -106,11 +116,11 @@ class OdooAPI:
<li><strong>Phone:</strong> {lead.phone}</li>
</ul>
<h3>Service Details:</h3>
<p>Service Details:<p>
<ul>
<li><strong>Service:</strong> {lead.service.name}</li>
<li><strong>Provider:</strong> {lead.service.cloud_provider.name}</li>
<li><strong>Plan:</strong> {lead.plan.name if lead.plan else 'Not specified'}</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 ''}
<li><strong>Categories:</strong> {', '.join(cat.name for cat in lead.service.categories.all())}</li>
</ul>
""",
@ -118,16 +128,7 @@ class OdooAPI:
"subtype_xmlid": "mail.mt_comment",
}
# Get context for message posting
context = {
"mail_post_autofollow": True,
"lang": "en_US",
"tz": "UTC",
"uid": self.odoo.env.uid,
"allowed_company_ids": [1], # Default company ID
}
# Post the message using the message_post method
# Post the message
self.odoo.env["crm.lead"].browse(odoo_lead_id).message_post(
body=message_data["body"],
message_type=message_data["message_type"],
@ -135,7 +136,6 @@ class OdooAPI:
)
logger.info(f"Successfully posted message to lead {odoo_lead_id}")
return odoo_lead_id
except odoorpc.error.RPCError as e: