Overhaul Forms

This commit is contained in:
Tobias Brunner 2025-03-03 14:30:05 +01:00
parent 25b91fb01b
commit 091e62b03c
16 changed files with 384 additions and 384 deletions

View file

@ -13,13 +13,3 @@ class LeadForm(forms.ModelForm):
"phone": forms.TextInput(attrs={"class": "form-control"}), "phone": forms.TextInput(attrs={"class": "form-control"}),
"message": forms.Textarea(attrs={"class": "form-control", "rows": 4}), "message": forms.Textarea(attrs={"class": "form-control", "rows": 4}),
} }
class PlanForm(forms.ModelForm):
class Meta:
model = Plan
fields = ("name", "description")
widgets = {
"description": forms.Textarea(attrs={"rows": 3}),
"features": forms.Textarea(attrs={"rows": 4}),
}

View file

@ -53,56 +53,21 @@ class OdooAPI:
logger.debug("Full error details:", exc_info=True) logger.debug("Full error details:", exc_info=True)
raise raise
def create_lead(self, lead, source="form"): def create_lead(self, lead, lead_title=""):
"""Create a lead in Odoo""" """Create a lead in Odoo"""
try: try:
logger.info(f"Attempting to create lead for {lead.name}") 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 # Prepare lead data
lead_data = { lead_data = {
"name": lead_name, "name": "Servala" + lead_title,
"contact_name": lead.name, "contact_name": lead.name,
"partner_name": ( "partner_name": (
lead.company if hasattr(lead, "company") and lead.company else "" lead.company if hasattr(lead, "company") and lead.company else ""
), ),
"email_from": lead.email, "email_from": lead.email,
"phone": lead.phone if hasattr(lead, "phone") and lead.phone else "", "phone": lead.phone if hasattr(lead, "phone") and lead.phone else "",
"description": ( "description": lead.message,
f"{lead.message}<br/><br/>" + "<br/>".join(service_details)
if lead.message
else "<br/>".join(service_details)
),
"type": "lead", "type": "lead",
"campaign_id": settings.ODOO_CONFIG["campaign_id"], "campaign_id": settings.ODOO_CONFIG["campaign_id"],
"source_id": settings.ODOO_CONFIG["source_id"], "source_id": settings.ODOO_CONFIG["source_id"],
@ -122,60 +87,6 @@ class OdooAPI:
odoo_lead_id = Lead.create(lead_data) odoo_lead_id = Lead.create(lead_data)
logger.info(f"Successfully created lead in Odoo with ID: {odoo_lead_id}") 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 return odoo_lead_id
except odoorpc.error.RPCError as e: except odoorpc.error.RPCError as e:

View file

@ -1,83 +1,24 @@
{% extends 'services/base.html' %} {% extends 'services/base.html' %}
{% load form_tags %} {% load contact_tags %}
{% block title %}Contact Us{% endblock %} {% block title %}Contact Us{% endblock %}
{% block content %} {% block content %}
<section class="section bg-primary-subtle">
<div class="container mx-auto px-20 px-lg-0 pt-40 pb-60">
<header class="section-primary__header text-center">
<h2 class="section-h1 fs-40 fs-lg-64 mb-24">Contact Us</h2>
<div class="text-gray-300 w-lg-37 mx-auto">
<p class="mb-0">We'd love to hear from you!</p>
</div>
</header>
</div>
</section>
<div class="container py-5"> <div class="container py-5">
<div class="row justify-content-center"> <div class="row justify-content-center">
<div class="col-md-8"> <div class="col-md-8">
<div class="card shadow-sm"> {% embedded_contact_form source="Contact Us" %}
<div class="card-body p-4">
<h2 class="mb-4">Contact Us</h2>
{% if messages %}
{% for message in messages %}
<div class="alert alert-{{ message.tags }}">
{{ message }}
</div>
{% endfor %}
{% endif %}
<form method="post">
{% csrf_token %}
<div class="mb-3">
<label for="{{ form.name.id_for_label }}" class="form-label">Your Name</label>
{{ form.name }}
{% if form.name.errors %}
<div class="invalid-feedback d-block">
{{ form.name.errors }}
</div>
{% endif %}
</div>
<div class="mb-3">
<label for="{{ form.email.id_for_label }}" class="form-label">Your Email Address</label>
{{ form.email }}
{% if form.email.errors %}
<div class="invalid-feedback d-block">
{{ form.email.errors }}
</div>
{% endif %}
</div>
<div class="mb-3">
<label for="{{ form.phone.id_for_label }}" class="form-label">Your Phone Number (Optional)</label>
{{ form.phone }}
{% if form.phone.errors %}
<div class="invalid-feedback d-block">
{{ form.phone.errors }}
</div>
{% endif %}
</div>
<div class="mb-3">
<label for="{{ form.company.id_for_label }}" class="form-label">Your Company (Optional)</label>
{{ form.company }}
{% if form.company.errors %}
<div class="invalid-feedback d-block">
{{ form.company.errors }}
</div>
{% endif %}
</div>
<div class="mb-3">
<label for="{{ form.message.id_for_label }}" class="form-label">Your Message (Optional)</label>
{{ form.message }}
{% if form.message.errors %}
<div class="invalid-feedback d-block">
{{ form.message.errors }}
</div>
{% endif %}
</div>
<div class="mt-4">
<button type="submit" class="btn btn-primary btn-lg">Send Message</button>
</div>
</form>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>

View file

@ -1,20 +0,0 @@
{% extends 'services/base.html' %}
{% block title %}Thanks{% endblock %}
{% block content %}
<div class="container py-5">
<div class="row justify-content-center">
<div class="col-md-8 text-center">
<div class="card shadow-sm">
<div class="card-body p-5">
<h2 class="mb-4">Thank You!</h2>
<p class="lead">Your message has been sent successfully.</p>
<p>We'll get back to you as soon as possible.</p>
<a href="{% url 'services:homepage' %}" class="btn btn-primary mt-3">Back to Home</a>
</div>
</div>
</div>
</div>
</div>
{% endblock %}

View file

@ -0,0 +1,68 @@
{% load form_tags %}
<div class="card mb-4">
<div class="card-body p-4">
<form method="post" action="{% url 'services:contact_form' %}">
{% csrf_token %}
<input type="hidden" name="source" value="{{ source|default:'Contact Form' }}">
{% if details %}
<input type="hidden" name="details" value="{{ details }}">
{% endif %}
{% if service %}
<input type="hidden" name="service_id" value="{{ service.id }}">
<input type="hidden" name="service_name" value="{{ service.name }}">
{% endif %}
{% if offering %}
<input type="hidden" name="offering_id" value="{{ offering.id }}">
<input type="hidden" name="offering_name" value="{{ offering.cloud_provider.name }}">
{% endif %}
{% if plan %}
<input type="hidden" name="plan_id" value="{{ plan.id }}">
<input type="hidden" name="plan_name" value="{{ plan.name }}">
{% endif %}
<input type="hidden" name="next" value="{{ request.path }}">
<div class="mb-3">
<label for="id_name" class="form-label">Your Name</label>
{{ form.name|addclass:"form-control" }}
{% if form.name.errors %}
<div class="invalid-feedback d-block">{{ form.name.errors }}</div>
{% endif %}
</div>
<div class="mb-3">
<label for="id_email" class="form-label">Your Email Address</label>
{{ form.email|addclass:"form-control" }}
{% if form.email.errors %}
<div class="invalid-feedback d-block">{{ form.email.errors }}</div>
{% endif %}
</div>
<div class="mb-3">
<label for="id_phone" class="form-label">Your Phone Number (Optional)</label>
{{ form.phone|addclass:"form-control" }}
{% if form.phone.errors %}
<div class="invalid-feedback d-block">{{ form.phone.errors }}</div>
{% endif %}
</div>
<div class="mb-3">
<label for="id_company" class="form-label">Your Company (Optional)</label>
{{ form.company|addclass:"form-control" }}
{% if form.company.errors %}
<div class="invalid-feedback d-block">{{ form.company.errors }}</div>
{% endif %}
</div>
<div class="mb-3">
<label for="id_message" class="form-label">Your Message (Optional)</label>
{{ form.message|addclass:"form-control" }}
{% if form.message.errors %}
<div class="invalid-feedback d-block">{{ form.message.errors }}</div>
{% endif %}
</div>
<button type="submit" class="btn btn-primary">Send Message</button>
</form>
</div>
</div>

View file

@ -150,10 +150,9 @@
{% endif %} {% endif %}
</div> </div>
<div> <div>
<a class="btn btn-primary btn-lg w-100" <h4 class="mb-3">Order This Plan</h4>
href="{% url 'services:create_lead' offering.service.slug %}?offering={{ offering.id }}&plan={{ plan.id }}"> {% load contact_tags %}
Order Now {% embedded_contact_form source="Plan Order" service=offering.service offering_id=offering.id plan_id=plan.id %}
</a>
</div> </div>
</div> </div>
</div> </div>
@ -161,8 +160,9 @@
<div class="col-12"> <div class="col-12">
<div class="alert alert-info"> <div class="alert alert-info">
<p>No plans available yet.</p> <p>No plans available yet.</p>
<a href="{% url 'services:create_lead' offering.service.slug %}?offering={{ offering.id }}" <h4 class="mb-3">I'm interested in this offering</h4>
class="btn btn-primary btn-lg mr-md-17 mb-17 mb-md-0px mb-lg-17 mb-xl-0 w-100 w-md-auto">I'm interested in a plan</a> {% load contact_tags %}
{% embedded_contact_form source="Offering Interest" service=offering.service offering_id=offering.id %}
</div> </div>
</div> </div>
{% endfor %} {% endfor %}

View file

@ -1,4 +1,5 @@
{% extends 'services/base.html' %} {% extends 'services/base.html' %}
{% load contact_tags %}
{% block title %}Consulting Partner {{ partner.name }}{% endblock %} {% block title %}Consulting Partner {{ partner.name }}{% endblock %}
@ -177,6 +178,18 @@
</div> </div>
</div> </div>
{% endif %} {% endif %}
<!-- Contact -->
<div class="pt-40">
<h3 class="fs-24 fw-semibold lh-1 mb-12">Contact</h3>
<div class="row">
<div class="col-12 mb-30">
{% embedded_contact_form source="Partner Page" details=partner.name %}
</div>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>

View file

@ -1,4 +1,6 @@
{% extends 'services/base.html' %} {% extends 'services/base.html' %}
{% load static %}
{% load contact_tags %}
{% block title %}Consulting Partners{% endblock %} {% block title %}Consulting Partners{% endblock %}
@ -162,4 +164,27 @@
</div> </div>
</div> </div>
</section> </section>
<section class="section bg-primary-subtle">
<div class="section-wrapper container mx-auto px-20 px-lg-0 pt-80 pb-120">
<div class="row">
<div class="col-12 col-lg-4 mb-30 mb-lg-0">
<div class="section-logo mx-auto">
<img class="img-fluid" src="{% static "img/section-logo.png" %}" alt="Sir Vala mascot">
</div>
</div>
<div class="col-12 col-lg-8">
<header class="section-primary__header">
<h2 class="section-h1 fs-40 fs-lg-60">Missing a Consulting Partner?</h2>
<div class="section-primary__desc">
<p>You miss a consulting partner on this list? Let us know which one and we'll get in touch with you!</p>
</div>
<div>
{% embedded_contact_form source="Missing Consulting Partner" %}
</div>
</header>
</div>
</div>
</div>
</section>
{% endblock %} {% endblock %}

View file

@ -1,4 +1,5 @@
{% extends 'services/base.html' %} {% extends 'services/base.html' %}
{% load contact_tags %}
{% block title %}Service Provider {{ provider.name }}{% endblock %} {% block title %}Service Provider {{ provider.name }}{% endblock %}
@ -177,6 +178,18 @@
</div> </div>
</div> </div>
{% endif %} {% endif %}
<!-- Contact -->
<div class="pt-40">
<h3 class="fs-24 fw-semibold lh-1 mb-12">Contact</h3>
<p>Do you miss a service on this cloud provider? Do you have any questions about this cloud provider? We're here to help!</p>
<div class="row">
<div class="col-12 mb-30">
{% embedded_contact_form source="Provider Page" details=provider.name %}
</div>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>

View file

@ -1,4 +1,6 @@
{% extends 'services/base.html' %} {% extends 'services/base.html' %}
{% load static %}
{% load contact_tags %}
{% block title %}Service Providers{% endblock %} {% block title %}Service Providers{% endblock %}
@ -128,4 +130,27 @@
</div> </div>
</div> </div>
</section> </section>
<section class="section bg-primary-subtle">
<div class="section-wrapper container mx-auto px-20 px-lg-0 pt-80 pb-120">
<div class="row">
<div class="col-12 col-lg-4 mb-30 mb-lg-0">
<div class="section-logo mx-auto">
<img class="img-fluid" src="{% static "img/section-logo.png" %}" alt="Sir Vala mascot">
</div>
</div>
<div class="col-12 col-lg-8">
<header class="section-primary__header">
<h2 class="section-h1 fs-40 fs-lg-60">Missing a Cloud Provider?</h2>
<div class="section-primary__desc">
<p>You miss a cloud provider on this list? Let us know which one and we'll get in touch with you!</p>
</div>
<div>
{% embedded_contact_form source="Missing Cloud Provider" %}
</div>
</header>
</div>
</div>
</div>
</section>
{% endblock %} {% endblock %}

View file

@ -129,19 +129,24 @@
<!-- CTA --> <!-- CTA -->
<div class="pt-24"> <div class="pt-24">
<div class="page-action"> <div class="page-action">
{% if service.is_coming_soon %} {% if service.offerings.exists %}
<a class="btn btn-primary btn-lg mr-md-17 mb-17 mb-md-0px mb-lg-17 mb-xl-0 w-100 w-md-auto" href="{% url 'services:create_lead' service.slug %}"
role="button">I'm interested in this service</a>
{% elif service.offerings.exists %}
<h3 class="fs-24 fw-semibold lh-1 mb-12">Get it on</h3> <h3 class="fs-24 fw-semibold lh-1 mb-12">Get it on</h3>
<p>Choose one of our trusted service providers</p> <p>Choose one of our trusted service providers</p>
{% for offering in service.offerings.all %} {% for offering in service.offerings.all %}
<p><a class="btn btn-primary btn-lg mr-md-17 mb-17 mb-md-0px mb-lg-17 mb-xl-0 w-100 w-md-auto" href="{% url 'services:offering_detail' offering.cloud_provider.slug offering.service.slug %}" <p><a class="btn btn-primary btn-lg mr-md-17 mb-17 mb-md-0px mb-lg-17 mb-xl-0 w-100 w-md-auto"
href="{% url 'services:offering_detail' offering.cloud_provider.slug offering.service.slug %}"
role="button">{{ offering.cloud_provider.name }}</a></p> role="button">{{ offering.cloud_provider.name }}</a></p>
{% endfor %} {% endfor %}
{% else %} {% else %}
<a class="btn btn-primary btn-lg mr-md-17 mb-17 mb-md-0px mb-lg-17 mb-xl-0 w-100 w-md-auto" href="{% url 'services:create_lead' service.slug %}" <div class="pt-40">
role="button">I'm interested in this service</a> <h3 class="fs-24 fw-semibold lh-1 mb-12">Contact Us About This Service</h3>
<div class="row">
<div class="col-12 mb-30">
{% load contact_tags %}
{% embedded_contact_form source="Service Detail" service=service %}
</div>
</div>
</div>
{% endif %} {% endif %}
</div> </div>
</div> </div>

View file

@ -1,5 +1,6 @@
{% extends 'services/base.html' %} {% extends 'services/base.html' %}
{% load static %} {% load static %}
{% load contact_tags %}
{% block title %}Services{% endblock %} {% block title %}Services{% endblock %}
@ -185,29 +186,33 @@
</div> </div>
</div> </div>
{% endfor %} {% endfor %}
<div class="col-12 col-md-6 col-lg-4 mb-30">
<div class="card h-100 d-flex flex-column">
<div class="d-flex justify-content-between">
<div class="card__image flex-shrink-0">
<a href="{% url 'services:contact' %}">
<img src="{% static "img/sir-vala.png" %}" alt="missing service logo" class="img-fluid">
</a>
</div>
</div>
<div class="card__content d-flex flex-column flex-grow-1">
<div class="card__header">
<h3 class="card__title"><a href="{% url 'services:contact' %}" class="text-decoration-none">Missing Service?</a></h3>
</div>
<div class="card__desc flex-grow-1">
<p class="mb-0">Your favorite service is missing? Let us know which one!</p>
</div>
</div>
</div>
</div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</section> </section>
<section class="section bg-primary-subtle">
<div class="section-wrapper container mx-auto px-20 px-lg-0 pt-80 pb-120">
<div class="row">
<div class="col-12 col-lg-4 mb-30 mb-lg-0">
<div class="section-logo mx-auto">
<img class="img-fluid" src="{% static "img/section-logo.png" %}" alt="Sir Vala mascot">
</div>
</div>
<div class="col-12 col-lg-8">
<header class="section-primary__header">
<h2 class="section-h1 fs-40 fs-lg-60">Missing a Service?</h2>
<div class="section-primary__desc">
<p>You miss a service on this list? Let us know which one and we'll get in touch with you!</p>
</div>
<div>
{% embedded_contact_form source="Missing Service" %}
</div>
</header>
</div>
</div>
</div>
</section>
{% endblock %} {% endblock %}

View file

@ -3,42 +3,18 @@
{% block title %}Thanks{% endblock %} {% block title %}Thanks{% endblock %}
{% block content %} {% block content %}
<section class="section"> <div class="container py-5">
<div class="container mx-auto px-20 px-lg-0 pt-80 pb-60"> <div class="row justify-content-center">
<div class="d-lg-flex"> <div class="col-md-8 text-center">
<div class="flex-1 pr-lg-40 mb-40 mb-lg-0"> <div class="card shadow-sm">
<div class="bg-gray-50 rounded-20 py-40 px-20 px-lg-40 d-flex justify-content-center align-items-center h-100"> <div class="card-body p-5">
<div class="text-center"> <h2 class="mb-4">Thank You!</h2>
<div class="mb-20"> <p class="lead">Your message has been sent successfully.</p>
<svg class="mx-auto d-block" width="101" height="102" viewBox="0 0 101 102" fill="none" <p>We'll get back to you as soon as possible.</p>
xmlns="http://www.w3.org/2000/svg"> <a href="{% url 'services:homepage' %}" class="btn btn-primary mt-3">Take me home</a>
<g clip-path="url(#clip0_8_818)">
<path d="M50.5 101.5C78.3904 101.5 101 78.8904 101 51C101 23.1096 78.3904 0.5 50.5 0.5C22.6096 0.5 0 23.1096 0 51C0 78.8904 22.6096 101.5 50.5 101.5Z"
fill="#9A63EC" />
<path d="M37.5957 73.7808L63.5626 99.7476C85.0689 94.0126 100.999 74.418 100.999 51C100.999 50.5221 100.999 50.0442 100.999 49.5662L80.6083 30.7681L37.5957 73.7808Z"
fill="#9A63EC" />
<path d="M51.7752 62.3107C54.0054 64.541 54.0054 68.3644 51.7752 70.5946L47.1553 75.2145C44.925 77.4448 41.1017 77.4448 38.8714 75.2145L18.6395 54.8233C16.4092 52.5931 16.4092 48.7697 18.6395 46.5394L23.2594 41.9196C25.4897 39.6893 29.313 39.6893 31.5433 41.9196L51.7752 62.3107Z"
fill="white" />
<path d="M69.4583 27.1041C71.6886 24.8738 75.5119 24.8738 77.7422 27.1041L82.3621 31.724C84.5924 33.9543 84.5924 37.7776 82.3621 40.0079L47.3148 74.8959C45.0845 77.1262 41.2612 77.1262 39.0309 74.8959L34.411 70.276C32.1807 68.0457 32.1807 64.2224 34.411 61.9921L69.4583 27.1041Z"
fill="white" />
</g>
<defs>
<clipPath id="clip0_8_818">
<rect width="101" height="101" fill="white" transform="translate(0 0.5)" />
</clipPath>
</defs>
</svg>
</div>
<header class="mb-20">
<h2 class="fs-32 fs-lg-40 fw-semibold mb-20">Inquiry received successfully!</h2>
<div class="fs-base text-gray-600 w-lg-75 mx-auto">
<p class="mb-0">Thank you for your interest in {{ service.name }}. We have received your inquiry and our team will contact you shortly. A confirmation email will be sent to your provided email address.</p>
</div>
</header>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</div> </div>
</section>
{% endblock %} {% endblock %}

View file

@ -0,0 +1,59 @@
# hub/services/templatetags/contact_tags.py
from django import template
from hub.services.forms import LeadForm
from hub.services.models import Service, ServiceOffering, Plan
register = template.Library()
@register.inclusion_tag("services/embedded_contact_form.html", takes_context=True)
def embedded_contact_form(
context, source=None, details=None, service=None, offering_id=None, plan_id=None
):
"""
Renders an embedded contact form with optional service context information.
Usage:
{% load contact_tags %}
{% embedded_contact_form source="Partner Page" details="ACME Corp" %}
{% embedded_contact_form service=service offering_id=offering.id plan_id=plan.id %}
"""
request = context["request"]
form = LeadForm()
service_obj = None
offering_obj = None
plan_obj = None
# Resolve service/offering/plan objects if IDs provided
if service and isinstance(service, str):
try:
service_obj = Service.objects.get(slug=service)
except Service.DoesNotExist:
pass
elif service:
service_obj = service
if offering_id and service_obj:
try:
offering_obj = ServiceOffering.objects.get(
id=offering_id, service=service_obj
)
except ServiceOffering.DoesNotExist:
pass
if plan_id and offering_obj:
try:
plan_obj = Plan.objects.get(id=plan_id, offering=offering_obj)
except Plan.DoesNotExist:
pass
return {
"form": form,
"source": source,
"details": details,
"service": service_obj,
"offering": offering_obj,
"plan": plan_obj,
"request": request,
}

View file

@ -17,8 +17,7 @@ urlpatterns = [
), ),
path("provider/<slug:slug>/", views.provider_detail, name="provider_detail"), path("provider/<slug:slug>/", views.provider_detail, name="provider_detail"),
path("partner/<slug:slug>/", views.partner_detail, name="partner_detail"), path("partner/<slug:slug>/", views.partner_detail, name="partner_detail"),
path("service/<slug:slug>/interest/", views.create_lead, name="create_lead"),
path("service/<slug:slug>/thank-you/", views.thank_you, name="thank_you"),
path("contact/", views.leads.contact, name="contact"), path("contact/", views.leads.contact, name="contact"),
path("contact/thank-you/", views.leads.contact_thank_you, name="contact_thank_you"), path("contact/thank-you/", views.thank_you, name="thank_you"),
path("contact-form/", views.contact_form, name="contact_form"),
] ]

