From 4f55ffeaf732cdb020a39179c99cfb4dc7049be0 Mon Sep 17 00:00:00 2001 From: Mark Veidemanis Date: Thu, 12 Jan 2023 07:20:48 +0000 Subject: [PATCH] Allow overriding topic --- core/forms.py | 6 ++++-- core/lib/notify.py | 13 +++++++------ core/lib/rules.py | 9 ++++++++- core/migrations/0015_notificationrule_topic.py | 18 ++++++++++++++++++ core/models.py | 1 + core/templates/partials/rule-list.html | 4 ++++ 6 files changed, 42 insertions(+), 9 deletions(-) create mode 100644 core/migrations/0015_notificationrule_topic.py diff --git a/core/forms.py b/core/forms.py index fef64df..c988838 100644 --- a/core/forms.py +++ b/core/forms.py @@ -88,13 +88,15 @@ class NotificationRuleForm(RestrictedFormMixin, ModelForm): model = NotificationRule fields = ( "name", - "priority", - "enabled", "data", + "priority", + "topic", + "enabled", ) help_texts = { "name": "The name 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.", "data": "The notification rule definition.", } diff --git a/core/lib/notify.py b/core/lib/notify.py index 8546b51..91ddb15 100644 --- a/core/lib/notify.py +++ b/core/lib/notify.py @@ -29,10 +29,11 @@ def raw_sendmsg(msg, title=None, priority=None, tags=None, url=None, topic=None) def sendmsg(user, *args, **kwargs): notification_settings = user.get_notification_settings() - if notification_settings.ntfy_topic is None: - # No topic set, so don't send - return - else: - topic = notification_settings.ntfy_topic + if "topic" not in kwargs: + if notification_settings.ntfy_topic is None: + # No topic set, so don't send + return + 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) diff --git a/core/lib/rules.py b/core/lib/rules.py index 5400411..7e2c67a 100644 --- a/core/lib/rules.py +++ b/core/lib/rules.py @@ -26,7 +26,14 @@ def rule_matched(rule, message, matched_fields): notify_message = f"{rule.name} matched on {matched_fields}\n{message}" 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): diff --git a/core/migrations/0015_notificationrule_topic.py b/core/migrations/0015_notificationrule_topic.py new file mode 100644 index 0000000..77d25de --- /dev/null +++ b/core/migrations/0015_notificationrule_topic.py @@ -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), + ), + ] diff --git a/core/models.py b/core/models.py index f81fad8..22fc2bd 100644 --- a/core/models.py +++ b/core/models.py @@ -146,6 +146,7 @@ class NotificationRule(models.Model): user = models.ForeignKey(User, on_delete=models.CASCADE) name = models.CharField(max_length=255) priority = models.IntegerField(choices=PRIORITY_CHOICES, default=1) + topic = models.CharField(max_length=255, null=True, blank=True) enabled = models.BooleanField(default=True) data = models.TextField() diff --git a/core/templates/partials/rule-list.html b/core/templates/partials/rule-list.html index a0b6130..fd18dbd 100644 --- a/core/templates/partials/rule-list.html +++ b/core/templates/partials/rule-list.html @@ -11,6 +11,8 @@ id user name + priority + topic enabled data length actions @@ -20,6 +22,8 @@ {{ item.id }} {{ item.user }} {{ item.name }} + {{ item.priority }} + {{ item.topic }} {% if item.enabled %}