generated from westfarn/web_django_template
Initial commit
This commit is contained in:
@@ -0,0 +1,16 @@
|
||||
from django.contrib import admin
|
||||
|
||||
from leads.models import Lead, LeadNote
|
||||
|
||||
|
||||
class LeadNoteInline(admin.TabularInline):
|
||||
model = LeadNote
|
||||
extra = 0
|
||||
|
||||
|
||||
@admin.register(Lead)
|
||||
class LeadAdmin(admin.ModelAdmin):
|
||||
list_display = ("contact", "status", "created_at")
|
||||
list_filter = ("status",)
|
||||
search_fields = ("contact__email", "contact__first_name", "contact__last_name")
|
||||
inlines = [LeadNoteInline]
|
||||
@@ -0,0 +1,17 @@
|
||||
from django.apps import AppConfig
|
||||
|
||||
|
||||
class LeadsConfig(AppConfig):
|
||||
default_auto_field = "django.db.models.BigAutoField"
|
||||
name = "leads"
|
||||
|
||||
def ready(self):
|
||||
from core.registry import register_portal_nav
|
||||
|
||||
register_portal_nav(
|
||||
section="leads",
|
||||
label="Leads",
|
||||
url_name="leads:list",
|
||||
group="Overview",
|
||||
order=20,
|
||||
)
|
||||
@@ -0,0 +1,48 @@
|
||||
# Generated by Django 6.1 on 2026-08-26 11:38
|
||||
|
||||
import django.db.models.deletion
|
||||
import uuid
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
('contacts', '0001_initial'),
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='Lead',
|
||||
fields=[
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('id', models.UUIDField(default=uuid.uuid4, editable=False, primary_key=True, serialize=False)),
|
||||
('message', models.TextField(blank=True)),
|
||||
('status', models.CharField(choices=[('new', 'New'), ('contacted', 'Contacted'), ('won', 'Won'), ('lost', 'Lost')], default='new', max_length=16)),
|
||||
('contact', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='leads', to='contacts.contact')),
|
||||
('owner', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, related_name='leads', to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
options={
|
||||
'ordering': ['-created_at'],
|
||||
},
|
||||
),
|
||||
migrations.CreateModel(
|
||||
name='LeadNote',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('created_at', models.DateTimeField(auto_now_add=True)),
|
||||
('updated_at', models.DateTimeField(auto_now=True)),
|
||||
('body', models.TextField()),
|
||||
('author', models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.SET_NULL, to=settings.AUTH_USER_MODEL)),
|
||||
('lead', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, related_name='notes', to='leads.lead')),
|
||||
],
|
||||
options={
|
||||
'ordering': ['-created_at'],
|
||||
},
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,46 @@
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
|
||||
from contacts.models import Contact
|
||||
from core.models import TimeStampedModel, UUIDPrimaryKeyModel
|
||||
|
||||
|
||||
class Lead(UUIDPrimaryKeyModel, TimeStampedModel):
|
||||
class Status(models.TextChoices):
|
||||
NEW = "new", "New"
|
||||
CONTACTED = "contacted", "Contacted"
|
||||
WON = "won", "Won"
|
||||
LOST = "lost", "Lost"
|
||||
|
||||
contact = models.ForeignKey(Contact, on_delete=models.CASCADE, related_name="leads")
|
||||
message = models.TextField(blank=True)
|
||||
status = models.CharField(
|
||||
max_length=16, choices=Status.choices, default=Status.NEW
|
||||
)
|
||||
owner = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL,
|
||||
null=True,
|
||||
blank=True,
|
||||
on_delete=models.SET_NULL,
|
||||
related_name="leads",
|
||||
)
|
||||
|
||||
class Meta:
|
||||
ordering = ["-created_at"]
|
||||
|
||||
def __str__(self) -> str:
|
||||
return f"Lead {self.contact} ({self.status})"
|
||||
|
||||
|
||||
class LeadNote(TimeStampedModel):
|
||||
lead = models.ForeignKey(Lead, on_delete=models.CASCADE, related_name="notes")
|
||||
author = models.ForeignKey(
|
||||
settings.AUTH_USER_MODEL,
|
||||
null=True,
|
||||
blank=True,
|
||||
on_delete=models.SET_NULL,
|
||||
)
|
||||
body = models.TextField()
|
||||
|
||||
class Meta:
|
||||
ordering = ["-created_at"]
|
||||
@@ -0,0 +1,88 @@
|
||||
{% extends "portal_base.html" %}
|
||||
{% block title %}{{ lead.contact }} · Lead{% endblock %}
|
||||
{% block topbar_title %}Lead · {{ lead.contact }}{% endblock %}
|
||||
{% block portal_content %}
|
||||
<div class="split">
|
||||
<div>
|
||||
<div class="panel">
|
||||
<div class="panel-h"><h2>Contact</h2></div>
|
||||
<div class="panel-b form-grid">
|
||||
<div class="form-grid cols-2">
|
||||
<div class="field">
|
||||
<label>Email</label>
|
||||
<input type="email" value="{{ lead.contact.email }}" readonly>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Phone</label>
|
||||
<input type="text" value="{{ lead.contact.phone }}" readonly>
|
||||
</div>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Message</label>
|
||||
<textarea readonly style="min-height:120px">{{ lead.message }}</textarea>
|
||||
</div>
|
||||
<div class="field">
|
||||
<label>Status</label>
|
||||
<input type="text" value="{{ lead.get_status_display }}" readonly>
|
||||
</div>
|
||||
<p class="hint-block">Status edits and note posting come next in functionality work.</p>
|
||||
<a class="btn btn-ghost btn-sm" href="{% url 'leads:list' %}">← Back to inbox</a>
|
||||
</div>
|
||||
</div>
|
||||
<div class="panel">
|
||||
<div class="panel-h"><h2>Notes</h2></div>
|
||||
<div class="panel-b">
|
||||
{% for note in lead.notes.all %}
|
||||
<div style="font-size:13px;color:#6b7280;margin-bottom:12px">
|
||||
<strong style="color:#1a1f2c">{% if note.author %}{{ note.author }}{% else %}System{% endif %}</strong>
|
||||
· {{ note.created_at|date:"M j, g:i A" }} — {{ note.body }}
|
||||
</div>
|
||||
{% empty %}
|
||||
<p class="empty-state" style="padding:0;margin:0 0 12px">No notes yet.</p>
|
||||
{% endfor %}
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
<div>
|
||||
<div class="panel">
|
||||
<div class="panel-h"><h2>Attribution</h2></div>
|
||||
<div class="panel-b" style="font-size:14px">
|
||||
{% if lead.attribution %}
|
||||
<p>
|
||||
<strong>utm_source</strong> {{ lead.attribution.utm_source|default:"—" }}<br>
|
||||
<strong>utm_medium</strong> {{ lead.attribution.utm_medium|default:"—" }}<br>
|
||||
<strong>utm_campaign</strong> {{ lead.attribution.utm_campaign|default:"—" }}<br>
|
||||
{% if lead.attribution.visit %}
|
||||
<strong>Landing</strong> {{ lead.attribution.visit.path|default:"—" }}<br>
|
||||
<strong>First touch</strong> {{ lead.attribution.visit.created_at|date:"M j, g:i A" }}
|
||||
{% endif %}
|
||||
<br><strong>Converted</strong> {{ lead.created_at|date:"M j, g:i A" }}
|
||||
</p>
|
||||
{% else %}
|
||||
<p class="muted">No UTM attribution on this lead.</p>
|
||||
{% endif %}
|
||||
</div>
|
||||
</div>
|
||||
<div class="panel">
|
||||
<div class="panel-h"><h2>Linked contact · consent</h2></div>
|
||||
<div class="panel-b">
|
||||
<div class="consent-pills">
|
||||
{% for channel, opted in consent_map.items %}
|
||||
<span class="badge {% if opted %}badge-optin{% else %}badge-optout{% endif %}">
|
||||
{{ channel|title }} {% if opted %}on{% else %}off{% endif %}
|
||||
</span>
|
||||
{% empty %}
|
||||
<span class="muted">No consent records.</span>
|
||||
{% endfor %}
|
||||
</div>
|
||||
<p style="font-size:13px;color:#6b7280;margin:12px 0 0">
|
||||
Form submit defaulted email opt-in with notice. Postcard requires address + separate consent.
|
||||
</p>
|
||||
<p style="margin-top:12px">
|
||||
<a href="{% url 'contacts:detail' lead.contact.pk %}">Open contact record →</a>
|
||||
</p>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,57 @@
|
||||
{% extends "portal_base.html" %}
|
||||
{% block title %}Leads · Portal{% endblock %}
|
||||
{% block topbar_title %}Lead inbox{% endblock %}
|
||||
{% block portal_content %}
|
||||
<div class="toolbar">
|
||||
<form class="toolbar-filters" method="get">
|
||||
<input type="search" name="q" value="{{ q }}" placeholder="Search name or email">
|
||||
<select name="status">
|
||||
<option value="">All statuses</option>
|
||||
{% for value, label in status_choices %}
|
||||
<option value="{{ value }}" {% if status_filter == value %}selected{% endif %}>{{ label }}</option>
|
||||
{% endfor %}
|
||||
</select>
|
||||
<button class="btn btn-sm btn-ghost" type="submit">Filter</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
<div class="panel">
|
||||
<div class="panel-b" style="padding:0">
|
||||
<table class="table">
|
||||
<thead>
|
||||
<tr>
|
||||
<th>Lead</th>
|
||||
<th>Interest</th>
|
||||
<th>UTM / source</th>
|
||||
<th>Status</th>
|
||||
<th>Received</th>
|
||||
</tr>
|
||||
</thead>
|
||||
<tbody>
|
||||
{% for lead in leads %}
|
||||
<tr>
|
||||
<td>
|
||||
<a href="{% url 'leads:detail' lead.pk %}">{{ lead.contact }}</a><br>
|
||||
<span class="muted">{{ lead.contact.email }}{% if lead.contact.phone %} · {{ lead.contact.phone }}{% endif %}</span>
|
||||
</td>
|
||||
<td class="muted">{{ lead.message|truncatechars:40|default:"—" }}</td>
|
||||
<td>
|
||||
{% if lead.attribution %}
|
||||
{{ lead.attribution.utm_source|default:"direct" }}
|
||||
{% if lead.attribution.utm_medium %}/ {{ lead.attribution.utm_medium }}{% endif %}
|
||||
{% if lead.attribution.utm_campaign %}/ {{ lead.attribution.utm_campaign }}{% endif %}
|
||||
{% else %}
|
||||
—
|
||||
{% endif %}
|
||||
</td>
|
||||
<td><span class="badge badge-{{ lead.status }}">{{ lead.get_status_display }}</span></td>
|
||||
<td>{{ lead.created_at|date:"M j, g:i A" }}</td>
|
||||
</tr>
|
||||
{% empty %}
|
||||
<tr><td colspan="5" class="empty-state">No leads match.</td></tr>
|
||||
{% endfor %}
|
||||
</tbody>
|
||||
</table>
|
||||
</div>
|
||||
</div>
|
||||
{% endblock %}
|
||||
@@ -0,0 +1,10 @@
|
||||
from django.urls import path
|
||||
|
||||
from leads import views
|
||||
|
||||
app_name = "leads"
|
||||
|
||||
urlpatterns = [
|
||||
path("", views.lead_list, name="list"),
|
||||
path("<uuid:pk>/", views.lead_detail, name="detail"),
|
||||
]
|
||||
@@ -0,0 +1,51 @@
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.db.models import Q
|
||||
from django.shortcuts import get_object_or_404, render
|
||||
|
||||
from contacts.models import Channel
|
||||
from leads.models import Lead
|
||||
|
||||
|
||||
@login_required
|
||||
def lead_list(request):
|
||||
leads = Lead.objects.select_related("contact", "attribution").all()
|
||||
q = (request.GET.get("q") or "").strip()
|
||||
status = (request.GET.get("status") or "").strip()
|
||||
if q:
|
||||
leads = leads.filter(
|
||||
Q(contact__first_name__icontains=q)
|
||||
| Q(contact__last_name__icontains=q)
|
||||
| Q(contact__email__icontains=q)
|
||||
| Q(contact__phone__icontains=q)
|
||||
| Q(message__icontains=q)
|
||||
)
|
||||
if status:
|
||||
leads = leads.filter(status=status)
|
||||
return render(
|
||||
request,
|
||||
"leads/list.html",
|
||||
{
|
||||
"leads": leads[:200],
|
||||
"status_choices": Lead.Status.choices,
|
||||
"q": q,
|
||||
"status_filter": status,
|
||||
},
|
||||
)
|
||||
|
||||
|
||||
@login_required
|
||||
def lead_detail(request, pk):
|
||||
lead = get_object_or_404(
|
||||
Lead.objects.select_related(
|
||||
"contact", "attribution", "attribution__visit"
|
||||
).prefetch_related("notes", "contact__consents"),
|
||||
pk=pk,
|
||||
)
|
||||
consent_map = {c.value: False for c in Channel}
|
||||
for record in lead.contact.consents.all():
|
||||
consent_map[record.channel] = record.opted_in
|
||||
return render(
|
||||
request,
|
||||
"leads/detail.html",
|
||||
{"lead": lead, "consent_map": consent_map},
|
||||
)
|
||||
Reference in New Issue
Block a user