generated from westfarn/web_django_template
150 lines
4.6 KiB
Python
150 lines
4.6 KiB
Python
from django.conf import settings
|
|
from django.db import models
|
|
|
|
from core.models import TimeStampedModel, UUIDPrimaryKeyModel
|
|
|
|
_GENERIC_ACCOUNT_LABELS = frozenset(
|
|
{
|
|
"linkedin member",
|
|
"instagram account",
|
|
"facebook page",
|
|
}
|
|
)
|
|
|
|
|
|
def is_generic_account_label(label: str) -> bool:
|
|
"""True when OAuth left a placeholder or unreadable name."""
|
|
text = (label or "").strip()
|
|
if not text:
|
|
return True
|
|
lowered = text.lower()
|
|
if lowered in _GENERIC_ACCOUNT_LABELS:
|
|
return True
|
|
if lowered.startswith("page ") and text.split()[-1].isdigit():
|
|
return True
|
|
if text.startswith("urn:"):
|
|
return True
|
|
return False
|
|
|
|
|
|
class Platform(models.TextChoices):
|
|
FACEBOOK = "facebook", "Facebook"
|
|
INSTAGRAM = "instagram", "Instagram"
|
|
LINKEDIN = "linkedin", "LinkedIn"
|
|
|
|
|
|
class SocialAppCredentials(UUIDPrimaryKeyModel, TimeStampedModel):
|
|
"""
|
|
Developer-app OAuth credentials entered in the portal (not env).
|
|
|
|
client_secret is Fernet-encrypted. One row per platform.
|
|
"""
|
|
|
|
platform = models.CharField(max_length=16, choices=Platform.choices, unique=True)
|
|
client_id = models.CharField(max_length=255, blank=True)
|
|
encrypted_client_secret = models.TextField(blank=True)
|
|
|
|
class Meta:
|
|
verbose_name_plural = "social app credentials"
|
|
ordering = ["platform"]
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.platform} app credentials"
|
|
|
|
@property
|
|
def is_configured(self) -> bool:
|
|
return bool(self.client_id and self.encrypted_client_secret)
|
|
|
|
|
|
class SocialAccount(UUIDPrimaryKeyModel, TimeStampedModel):
|
|
platform = models.CharField(max_length=16, choices=Platform.choices)
|
|
label = models.CharField(max_length=120)
|
|
external_id = models.CharField(max_length=255, blank=True)
|
|
# Fernet-encrypted JSON blob of OAuth tokens
|
|
encrypted_tokens = models.TextField(blank=True)
|
|
is_active = models.BooleanField(default=True)
|
|
owner = models.ForeignKey(
|
|
settings.AUTH_USER_MODEL,
|
|
null=True,
|
|
blank=True,
|
|
on_delete=models.SET_NULL,
|
|
related_name="social_accounts",
|
|
)
|
|
|
|
class Meta:
|
|
ordering = ["platform", "label"]
|
|
|
|
def __str__(self) -> str:
|
|
return f"{self.platform}: {self.label}"
|
|
|
|
@property
|
|
def display_name(self) -> str:
|
|
return (self.label or "").strip() or self.get_platform_display()
|
|
|
|
@staticmethod
|
|
def resolve_label(existing: "SocialAccount | None", incoming: str) -> str:
|
|
"""Keep a custom rename; otherwise take the OAuth name."""
|
|
incoming = (incoming or "").strip()[:120]
|
|
if existing is not None and not is_generic_account_label(existing.label):
|
|
return existing.label
|
|
if incoming:
|
|
return incoming
|
|
if existing and existing.label:
|
|
return existing.label
|
|
return "Account"
|
|
|
|
|
|
class SocialPost(UUIDPrimaryKeyModel, TimeStampedModel):
|
|
class Status(models.TextChoices):
|
|
DRAFT = "draft", "Draft"
|
|
SCHEDULED = "scheduled", "Scheduled"
|
|
QUEUED = "queued", "Queued"
|
|
PUBLISHING = "publishing", "Publishing"
|
|
PUBLISHED = "published", "Published"
|
|
FAILED = "failed", "Failed"
|
|
CANCELLED = "cancelled", "Cancelled"
|
|
|
|
body = models.TextField()
|
|
media = models.JSONField(default=list, blank=True)
|
|
scheduled_for = models.DateTimeField(null=True, blank=True)
|
|
status = models.CharField(
|
|
max_length=16, choices=Status.choices, default=Status.DRAFT
|
|
)
|
|
created_by = models.ForeignKey(
|
|
settings.AUTH_USER_MODEL,
|
|
null=True,
|
|
blank=True,
|
|
on_delete=models.SET_NULL,
|
|
)
|
|
ollama_prompt = models.TextField(blank=True)
|
|
error = models.TextField(blank=True)
|
|
|
|
class Meta:
|
|
ordering = ["-created_at"]
|
|
|
|
def __str__(self) -> str:
|
|
return f"Post {self.pk} ({self.status})"
|
|
|
|
|
|
class SocialPostTarget(UUIDPrimaryKeyModel, TimeStampedModel):
|
|
class Status(models.TextChoices):
|
|
PENDING = "pending", "Pending"
|
|
PUBLISHED = "published", "Published"
|
|
FAILED = "failed", "Failed"
|
|
|
|
post = models.ForeignKey(
|
|
SocialPost, on_delete=models.CASCADE, related_name="targets"
|
|
)
|
|
account = models.ForeignKey(
|
|
SocialAccount, on_delete=models.CASCADE, related_name="targets"
|
|
)
|
|
platform = models.CharField(max_length=16, choices=Platform.choices)
|
|
status = models.CharField(
|
|
max_length=16, choices=Status.choices, default=Status.PENDING
|
|
)
|
|
remote_id = models.CharField(max_length=255, blank=True)
|
|
error = models.TextField(blank=True)
|
|
|
|
class Meta:
|
|
unique_together = ("post", "account")
|