forked from erp-dev/erp
feat: mission category
This commit is contained in:
@@ -1,4 +1,6 @@
|
||||
from django.core.exceptions import ValidationError
|
||||
from django.db import models
|
||||
from django.db.models import Q
|
||||
|
||||
from flower.common import ModelBase
|
||||
|
||||
@@ -25,12 +27,6 @@ class Notifier(ModelBase):
|
||||
verbose_name="所属商户",
|
||||
)
|
||||
name = models.CharField(max_length=100, verbose_name="通知器名称")
|
||||
event_key = models.CharField(
|
||||
max_length=100,
|
||||
choices=NotificationEventKeyEnum.choices,
|
||||
db_index=True,
|
||||
verbose_name="事件标识",
|
||||
)
|
||||
channel = models.CharField(
|
||||
max_length=50,
|
||||
choices=NotifierChannelEnum.choices,
|
||||
@@ -50,7 +46,7 @@ class Notifier(ModelBase):
|
||||
return config.get(key, default)
|
||||
|
||||
def __str__(self):
|
||||
return f"{self.name} ({self.event_key})"
|
||||
return self.name
|
||||
|
||||
class Meta:
|
||||
verbose_name = "通知器"
|
||||
@@ -59,5 +55,70 @@ class Notifier(ModelBase):
|
||||
models.UniqueConstraint(fields=["merchant", "name"], name="unique_notifier_name_per_merchant"),
|
||||
]
|
||||
indexes = [
|
||||
models.Index(fields=["merchant", "event_key", "is_enabled"]),
|
||||
models.Index(fields=["merchant", "channel", "is_enabled"], name="notifier_no_merchan_532b51_idx"),
|
||||
]
|
||||
|
||||
|
||||
class NotifierRoute(ModelBase):
|
||||
id = models.BigAutoField(primary_key=True)
|
||||
merchant = models.ForeignKey(
|
||||
"basic_info.Merchant",
|
||||
on_delete=models.PROTECT,
|
||||
related_name="notifier_routes",
|
||||
verbose_name="所属商户",
|
||||
)
|
||||
notifier = models.ForeignKey(
|
||||
Notifier,
|
||||
on_delete=models.CASCADE,
|
||||
related_name="routes",
|
||||
verbose_name="通知器",
|
||||
)
|
||||
event_key = models.CharField(
|
||||
max_length=100,
|
||||
choices=NotificationEventKeyEnum.choices,
|
||||
db_index=True,
|
||||
verbose_name="事件标识",
|
||||
)
|
||||
mission_category = models.ForeignKey(
|
||||
"mission.MissionCategory",
|
||||
on_delete=models.PROTECT,
|
||||
related_name="notifier_routes",
|
||||
null=True,
|
||||
blank=True,
|
||||
verbose_name="任务分类路由",
|
||||
help_text="为空时表示该事件的通配路由",
|
||||
)
|
||||
is_enabled = models.BooleanField(default=True, db_index=True, verbose_name="是否启用")
|
||||
description = models.TextField(blank=True, null=True, verbose_name="备注描述")
|
||||
|
||||
def clean(self):
|
||||
errors = {}
|
||||
if self.notifier_id and self.merchant_id and self.notifier.merchant_id != self.merchant_id:
|
||||
errors["merchant"] = "路由所属商户必须与通知器所属商户一致"
|
||||
if self.mission_category_id and self.merchant_id and self.mission_category.merchant_id != self.merchant_id:
|
||||
errors["mission_category"] = "任务分类必须属于当前路由商户"
|
||||
if errors:
|
||||
raise ValidationError(errors)
|
||||
|
||||
def __str__(self):
|
||||
category_name = self.mission_category.name if self.mission_category_id else "全部分类"
|
||||
return f"{self.notifier.name} -> {self.event_key} [{category_name}]"
|
||||
|
||||
class Meta:
|
||||
verbose_name = "通知路由"
|
||||
verbose_name_plural = "通知路由"
|
||||
constraints = [
|
||||
models.UniqueConstraint(
|
||||
fields=["notifier", "event_key", "mission_category"],
|
||||
name="unique_notifier_route_per_scope",
|
||||
),
|
||||
models.UniqueConstraint(
|
||||
fields=["notifier", "event_key"],
|
||||
condition=Q(mission_category__isnull=True),
|
||||
name="unique_notifier_route_global_scope",
|
||||
),
|
||||
]
|
||||
indexes = [
|
||||
models.Index(fields=["merchant", "event_key", "is_enabled"], name="notifier_no_merchan_58d463_idx"),
|
||||
models.Index(fields=["merchant", "mission_category", "is_enabled"], name="notifier_no_merchan_14b7f6_idx"),
|
||||
]
|
||||
|
||||
Reference in New Issue
Block a user