2023-12-05 03:16:34 +00:00
|
|
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
|
|
|
from django.contrib.contenttypes.models import ContentType
|
|
|
|
from django.db import models
|
|
|
|
from django.utils.translation import gettext_lazy as _
|
|
|
|
|
|
|
|
from common.utils import lazyproperty
|
|
|
|
from orgs.mixins.models import JMSOrgBaseModel
|
|
|
|
|
|
|
|
|
|
|
|
class Label(JMSOrgBaseModel):
|
|
|
|
name = models.CharField(max_length=64, verbose_name=_("Name"), db_index=True)
|
|
|
|
value = models.CharField(max_length=64, unique=False, verbose_name=_("Value"))
|
|
|
|
internal = models.BooleanField(default=False, verbose_name=_("Internal"))
|
2024-06-04 11:08:16 +00:00
|
|
|
color = models.CharField(
|
|
|
|
max_length=32, default="", blank=True, verbose_name=_("Color")
|
|
|
|
)
|
2023-12-05 03:16:34 +00:00
|
|
|
|
|
|
|
class Meta:
|
2024-05-27 03:07:36 +00:00
|
|
|
unique_together = [("name", "value", "org_id")]
|
|
|
|
verbose_name = _("Tag")
|
2023-12-05 03:16:34 +00:00
|
|
|
|
|
|
|
@lazyproperty
|
|
|
|
def res_count(self):
|
|
|
|
return self.labeled_resources.count()
|
|
|
|
|
2023-12-19 10:42:44 +00:00
|
|
|
@lazyproperty
|
|
|
|
def display_name(self):
|
2024-05-27 03:07:36 +00:00
|
|
|
return "{}:{}".format(self.name, self.value)
|
2023-12-19 10:42:44 +00:00
|
|
|
|
2023-12-05 03:16:34 +00:00
|
|
|
def __str__(self):
|
2024-05-27 03:07:36 +00:00
|
|
|
return "{}:{}".format(self.name, self.value)
|
2023-12-05 03:16:34 +00:00
|
|
|
|
|
|
|
|
|
|
|
class LabeledResource(JMSOrgBaseModel):
|
2023-12-18 09:56:16 +00:00
|
|
|
label = models.ForeignKey(
|
2024-05-27 03:07:36 +00:00
|
|
|
Label,
|
|
|
|
on_delete=models.CASCADE,
|
|
|
|
related_name="labeled_resources",
|
|
|
|
verbose_name=_("Tag"),
|
2023-12-18 09:56:16 +00:00
|
|
|
)
|
2023-12-05 03:16:34 +00:00
|
|
|
res_type = models.ForeignKey(ContentType, on_delete=models.CASCADE)
|
2024-05-27 03:07:36 +00:00
|
|
|
res_id = models.CharField(
|
|
|
|
max_length=36, verbose_name=_("Resource ID"), db_index=True
|
|
|
|
)
|
|
|
|
resource = GenericForeignKey("res_type", "res_id")
|
2023-12-05 03:16:34 +00:00
|
|
|
|
|
|
|
class Meta:
|
2024-05-27 03:07:36 +00:00
|
|
|
unique_together = [("label", "res_type", "res_id", "org_id")]
|
|
|
|
verbose_name = _("Tagged resource")
|
2023-12-05 03:16:34 +00:00
|
|
|
|
2023-12-20 12:49:50 +00:00
|
|
|
def __str__(self):
|
2024-05-27 03:07:36 +00:00
|
|
|
return "{} => {}".format(self.label, self.resource)
|