Files
monica_site/site/messaging/models.py
T
westfarn 045e956011
Deploy Beta / unit-tests (push) Successful in 24s
Deploy Beta / docker (push) Successful in 30s
Deploy Beta / deploy-beta (push) Successful in 7m22s
Add PCM carrier-route postcard pick-and-send (#16)
## Summary
Closes #15.
Mirrors westfarn/web_django_template#11.

- Compose can target the postcard mailing list **or** find and select one or two USPS carrier routes by ZIP (optional radius).
- Estimated cost is shown per route and for the selection (PCM design pricing, else `PCM_POSTCARD_PRICE_*` fallback).
- Selected routes order through PCM `POST /order/postcard/with-list-count` — occupant addresses are not imported into Contacts.
- Not official EDDM postage; PCM FirstClass/Standard purchased occupant lists on carrier routes.

## Test plan
- [ ] Campaign compose: mailing-list audience still quotes list size × per-piece cost.
- [ ] Switch to USPS carrier routes, search ZIP 60540 (mocked or live PCM), pick 1–2 routes, confirm household count + estimated cost.
- [ ] Saving without a selection is rejected; saving with a quote creates one contact-less Message and shows the route panel on the report.
- [ ] Send places `/order/postcard/with-list-count` (not per-recipient `/order/postcard`).
- [ ] `uv run python manage.py test messaging.tests.CarrierRoutePostcardTests messaging.tests.PostcardDesignPickTests`

Reviewed-on: #16
2026-09-21 13:29:20 -07:00

152 lines
5.1 KiB
Python

from django.conf import settings
from django.db import models
from contacts.models import Channel, Contact
from core.models import TimeStampedModel, UUIDPrimaryKeyModel
class MessageTemplate(UUIDPrimaryKeyModel, TimeStampedModel):
name = models.CharField(max_length=120)
channel = models.CharField(max_length=16, choices=Channel.choices)
subject = models.CharField(max_length=255, blank=True)
body = models.TextField()
postcard_front = models.JSONField(default=dict, blank=True)
postcard_back = models.JSONField(default=dict, blank=True)
def __str__(self) -> str:
return f"{self.name} ({self.channel})"
class Campaign(UUIDPrimaryKeyModel, TimeStampedModel):
class Status(models.TextChoices):
DRAFT = "draft", "Draft"
SCHEDULED = "scheduled", "Scheduled"
SENDING = "sending", "Sending"
COMPLETED = "completed", "Completed"
CANCELLED = "cancelled", "Cancelled"
class Audience(models.TextChoices):
EMAIL_OPT_IN = "email_opt_in", "Mailing list · email opt-in"
SMS_OPT_IN = "sms_opt_in", "Mailing list · SMS opt-in"
POSTCARD_OPT_IN = "postcard_opt_in", "Mailing list · postcard opt-in"
CARRIER_ROUTE = "carrier_route", "USPS carrier routes"
name = models.CharField(max_length=120)
channel = models.CharField(max_length=16, choices=Channel.choices)
audience = models.CharField(
max_length=32,
choices=Audience.choices,
blank=True,
default="",
)
template = models.ForeignKey(
MessageTemplate,
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="campaigns",
)
status = models.CharField(
max_length=16, choices=Status.choices, default=Status.DRAFT
)
scheduled_for = models.DateTimeField(null=True, blank=True)
created_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
null=True,
blank=True,
on_delete=models.SET_NULL,
)
subject_override = models.CharField(max_length=255, blank=True)
body_override = models.TextField(blank=True)
# Set when realtor summary email is sent (campaign COMPLETED).
notify_sent_at = models.DateTimeField(null=True, blank=True)
# PCM purchased-list targeting (ZIP/CRRT codes, listCountID, quote).
route_targeting = models.JSONField(default=dict, blank=True)
class Meta:
ordering = ["-created_at"]
def __str__(self) -> str:
return self.name
class Message(UUIDPrimaryKeyModel, TimeStampedModel):
class Status(models.TextChoices):
DRAFT = "draft", "Draft"
SCHEDULED = "scheduled", "Scheduled"
QUEUED = "queued", "Queued"
SENT = "sent", "Sent"
DELIVERED = "delivered", "Delivered"
OPENED = "opened", "Opened"
CLICKED = "clicked", "Clicked"
FAILED = "failed", "Failed"
BOUNCED = "bounced", "Bounced"
SUPPRESSED = "suppressed", "Suppressed"
campaign = models.ForeignKey(
Campaign, on_delete=models.CASCADE, related_name="messages"
)
contact = models.ForeignKey(
Contact, null=True, blank=True, on_delete=models.CASCADE, related_name="messages"
)
channel = models.CharField(max_length=16, choices=Channel.choices)
status = models.CharField(
max_length=16, choices=Status.choices, default=Status.DRAFT
)
provider = models.CharField(max_length=64, blank=True)
provider_message_id = models.CharField(max_length=255, blank=True)
scheduled_for = models.DateTimeField(null=True, blank=True)
sent_at = models.DateTimeField(null=True, blank=True)
error = models.TextField(blank=True)
body_snapshot = models.TextField(blank=True)
class Meta:
ordering = ["-created_at"]
def __str__(self) -> str:
who = self.contact or "carrier-route mailing"
return f"{self.channel} → {who} ({self.status})"
class ProviderEvent(TimeStampedModel):
message = models.ForeignKey(
Message,
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="events",
)
provider = models.CharField(max_length=64)
event_type = models.CharField(max_length=64)
payload = models.JSONField(default=dict, blank=True)
class StoredFile(UUIDPrimaryKeyModel, TimeStampedModel):
"""Binary file blob in the database (no filesystem media storage)."""
class Kind(models.TextChoices):
CAMPAIGN_IMAGE = "campaign_image", "Campaign image"
SOCIAL_IMAGE = "social_image", "Social image"
SOCIAL_VIDEO = "social_video", "Social video"
kind = models.CharField(
max_length=32, choices=Kind.choices, default=Kind.CAMPAIGN_IMAGE
)
filename = models.CharField(max_length=255, blank=True)
content_type = models.CharField(max_length=128)
size = models.PositiveIntegerField(default=0)
data = models.BinaryField()
uploaded_by = models.ForeignKey(
settings.AUTH_USER_MODEL,
null=True,
blank=True,
on_delete=models.SET_NULL,
related_name="uploaded_files",
)
class Meta:
ordering = ["-created_at"]
def __str__(self) -> str:
return self.filename or str(self.pk)