website/hub/services/views/leads.py

216 lines
8 KiB
Python
Raw Normal View History

2025-01-30 09:49:27 +01:00
import logging
2025-03-04 17:01:03 +01:00
import time
2025-03-03 14:30:05 +01:00
from django.shortcuts import render, redirect
2025-01-30 09:49:27 +01:00
from django.contrib import messages
from django.core.mail import send_mail
from django.template.loader import render_to_string
from django.utils.html import strip_tags
from django.conf import settings
2025-03-03 14:30:05 +01:00
2025-01-30 11:23:25 +01:00
from hub.services.forms import LeadForm
from hub.services.odoo import OdooAPI
2025-01-30 09:49:27 +01:00
logger = logging.getLogger(__name__)
2025-03-03 14:30:05 +01:00
def contact(request):
return render(request, "services/contact_form.html")
2025-01-30 09:49:27 +01:00
2025-03-03 14:30:05 +01:00
def thank_you(request):
return render(request, "services/thank_you.html")
2025-02-27 15:44:55 +01:00
2025-03-03 14:30:05 +01:00
def contact_form(request):
2025-02-27 15:44:55 +01:00
if request.method == "POST":
2025-03-04 17:01:03 +01:00
# Spam protection checks
honeypot_value = request.POST.get("website", "")
timestamp_value = request.POST.get("form_timestamp", "0")
current_time = int(time.time())
# Check 1: Honeypot field should be empty
if honeypot_value:
# Bot detected - silently redirect
return redirect("services:homepage")
# Check 2: Form shouldn't be submitted too quickly (< 3 seconds)
try:
form_time = int(timestamp_value)
if current_time - form_time < 3:
# Too quick submission - likely a bot
return redirect("services:homepage")
except ValueError:
# Invalid timestamp - likely a bot
return redirect("services:homepage")
# Continue with normal form processing
2025-02-27 15:44:55 +01:00
form = LeadForm(request.POST)
if form.is_valid():
2025-03-03 14:30:05 +01:00
from hub.services.models import Lead, Service, ServiceOffering, Plan
2025-02-27 15:44:55 +01:00
2025-03-03 14:30:05 +01:00
# Create a lead with context information
2025-02-27 15:44:55 +01:00
lead = Lead(
name=form.cleaned_data["name"],
email=form.cleaned_data["email"],
2025-03-04 16:49:30 +01:00
message=form.cleaned_data["message"] or "",
2025-02-27 15:44:55 +01:00
company=form.cleaned_data["company"],
phone=form.cleaned_data["phone"],
)
2025-03-03 14:30:05 +01:00
# 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}")
2025-03-04 16:49:30 +01:00
# Handle selected choice if present
selected_choice = request.POST.get("selected_choice", "")
if selected_choice:
try:
choice_id, choice_name = selected_choice.split("|", 1)
# Add selected choice to message
service_info.append(f"Selected Plan: {choice_name}")
# Try to set the plan based on the choice_id
try:
lead.plan = Plan.objects.get(id=choice_id)
except Plan.DoesNotExist:
pass
except ValueError:
pass
2025-03-03 14:30:05 +01:00
if service_info:
context_info.append("Service Information: " + ", ".join(service_info))
# Format the final message with all context
formatted_context = "<br>".join(context_info)
original_message = lead.message
2025-03-03 14:30:05 +01:00
lead.message = (
lead.message + "<br><br>" + formatted_context
if lead.message
else formatted_context
)
2025-02-27 15:44:55 +01:00
try:
2025-03-03 14:30:05 +01:00
logger.info(
f"Contact form submission for {service_name or 'general inquiry'}"
)
2025-02-27 15:44:55 +01:00
odoo = OdooAPI()
2025-03-03 14:30:05 +01:00
odoo_lead_id = odoo.create_lead(lead, lead_title)
2025-02-27 15:44:55 +01:00
lead.odoo_lead_id = odoo_lead_id
lead.save()
2025-03-03 14:30:05 +01:00
logger.info(f"Successfully created lead with Odoo ID: {odoo_lead_id}")
2025-02-27 15:44:55 +01:00
# Send confirmation email
try:
# Prepare context for email template
email_context = {
"name": lead.name,
"email": lead.email,
"company": lead.company or "Not provided",
"phone": lead.phone or "Not provided",
"message": original_message,
"service": service_name or "General inquiry",
"provider": offering_name or "Not specified",
"plan": plan_name or "Not specified",
}
# Render email content from template
html_message = render_to_string(
"email/lead_confirmation.html", email_context
)
plain_message = render_to_string(
"email/lead_confirmation.txt", email_context
)
# Send the email
send_mail(
subject=f'Thank you for contacting Servala about {service_name or "our services"}',
message=plain_message,
html_message=html_message,
from_email=settings.DEFAULT_FROM_EMAIL,
recipient_list=[lead.email],
fail_silently=False,
)
logger.info(f"Confirmation email sent to {lead.email}")
except Exception as e:
# Log error but don't stop the process
logger.error(
f"Failed to send confirmation email: {str(e)}", exc_info=True
)
2025-03-03 14:30:05 +01:00
# Redirect to thank you page
return redirect("services:thank_you")
2025-02-27 15:44:55 +01:00
2025-03-03 14:30:05 +01:00
except Exception as e:
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", ""),
},
)
2025-02-27 15:44:55 +01:00
2025-03-03 14:30:05 +01:00
return redirect(
"services:homepage"
) # Redirect if someone tries to access the URL directly