Allow using webhooks for notifications

This commit is contained in:
2023-01-15 18:40:17 +00:00
parent 46c7d96310
commit eb2486afba
7 changed files with 168 additions and 28 deletions

View File

@@ -35,6 +35,11 @@ INTERVAL_CHOICES = (
(86400, "Every day"),
)
SERVICE_CHOICES = (
("ntfy", "NTFY"),
("webhook", "Custom webhook"),
)
class Plan(models.Model):
name = models.CharField(max_length=255, unique=True)
@@ -166,11 +171,13 @@ class NotificationRule(models.Model):
name = models.CharField(max_length=255)
priority = models.IntegerField(choices=PRIORITY_CHOICES, default=1)
topic = models.CharField(max_length=255, null=True, blank=True)
url = models.CharField(max_length=1024, null=True, blank=True)
interval = models.IntegerField(choices=INTERVAL_CHOICES, default=0)
window = models.CharField(max_length=255, null=True, blank=True)
enabled = models.BooleanField(default=True)
data = models.TextField()
match = models.JSONField(null=True, blank=True)
service = models.CharField(choices=SERVICE_CHOICES, max_length=255, default="ntfy")
def __str__(self):
return f"{self.user} - {self.name}"
@@ -182,11 +189,20 @@ class NotificationRule(models.Model):
raise ValueError(f"Invalid YAML: {e}")
return parsed
@property
def matches(self):
"""
Get the total number of matches for this rule.
"""
if isinstance(self.match, dict):
return f"{sum(list(self.match.values()))}/{len(self.match)}"
class NotificationSettings(models.Model):
user = models.OneToOneField(User, on_delete=models.CASCADE)
ntfy_topic = models.CharField(max_length=255, null=True, blank=True)
ntfy_url = models.CharField(max_length=255, null=True, blank=True)
topic = models.CharField(max_length=255, null=True, blank=True)
url = models.CharField(max_length=1024, null=True, blank=True)
service = models.CharField(choices=SERVICE_CHOICES, max_length=255, default="ntfy")
def __str__(self):
return f"Notification settings for {self.user}"