Allow overriding topic

This commit is contained in:
Mark Veidemanis 2023-01-12 07:20:48 +00:00
parent 0b840d227b
commit 4f55ffeaf7
Signed by: m
GPG Key ID: 5ACFCEED46C0904F
6 changed files with 42 additions and 9 deletions

View File

@ -88,13 +88,15 @@ class NotificationRuleForm(RestrictedFormMixin, ModelForm):
model = NotificationRule model = NotificationRule
fields = ( fields = (
"name", "name",
"priority",
"enabled",
"data", "data",
"priority",
"topic",
"enabled",
) )
help_texts = { help_texts = {
"name": "The name of the rule.", "name": "The name of the rule.",
"priority": "The priority of the rule.", "priority": "The priority of the rule.",
"topic": "The topic to send notifications to. Leave blank for default.",
"enabled": "Whether the rule is enabled.", "enabled": "Whether the rule is enabled.",
"data": "The notification rule definition.", "data": "The notification rule definition.",
} }

View File

@ -29,10 +29,11 @@ def raw_sendmsg(msg, title=None, priority=None, tags=None, url=None, topic=None)
def sendmsg(user, *args, **kwargs): def sendmsg(user, *args, **kwargs):
notification_settings = user.get_notification_settings() notification_settings = user.get_notification_settings()
if notification_settings.ntfy_topic is None: if "topic" not in kwargs:
# No topic set, so don't send if notification_settings.ntfy_topic is None:
return # No topic set, so don't send
else: return
topic = notification_settings.ntfy_topic else:
kwargs["topic"] = notification_settings.ntfy_topic
raw_sendmsg(*args, **kwargs, url=notification_settings.ntfy_url, topic=topic) raw_sendmsg(*args, **kwargs, url=notification_settings.ntfy_url)

View File

@ -26,7 +26,14 @@ def rule_matched(rule, message, matched_fields):
notify_message = f"{rule.name} matched on {matched_fields}\n{message}" notify_message = f"{rule.name} matched on {matched_fields}\n{message}"
notify_message = notify_message.encode("utf-8", "replace") notify_message = notify_message.encode("utf-8", "replace")
sendmsg(rule.user, notify_message, title=title, priority=str(rule.priority))
cast = {
"title": title,
"priority": str(rule.priority),
}
if rule.topic is not None:
cast["topic"] = rule.topic
sendmsg(rule.user, notify_message, **cast)
def process_rules(data): def process_rules(data):

View File

@ -0,0 +1,18 @@
# Generated by Django 4.1.5 on 2023-01-12 18:14
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
('core', '0014_notificationrule_priority'),
]
operations = [
migrations.AddField(
model_name='notificationrule',
name='topic',
field=models.CharField(blank=True, max_length=255, null=True),
),
]

View File

@ -146,6 +146,7 @@ class NotificationRule(models.Model):
user = models.ForeignKey(User, on_delete=models.CASCADE) user = models.ForeignKey(User, on_delete=models.CASCADE)
name = models.CharField(max_length=255) name = models.CharField(max_length=255)
priority = models.IntegerField(choices=PRIORITY_CHOICES, default=1) priority = models.IntegerField(choices=PRIORITY_CHOICES, default=1)
topic = models.CharField(max_length=255, null=True, blank=True)
enabled = models.BooleanField(default=True) enabled = models.BooleanField(default=True)
data = models.TextField() data = models.TextField()

View File

@ -11,6 +11,8 @@
<th>id</th> <th>id</th>
<th>user</th> <th>user</th>
<th>name</th> <th>name</th>
<th>priority</th>
<th>topic</th>
<th>enabled</th> <th>enabled</th>
<th>data length</th> <th>data length</th>
<th>actions</th> <th>actions</th>
@ -20,6 +22,8 @@
<td>{{ item.id }}</td> <td>{{ item.id }}</td>
<td>{{ item.user }}</td> <td>{{ item.user }}</td>
<td>{{ item.name }}</td> <td>{{ item.name }}</td>
<td>{{ item.priority }}</td>
<td>{{ item.topic }}</td>
<td> <td>
{% if item.enabled %} {% if item.enabled %}
<span class="icon"> <span class="icon">