Implement business plans
This commit is contained in:
317
core/models.py
317
core/models.py
@@ -18,6 +18,7 @@ SERVICE_CHOICES = (
|
||||
("xmpp", "XMPP"),
|
||||
("instagram", "Instagram"),
|
||||
)
|
||||
CHANNEL_SERVICE_CHOICES = SERVICE_CHOICES + (("web", "Web"),)
|
||||
MBTI_CHOICES = (
|
||||
("INTJ", "INTJ - Architect"),
|
||||
("INTP", "INTP - Logician"),
|
||||
@@ -297,9 +298,61 @@ class Message(models.Model):
|
||||
blank=True,
|
||||
help_text="Raw normalized delivery/read receipt metadata.",
|
||||
)
|
||||
source_service = models.CharField(
|
||||
max_length=255,
|
||||
choices=CHANNEL_SERVICE_CHOICES,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="Source service where this message originally appeared.",
|
||||
)
|
||||
source_message_id = models.CharField(
|
||||
max_length=255,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="Source service message id when available.",
|
||||
)
|
||||
source_chat_id = models.CharField(
|
||||
max_length=255,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="Source service chat or thread identifier when available.",
|
||||
)
|
||||
reply_to = models.ForeignKey(
|
||||
"self",
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name="reply_children",
|
||||
help_text="Resolved local message this message replies to.",
|
||||
)
|
||||
reply_source_service = models.CharField(
|
||||
max_length=255,
|
||||
choices=CHANNEL_SERVICE_CHOICES,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="Source service for the replied target.",
|
||||
)
|
||||
reply_source_message_id = models.CharField(
|
||||
max_length=255,
|
||||
null=True,
|
||||
blank=True,
|
||||
help_text="Source message id for the replied target.",
|
||||
)
|
||||
message_meta = models.JSONField(
|
||||
default=dict,
|
||||
blank=True,
|
||||
help_text="Normalized message metadata such as origin tags.",
|
||||
)
|
||||
|
||||
class Meta:
|
||||
ordering = ["ts"]
|
||||
indexes = [
|
||||
models.Index(fields=["user", "source_service", "source_message_id"]),
|
||||
models.Index(fields=["user", "session", "ts"]),
|
||||
models.Index(
|
||||
fields=["user", "reply_source_service", "reply_source_message_id"]
|
||||
),
|
||||
]
|
||||
|
||||
|
||||
class Group(models.Model):
|
||||
@@ -1568,6 +1621,270 @@ class PatternArtifactExport(models.Model):
|
||||
)
|
||||
|
||||
|
||||
class CommandProfile(models.Model):
|
||||
WINDOW_SCOPE_CHOICES = (
|
||||
("conversation", "Conversation"),
|
||||
)
|
||||
VISIBILITY_CHOICES = (
|
||||
("status_in_source", "Status In Source"),
|
||||
("silent", "Silent"),
|
||||
)
|
||||
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
slug = models.CharField(max_length=64, default="bp")
|
||||
name = models.CharField(max_length=255, default="Business Plan")
|
||||
enabled = models.BooleanField(default=True)
|
||||
trigger_token = models.CharField(max_length=64, default="#bp#")
|
||||
reply_required = models.BooleanField(default=True)
|
||||
exact_match_only = models.BooleanField(default=True)
|
||||
window_scope = models.CharField(
|
||||
max_length=64,
|
||||
choices=WINDOW_SCOPE_CHOICES,
|
||||
default="conversation",
|
||||
)
|
||||
template_text = models.TextField(blank=True, default="")
|
||||
visibility_mode = models.CharField(
|
||||
max_length=64,
|
||||
choices=VISIBILITY_CHOICES,
|
||||
default="status_in_source",
|
||||
)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
constraints = [
|
||||
models.UniqueConstraint(
|
||||
fields=["user", "slug"],
|
||||
name="unique_command_profile_per_user",
|
||||
)
|
||||
]
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.user_id}:{self.slug}"
|
||||
|
||||
|
||||
class CommandChannelBinding(models.Model):
|
||||
DIRECTION_CHOICES = (
|
||||
("ingress", "Ingress"),
|
||||
("egress", "Egress"),
|
||||
("scratchpad_mirror", "Scratchpad Mirror"),
|
||||
)
|
||||
|
||||
profile = models.ForeignKey(
|
||||
CommandProfile,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="channel_bindings",
|
||||
)
|
||||
direction = models.CharField(max_length=64, choices=DIRECTION_CHOICES)
|
||||
service = models.CharField(max_length=255, choices=CHANNEL_SERVICE_CHOICES)
|
||||
channel_identifier = models.CharField(max_length=255)
|
||||
enabled = models.BooleanField(default=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
indexes = [
|
||||
models.Index(fields=["profile", "direction", "service"]),
|
||||
models.Index(fields=["profile", "service", "channel_identifier"]),
|
||||
]
|
||||
|
||||
|
||||
class CommandAction(models.Model):
|
||||
ACTION_CHOICES = (
|
||||
("extract_bp", "Extract Business Plan"),
|
||||
("post_result", "Post Result"),
|
||||
("save_document", "Save Document"),
|
||||
)
|
||||
|
||||
profile = models.ForeignKey(
|
||||
CommandProfile,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="actions",
|
||||
)
|
||||
action_type = models.CharField(max_length=64, choices=ACTION_CHOICES)
|
||||
enabled = models.BooleanField(default=True)
|
||||
config = models.JSONField(default=dict, blank=True)
|
||||
position = models.PositiveIntegerField(default=0)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["position", "id"]
|
||||
indexes = [models.Index(fields=["profile", "action_type", "enabled"])]
|
||||
|
||||
|
||||
class BusinessPlanDocument(models.Model):
|
||||
STATUS_CHOICES = (
|
||||
("draft", "Draft"),
|
||||
("final", "Final"),
|
||||
)
|
||||
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
command_profile = models.ForeignKey(
|
||||
CommandProfile,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name="business_plan_documents",
|
||||
)
|
||||
source_service = models.CharField(max_length=255, choices=CHANNEL_SERVICE_CHOICES)
|
||||
source_channel_identifier = models.CharField(max_length=255, blank=True, default="")
|
||||
trigger_message = models.ForeignKey(
|
||||
Message,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name="business_plan_trigger_docs",
|
||||
)
|
||||
anchor_message = models.ForeignKey(
|
||||
Message,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name="business_plan_anchor_docs",
|
||||
)
|
||||
title = models.CharField(max_length=255, default="Business Plan")
|
||||
status = models.CharField(max_length=32, choices=STATUS_CHOICES, default="draft")
|
||||
content_markdown = models.TextField(blank=True, default="")
|
||||
structured_payload = models.JSONField(default=dict, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
indexes = [
|
||||
models.Index(fields=["user", "status", "updated_at"]),
|
||||
models.Index(fields=["user", "source_service", "source_channel_identifier"]),
|
||||
]
|
||||
|
||||
|
||||
class BusinessPlanRevision(models.Model):
|
||||
document = models.ForeignKey(
|
||||
BusinessPlanDocument,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="revisions",
|
||||
)
|
||||
editor_user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
content_markdown = models.TextField(blank=True, default="")
|
||||
structured_payload = models.JSONField(default=dict, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
|
||||
class Meta:
|
||||
ordering = ["created_at"]
|
||||
|
||||
|
||||
class CommandRun(models.Model):
|
||||
STATUS_CHOICES = (
|
||||
("pending", "Pending"),
|
||||
("running", "Running"),
|
||||
("ok", "OK"),
|
||||
("failed", "Failed"),
|
||||
("skipped", "Skipped"),
|
||||
)
|
||||
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
profile = models.ForeignKey(
|
||||
CommandProfile,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="runs",
|
||||
)
|
||||
trigger_message = models.ForeignKey(
|
||||
Message,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="command_runs",
|
||||
)
|
||||
status = models.CharField(max_length=32, choices=STATUS_CHOICES, default="pending")
|
||||
error = models.TextField(blank=True, default="")
|
||||
result_ref = models.ForeignKey(
|
||||
BusinessPlanDocument,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name="command_runs",
|
||||
)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
constraints = [
|
||||
models.UniqueConstraint(
|
||||
fields=["profile", "trigger_message"],
|
||||
name="unique_command_run_profile_trigger_message",
|
||||
)
|
||||
]
|
||||
indexes = [models.Index(fields=["user", "status", "updated_at"])]
|
||||
|
||||
|
||||
class TranslationBridge(models.Model):
|
||||
DIRECTION_CHOICES = (
|
||||
("a_to_b", "A To B"),
|
||||
("b_to_a", "B To A"),
|
||||
("bidirectional", "Bidirectional"),
|
||||
)
|
||||
|
||||
user = models.ForeignKey(User, on_delete=models.CASCADE)
|
||||
name = models.CharField(max_length=255, default="Translation Bridge")
|
||||
enabled = models.BooleanField(default=True)
|
||||
a_service = models.CharField(max_length=255, choices=CHANNEL_SERVICE_CHOICES)
|
||||
a_channel_identifier = models.CharField(max_length=255)
|
||||
a_language = models.CharField(max_length=64, default="en")
|
||||
b_service = models.CharField(max_length=255, choices=CHANNEL_SERVICE_CHOICES)
|
||||
b_channel_identifier = models.CharField(max_length=255)
|
||||
b_language = models.CharField(max_length=64, default="en")
|
||||
direction = models.CharField(
|
||||
max_length=32,
|
||||
choices=DIRECTION_CHOICES,
|
||||
default="bidirectional",
|
||||
)
|
||||
quick_mode_title = models.CharField(max_length=255, blank=True, default="")
|
||||
settings = models.JSONField(default=dict, blank=True)
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
indexes = [
|
||||
models.Index(fields=["user", "enabled"]),
|
||||
models.Index(fields=["user", "a_service", "a_channel_identifier"]),
|
||||
models.Index(fields=["user", "b_service", "b_channel_identifier"]),
|
||||
]
|
||||
|
||||
|
||||
class TranslationEventLog(models.Model):
|
||||
STATUS_CHOICES = (
|
||||
("pending", "Pending"),
|
||||
("ok", "OK"),
|
||||
("failed", "Failed"),
|
||||
("skipped", "Skipped"),
|
||||
)
|
||||
|
||||
bridge = models.ForeignKey(
|
||||
TranslationBridge,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="events",
|
||||
)
|
||||
source_message = models.ForeignKey(
|
||||
Message,
|
||||
on_delete=models.SET_NULL,
|
||||
null=True,
|
||||
blank=True,
|
||||
related_name="translation_events",
|
||||
)
|
||||
target_service = models.CharField(max_length=255, choices=CHANNEL_SERVICE_CHOICES)
|
||||
target_channel = models.CharField(max_length=255)
|
||||
status = models.CharField(max_length=32, choices=STATUS_CHOICES, default="pending")
|
||||
error = models.TextField(blank=True, default="")
|
||||
origin_tag = models.CharField(max_length=255, blank=True, default="")
|
||||
content_hash = models.CharField(max_length=255, blank=True, default="")
|
||||
created_at = models.DateTimeField(auto_now_add=True)
|
||||
updated_at = models.DateTimeField(auto_now=True)
|
||||
|
||||
class Meta:
|
||||
indexes = [
|
||||
models.Index(fields=["bridge", "created_at"]),
|
||||
models.Index(fields=["bridge", "status", "updated_at"]),
|
||||
models.Index(fields=["origin_tag"]),
|
||||
]
|
||||
|
||||
|
||||
# class Perms(models.Model):
|
||||
# class Meta:
|
||||
# permissions = (
|
||||
|
||||
Reference in New Issue
Block a user