View file

@ -1,107 +1,28 @@
import logging import logging
from django.conf import settings from django.shortcuts import render, redirect
from django.shortcuts import render, get_object_or_404, redirect
from django.contrib import messages from django.contrib import messages
from django.urls import reverse
from hub.services.models import (
Service,
ServiceOffering,
Plan,
)
from hub.services.forms import LeadForm from hub.services.forms import LeadForm
from hub.services.odoo import OdooAPI from hub.services.odoo import OdooAPI
logger = logging.getLogger(__name__) logger = logging.getLogger(__name__)
def create_lead(request, slug):
service = get_object_or_404(
Service.objects.prefetch_related("categories"), slug=slug
)
selected_offering = None
selected_plan = None
# Get the offering if specified
if request.GET.get("offering"):
selected_offering = get_object_or_404(
ServiceOffering.objects.select_related("cloud_provider").prefetch_related(
"plans"
),
id=request.GET.get("offering"),
service=service,
)
if selected_offering.plans.exists():
if not request.GET.get("plan"):
# If there's only one plan, automatically select it
if selected_offering.plans.count() == 1:
return redirect(
f"{reverse('services:create_lead', kwargs={'slug': service.slug})}?offering={selected_offering.id}&plan={selected_offering.plans.first().id}"
)
# If there are multiple plans, redirect to offering detail
return redirect(
"services:offering_detail",
provider_slug=selected_offering.cloud_provider.slug,
service_slug=selected_offering.service.slug,
)
# Get the selected plan
selected_plan = get_object_or_404(
Plan,
id=request.GET.get("plan"),
offering=selected_offering,
)
if request.method == "POST":
form = LeadForm(request.POST)
if form.is_valid():
lead = form.save(commit=False)
lead.service = service
lead.offering = selected_offering
lead.plan = selected_plan
try:
logger.info(f"Attempting to create lead for service: {service.name}")
odoo = OdooAPI()
odoo_lead_id = odoo.create_lead(lead)
lead.odoo_lead_id = odoo_lead_id
lead.save()
logger.info(f"Successfully created lead with Odoo ID: {odoo_lead_id}")
return redirect("services:thank_you", slug=service.slug)
except Exception as e:
logger.error(f"Failed to create lead: {str(e)}", exc_info=True)
error_message = "Sorry, there was an error processing your request. Please try again later."
if settings.DEBUG:
error_message += f" Error: {str(e)}"
messages.error(request, error_message)
else:
form = LeadForm()
context = {
"form": form,
"service": service,
"selected_offering": selected_offering,
"selected_plan": selected_plan,
}
return render(request, "services/lead_form.html", context)
def thank_you(request, slug):
service = get_object_or_404(Service, slug=slug)
return render(request, "services/thank_you.html", {"service": service})
def contact(request): def contact(request):
return render(request, "services/contact_form.html")
def thank_you(request):
return render(request, "services/thank_you.html")
def contact_form(request):
if request.method == "POST": if request.method == "POST":
form = LeadForm(request.POST) form = LeadForm(request.POST)
if form.is_valid(): if form.is_valid():
# Create a minimal Lead object from hub.services.models import Lead, Service, ServiceOffering, Plan
from hub.services.models import Lead
# Create a lead with context information
lead = Lead( lead = Lead(
name=form.cleaned_data["name"], name=form.cleaned_data["name"],
email=form.cleaned_data["email"], email=form.cleaned_data["email"],
@ -110,37 +31,106 @@ def contact(request):
phone=form.cleaned_data["phone"], phone=form.cleaned_data["phone"],
) )
# Store the source/context information
source = request.POST.get("source", "Contact Form")
details = request.POST.get("details", "")
next_url = request.POST.get("next", "/")
# Handle service/offering/plan data
service_id = request.POST.get("service_id")
service_name = request.POST.get("service_name", "")
offering_id = request.POST.get("offering_id")
offering_name = request.POST.get("offering_name", "")
plan_id = request.POST.get("plan_id")
plan_name = request.POST.get("plan_name", "")
# Link to related objects if they exist
if service_id:
try: try:
logger.info(f"Attempting to create contact lead from {lead.name}") lead.service = Service.objects.get(id=service_id)
except Service.DoesNotExist:
pass
if offering_id:
try:
lead.offering = ServiceOffering.objects.get(id=offering_id)
except ServiceOffering.DoesNotExist:
pass
if plan_id:
try:
lead.plan = Plan.objects.get(id=plan_id)
except Plan.DoesNotExist:
pass
# Add context to the message and lead_title
lead_title = ""
context_info = []
if source:
context_info.append(f"Source Page: {source}")
lead_title += f" - {source}"
if details:
context_info.append(f"Details: {details}")
lead_title += f" - {details}"
# Add service information to lead title and message
service_info = []
if service_name:
service_info.append(f"Service: {service_name}")
lead_title += f" - {service_name}"
if offering_name:
service_info.append(f"Provider: {offering_name}")
if plan_name:
service_info.append(f"Plan: {plan_name}")
if service_info:
context_info.append("Service Information: " + ", ".join(service_info))
# Format the final message with all context
formatted_context = "<br>".join(context_info)
lead.message = (
lead.message + "<br><br>" + formatted_context
if lead.message
else formatted_context
)
try:
logger.info(
f"Contact form submission for {service_name or 'general inquiry'}"
)
odoo = OdooAPI() odoo = OdooAPI()
odoo_lead_id = odoo.create_lead(lead, source="contact_form") odoo_lead_id = odoo.create_lead(lead, lead_title)
lead.odoo_lead_id = odoo_lead_id lead.odoo_lead_id = odoo_lead_id
lead.save() lead.save()
logger.info( logger.info(f"Successfully created lead with Odoo ID: {odoo_lead_id}")
f"Successfully created contact lead with Odoo ID: {odoo_lead_id}"
) # Redirect to thank you page
messages.success( return redirect("services:thank_you")
request, "Thank you for your message. We'll get back to you soon!"
)
return redirect("services:contact_thank_you")
except Exception as e: except Exception as e:
logger.error(f"Failed to create contact lead: {str(e)}", exc_info=True) logger.error(f"Failed to create lead: {str(e)}", exc_info=True)
error_message = "Sorry, there was an error processing your request. Please try again later." messages.error(
request,
if settings.DEBUG: "Sorry, there was an error processing your request. Please try again later.",
error_message += f" Error: {str(e)}" )
return redirect(next_url)
messages.error(request, error_message)
else: else:
form = LeadForm() # Return form with errors
return render(
context = { request,
"services/contact_form.html",
{
"form": form, "form": form,
} "source": request.POST.get("source", ""),
return render(request, "services/contact_form.html", context) "details": request.POST.get("details", ""),
},
)
return redirect(
def contact_thank_you(request): "services:homepage"
return render(request, "services/contact_thank_you.html") ) # Redirect if someone tries to access the URL directly