post message in chatter

This commit is contained in:
Tobias Brunner 2025-01-28 10:54:35 +01:00
parent 70f4a02db9
commit 7b21d9d612
No known key found for this signature in database
3 changed files with 68 additions and 0 deletions

View file

@ -0,0 +1,24 @@
# Generated by Django 5.1.5 on 2025-01-28 09:48
import django.db.models.deletion
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("services", "0010_remove_service_price"),
]
operations = [
migrations.AddField(
model_name="lead",
name="plan",
field=models.ForeignKey(
blank=True,
null=True,
on_delete=django.db.models.deletion.SET_NULL,
to="services.plan",
),
),
]

View file

@ -195,6 +195,7 @@ class Service(models.Model):
class Lead(models.Model):
service = models.ForeignKey(Service, on_delete=models.CASCADE)
plan = models.ForeignKey(Plan, on_delete=models.SET_NULL, null=True, blank=True)
name = models.CharField(max_length=200)
company = models.CharField(max_length=200)
email = models.EmailField()

View file

@ -93,6 +93,49 @@ class OdooAPI:
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>
<h3>Contact Details:</h3>
<ul>
<li><strong>Name:</strong> {lead.name}</li>
<li><strong>Company:</strong> {lead.company}</li>
<li><strong>Email:</strong> {lead.email}</li>
<li><strong>Phone:</strong> {lead.phone}</li>
</ul>
<h3>Service Details:</h3>
<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>
<li><strong>Categories:</strong> {', '.join(cat.name for cat in lead.service.categories.all())}</li>
</ul>
""",
"message_type": "comment",
"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
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: