51 lines
No EOL
2.1 KiB
Python
51 lines
No EOL
2.1 KiB
Python
from django.db import models
|
|
from django.contrib.contenttypes.fields import GenericForeignKey
|
|
from django.contrib.contenttypes.models import ContentType
|
|
from django.conf import settings
|
|
from django.urls import reverse
|
|
from django.utils.translation import gettext_lazy as _
|
|
from .notification_kind import NotificationKind
|
|
|
|
# ---------------------------------------------------------------------------
|
|
# Notification
|
|
# ---------------------------------------------------------------------------
|
|
class Notification(models.Model):
|
|
|
|
class Meta:
|
|
verbose_name = _("Notification")
|
|
verbose_name_plural = _("Notifications")
|
|
|
|
user = models.ForeignKey(
|
|
settings.AUTH_USER_MODEL,
|
|
on_delete=models.SET_NULL,
|
|
null=True, blank=True,
|
|
related_name="notifications"
|
|
)
|
|
message = models.TextField()
|
|
kind = models.CharField(max_length=40, choices=NotificationKind.choices, default="")
|
|
created_at = models.DateTimeField(auto_now_add=True)
|
|
read = models.BooleanField(default=False) # Read in WebApp
|
|
sent = models.BooleanField(default=False) # Sent via Mail (optional)
|
|
|
|
# Optional relation to any object
|
|
content_type = models.ForeignKey(ContentType, on_delete=models.CASCADE, null=True, blank=True)
|
|
object_id = models.PositiveIntegerField(null=True, blank=True)
|
|
related_object = GenericForeignKey("content_type", "object_id")
|
|
target_url = models.CharField(max_length=500, blank=True, null=True)
|
|
|
|
def __str__(self):
|
|
user_display = self.user.username if self.user else "System"
|
|
return f"{user_display}: {self.message[:50]}..."
|
|
|
|
def get_link(self):
|
|
"""Return URL to the related object if available."""
|
|
if not self.related_object:
|
|
return None
|
|
model_name = self.content_type.model
|
|
if model_name == "risk":
|
|
return reverse("risks:show_risk", args=[self.object_id])
|
|
if model_name == "control":
|
|
return reverse("risks:show_control", args=[self.object_id])
|
|
if model_name == "incident":
|
|
return reverse("risks:show_incident", args=[self.object_id])
|
|
return None |