add newsletter subscription functionality
This commit is contained in:
parent
0dd73bedde
commit
92a8b0951e
9 changed files with 132 additions and 4 deletions
|
@ -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
|
||||
|
|
1
hub/services/static/js/htmx204.min.js
vendored
Normal file
1
hub/services/static/js/htmx204.min.js
vendored
Normal file
File diff suppressed because one or more lines are too long
|
@ -18,6 +18,7 @@
|
|||
{% block extra_css %}{% endblock %}
|
||||
|
||||
<script defer data-domain="servala.com" src="https://plausible.io/js/script.file-downloads.hash.outbound-links.tagged-events.js"></script>
|
||||
<script defer src="{% static "js/htmx204.min.js" %}"></script>
|
||||
<script defer src="{% static "js/alpine-collapse.min.js" %}"></script>
|
||||
<script defer src="{% static "js/servala-main.js" %}"></script>
|
||||
{% block extra_js %}{% endblock %}
|
||||
|
@ -100,11 +101,18 @@
|
|||
<h2 class="section-h1 fs-40 fs-lg-60 d-inline-block mb-24">Ready to Get Started?</h2>
|
||||
</div>
|
||||
<div class="text-gray-300 w-lg-37 mx-auto mb-24">
|
||||
<p class="mb-0">Explore all available Services on Servala, with new ones added regularly.</p>
|
||||
<p class="mb-0">Subscribe to our newsletter to stay informed.</p>
|
||||
</div>
|
||||
<div>
|
||||
<a class="btn btn-primary btn-lg mr-md-17 mb-17 mb-md-0 w-100 w-md-auto" href="{% url 'services:homepage' %}" role="button">Discover
|
||||
Services</a>
|
||||
<div class="mt-4">
|
||||
<form hx-post="{% url 'services:subscribe' %}" hx-target="#signup-form-container" hx-swap="outerHTML">
|
||||
{% csrf_token %}
|
||||
<div id="signup-form-container" class="d-flex justify-content-center">
|
||||
<div class="input-group" style="max-width: 500px;">
|
||||
<input type="email" name="email" class="form-control" id="email-input" placeholder="Your email address" required>
|
||||
<button type="submit" class="btn btn-primary">Subscribe to updates</button>
|
||||
</div>
|
||||
</div>
|
||||
</form>
|
||||
</div>
|
||||
</header>
|
||||
</div>
|
||||
|
|
7
hub/services/templates/subscriptions/error.html
Normal file
7
hub/services/templates/subscriptions/error.html
Normal file
|
@ -0,0 +1,7 @@
|
|||
<div id="signup-form-container" class="text-center py-3">
|
||||
<div class="alert alert-danger" role="alert">
|
||||
<h4 class="section-h1">Oops!</h4>
|
||||
<p class="text-gray-300">Something went wrong. Please try again later.</p>
|
||||
<button class="btn btn-primary mt-2" onclick="location.reload()">Try Again</button>
|
||||
</div>
|
||||
</div>
|
6
hub/services/templates/subscriptions/success.html
Normal file
6
hub/services/templates/subscriptions/success.html
Normal file
|
@ -0,0 +1,6 @@
|
|||
<div id="signup-form-container" class="text-center py-3">
|
||||
<div role="alert">
|
||||
<h4 class="section-h1">Thank you!</h4>
|
||||
<p class="text-gray-300">You have been successfully subscribed to our mailing list.</p>
|
||||
</div>
|
||||
</div>
|
|
@ -21,4 +21,5 @@ urlpatterns = [
|
|||
path("contact/", views.leads.contact, name="contact"),
|
||||
path("contact/thank-you/", views.thank_you, name="thank_you"),
|
||||
path("contact-form/", views.contact_form, name="contact_form"),
|
||||
path("subscribe/", views.subscribe, name="subscribe"),
|
||||
]
|
||||
|
|
|
@ -4,3 +4,4 @@ from .partners import *
|
|||
from .providers import *
|
||||
from .services import *
|
||||
from .pages import *
|
||||
from .subscriptions import *
|
||||
|
|
36
hub/services/views/subscriptions.py
Normal file
36
hub/services/views/subscriptions.py
Normal file
|
@ -0,0 +1,36 @@
|
|||
import logging
|
||||
from django.conf import settings
|
||||
from django.shortcuts import render
|
||||
from django.views.decorators.http import require_POST
|
||||
from hub.services.odoo import OdooAPI
|
||||
|
||||
logger = logging.getLogger(__name__)
|
||||
|
||||
|
||||
@require_POST
|
||||
def subscribe(request):
|
||||
"""Handle mailing list subscription requests"""
|
||||
email = request.POST.get("email", "")
|
||||
if not email:
|
||||
return render(request, "subscriptions/error.html")
|
||||
|
||||
try:
|
||||
# Get mailing list ID from settings
|
||||
mailing_list_id = settings.ODOO_CONFIG.get("mailing_list_id", 46)
|
||||
|
||||
# Connect to Odoo
|
||||
odoo = OdooAPI()
|
||||
|
||||
# Add subscriber to mailing list
|
||||
result = odoo.add_to_mailing_list(email, mailing_list_id)
|
||||
|
||||
if result:
|
||||
return render(request, "subscriptions/success.html")
|
||||
else:
|
||||
return render(request, "subscriptions/error.html")
|
||||
|
||||
except Exception as e:
|
||||
logger.error(
|
||||
f"Failed to add subscriber to mailing list: {str(e)}", exc_info=True
|
||||
)
|
||||
return render(request, "subscriptions/error.html")
|
|
@ -204,6 +204,7 @@ ODOO_CONFIG = {
|
|||
"source_id": env.str("ODOO_LEAD_SOURCE_ID", default="1"),
|
||||
"medium_id": env.str("ODOO_LEAD_MEDIUM_ID", default="1"),
|
||||
"tag_id": env.str("ODOO_LEAD_TAG_ID", default="1"),
|
||||
"mailing_list_id": env.int("ODOO_MAILING_LIST_ID", default=46),
|
||||
}
|
||||
|
||||
BROKER_USERNAME = env.str("BROKER_USERNAME", default="broker")
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue