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

@ -1,107 +1,28 @@
import logging
from django.conf import settings
from django.shortcuts import render, get_object_or_404, redirect
from django.shortcuts import render, redirect
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.odoo import OdooAPI
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):
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":
form = LeadForm(request.POST)
if form.is_valid():
# Create a minimal Lead object
from hub.services.models import Lead
from hub.services.models import Lead, Service, ServiceOffering, Plan
# Create a lead with context information
lead = Lead(
name=form.cleaned_data["name"],
email=form.cleaned_data["email"],
@ -110,37 +31,106 @@ def contact(request):
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:
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"Attempting to create contact lead from {lead.name}")
logger.info(
f"Contact form submission for {service_name or 'general inquiry'}"
)
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.save()
logger.info(
f"Successfully created contact lead with Odoo ID: {odoo_lead_id}"
)
messages.success(
request, "Thank you for your message. We'll get back to you soon!"
)
return redirect("services:contact_thank_you")
logger.info(f"Successfully created lead with Odoo ID: {odoo_lead_id}")
# Redirect to thank you page
return redirect("services:thank_you")
except Exception as e:
logger.error(f"Failed to create contact lead: {str(e)}", exc_info=True)
error_message = "Sorry, there was an error processing your request. Please try again later."
logger.error(f"Failed to create lead: {str(e)}", exc_info=True)
messages.error(
request,
"Sorry, there was an error processing your request. Please try again later.",
)
return redirect(next_url)
else:
# Return form with errors
return render(
request,
"services/contact_form.html",
{
"form": form,
"source": request.POST.get("source", ""),
"details": request.POST.get("details", ""),
},
)
if settings.DEBUG:
error_message += f" Error: {str(e)}"
messages.error(request, error_message)
else:
form = LeadForm()
context = {
"form": form,
}
return render(request, "services/contact_form.html", context)
def contact_thank_you(request):
return render(request, "services/contact_thank_you.html")
return redirect(
"services:homepage"
) # Redirect if someone tries to access the URL directly