add newsletter subscription functionality

This commit is contained in:
Tobias Brunner 2025-03-27 10:33:27 +01:00
parent 0dd73bedde
commit 92a8b0951e
No known key found for this signature in database
9 changed files with 132 additions and 4 deletions

View file

@ -97,3 +97,70 @@ class OdooAPI:
logger.error(f"Unexpected error creating lead: {str(e)}")
logger.debug("Full error details:", exc_info=True)
raise
def add_to_mailing_list(self, email, mailing_list_id=46):
"""Add an email to a mailing list in Odoo"""
try:
logger.info(
f"Attempting to add {email} to mailing list ID {mailing_list_id}"
)
# Ensure we have a valid connection
if not self.odoo:
logger.warning("No active Odoo connection, attempting to reconnect")
self._connect()
# Add contact to mailing list
MailingContact = self.odoo.env["mailing.contact"]
existing_mailing_contacts = MailingContact.search([("email", "=", email)])
if existing_mailing_contacts:
mailing_contact_id = existing_mailing_contacts[0]
# Check if already in this list
MailingContactList = self.odoo.env["mailing.contact.subscription"]
existing_subscriptions = MailingContactList.search(
[
("contact_id", "=", mailing_contact_id),
("list_id", "=", mailing_list_id),
]
)
if not existing_subscriptions:
# Add to this specific list
MailingContactList.create(
{
"contact_id": mailing_contact_id,
"list_id": mailing_list_id,
"opt_out": False,
}
)
else:
# Create new mailing contact and subscription
mailing_contact_id = MailingContact.create(
{
"name": email.split("@")[0],
"email": email,
}
)
MailingContactList = self.odoo.env["mailing.contact.subscription"]
MailingContactList.create(
{
"contact_id": mailing_contact_id,
"list_id": mailing_list_id,
"opt_out": False,
}
)
logger.info(f"Successfully added {email} to mailing list")
return True
except odoorpc.error.RPCError as e:
logger.error(f"RPC Error adding to mailing list: {str(e)}")
logger.debug("Full RPC error details:", exc_info=True)
return False
except Exception as e:
logger.error(f"Unexpected error adding to mailing list: {str(e)}")
logger.debug("Full error details:", exc_info=True)
return False