Implement notification rules and settings
parent
a70bc16d22
commit
f93d37d1c0
@ -0,0 +1,60 @@
|
||||
from core.db.storage import db
|
||||
from yaml import load, dump
|
||||
from yaml.scanner import ScannerError
|
||||
from yaml.parser import ParserError
|
||||
try:
|
||||
from yaml import CLoader as Loader, CDumper as Dumper
|
||||
except ImportError:
|
||||
from yaml import Loader, Dumper
|
||||
|
||||
class NotificationRuleData(object):
|
||||
def __init__(self, user, data):
|
||||
self.user = user
|
||||
self.data = data
|
||||
self.parsed = None
|
||||
|
||||
self.parse_data()
|
||||
self.validate_permissions()
|
||||
|
||||
def validate_permissions(self):
|
||||
"""
|
||||
Validate permissions for the source and index variables.
|
||||
"""
|
||||
if "index" in self.parsed:
|
||||
index = self.parsed["index"]
|
||||
if type(index) == list:
|
||||
for i in index:
|
||||
db.parse_index(self.user, {"index": i}, raise_error=True)
|
||||
else:
|
||||
db.parse_index(self.user, {"index": index}, raise_error=True)
|
||||
else:
|
||||
# Get the default value for the user if not present
|
||||
index = db.parse_index(self.user, {}, raise_error=True)
|
||||
self.parsed["index"] = index
|
||||
|
||||
if "source" in self.parsed:
|
||||
source = self.parsed["source"]
|
||||
if type(source) == list:
|
||||
for i in source:
|
||||
db.parse_source(self.user, {"source": i}, raise_error=True)
|
||||
else:
|
||||
db.parse_source(self.user, {"source": source}, raise_error=True)
|
||||
else:
|
||||
# Get the default value for the user if not present
|
||||
source = db.parse_source(self.user, {}, raise_error=True)
|
||||
self.parsed["source"] = source
|
||||
|
||||
def parse_data(self):
|
||||
"""
|
||||
Parse the data in the text field to YAML.
|
||||
"""
|
||||
try:
|
||||
self.parsed = load(self.data, Loader=Loader)
|
||||
except (ScannerError, ParserError) as e:
|
||||
raise ValueError(f"Invalid YAML: {e}")
|
||||
|
||||
def __str__(self):
|
||||
return dump(self.parsed, Dumper=Dumper)
|
||||
|
||||
def get_data(self):
|
||||
return self.parsed
|
@ -0,0 +1,25 @@
|
||||
# Generated by Django 4.1.3 on 2023-01-12 15:12
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0011_alter_perms_options'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='NotificationRule',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('name', models.CharField(max_length=255)),
|
||||
('enabled', models.BooleanField(default=True)),
|
||||
('data', models.TextField()),
|
||||
('user', models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
@ -0,0 +1,24 @@
|
||||
# Generated by Django 4.1.3 on 2023-01-12 15:25
|
||||
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
import django.db.models.deletion
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
('core', '0012_notificationrule'),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='NotificationSettings',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('ntfy_topic', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('ntfy_url', models.CharField(blank=True, max_length=255, null=True)),
|
||||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
@ -0,0 +1,69 @@
|
||||
{% include 'partials/notify.html' %}
|
||||
|
||||
<table
|
||||
class="table is-fullwidth is-hoverable"
|
||||
hx-target="#{{ context_object_name }}-table"
|
||||
id="{{ context_object_name }}-table"
|
||||
hx-swap="outerHTML"
|
||||
hx-trigger="{{ context_object_name_singular }}Event from:body"
|
||||
hx-get="{{ list_url }}">
|
||||
<thead>
|
||||
<th>id</th>
|
||||
<th>user</th>
|
||||
<th>name</th>
|
||||
<th>enabled</th>
|
||||
<th>data length</th>
|
||||
<th>actions</th>
|
||||
</thead>
|
||||
{% for item in object_list %}
|
||||
<tr>
|
||||
<td>{{ item.id }}</td>
|
||||
<td>{{ item.user }}</td>
|
||||
<td>{{ item.name }}</td>
|
||||
<td>
|
||||
{% if item.enabled %}
|
||||
<span class="icon">
|
||||
<i class="fa-solid fa-check"></i>
|
||||
</span>
|
||||
{% else %}
|
||||
<span class="icon">
|
||||
<i class="fa-solid fa-xmark"></i>
|
||||
</span>
|
||||
{% endif %}
|
||||
</td>
|
||||
<td>{{ item.data|length }}</td>
|
||||
<td>
|
||||
<div class="buttons">
|
||||
<button
|
||||
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
|
||||
hx-get="{% url 'rule_update' type=type pk=item.id %}"
|
||||
hx-trigger="click"
|
||||
hx-target="#{{ type }}s-here"
|
||||
hx-swap="innerHTML"
|
||||
class="button">
|
||||
<span class="icon-text">
|
||||
<span class="icon">
|
||||
<i class="fa-solid fa-pencil"></i>
|
||||
</span>
|
||||
</span>
|
||||
</button>
|
||||
<button
|
||||
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
|
||||
hx-delete="{% url 'rule_delete' type=type pk=item.id %}"
|
||||
hx-trigger="click"
|
||||
hx-target="#modals-here"
|
||||
hx-swap="innerHTML"
|
||||
hx-confirm="Are you sure you wish to delete {{ item.name }}?"
|
||||
class="button">
|
||||
<span class="icon-text">
|
||||
<span class="icon">
|
||||
<i class="fa-solid fa-xmark"></i>
|
||||
</span>
|
||||
</span>
|
||||
</button>
|
||||
</div>
|
||||
</td>
|
||||
</tr>
|
||||
{% endfor %}
|
||||
|
||||
</table>
|
@ -0,0 +1,34 @@
|
||||
{% include 'partials/notify.html' %}
|
||||
{% if page_title is not None %}
|
||||
<h1 class="title is-4">{{ page_title }}</h1>
|
||||
{% endif %}
|
||||
{% if page_subtitle is not None %}
|
||||
<h1 class="subtitle">{{ page_subtitle }}</h1>
|
||||
{% endif %}
|
||||
{% load crispy_forms_tags %}
|
||||
|
||||
{% load crispy_forms_bulma_field %}
|
||||
<form
|
||||
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
|
||||
hx-post="{{ submit_url }}"
|
||||
hx-target="#modals-here"
|
||||
hx-swap="innerHTML">
|
||||
{% csrf_token %}
|
||||
{{ form|crispy }}
|
||||
{% if hide_cancel is not True %}
|
||||
<button
|
||||
type="button"
|
||||
class="button is-light modal-close-button">
|
||||
Cancel
|
||||
</button>
|
||||
{% endif %}
|
||||
<button type="submit" class="button modal-close-button">Submit</button>
|
||||
</form>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
@ -0,0 +1,45 @@
|
||||
{% include 'partials/notify.html' %}
|
||||
{% if page_title is not None %}
|
||||
<h1 class="title is-4">{{ page_title }}</h1>
|
||||
{% endif %}
|
||||
{% if page_subtitle is not None %}
|
||||
<h1 class="subtitle">{{ page_subtitle }}</h1>
|
||||
{% endif %}
|
||||
<div class="buttons">
|
||||
|
||||
{% if submit_url is not None %}
|
||||
<button
|
||||
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
|
||||
hx-get="{{ submit_url }}"
|
||||
hx-trigger="click"
|
||||
hx-target="#modals-here"
|
||||
class="button">
|
||||
<span class="icon-text">
|
||||
<span class="icon">
|
||||
<i class="fa-solid fa-plus"></i>
|
||||
</span>
|
||||
<span>{{ title_singular }}</span>
|
||||
</span>
|
||||
</button>
|
||||
{% endif %}
|
||||
{% if delete_all_url is not None %}
|
||||
<button
|
||||
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
|
||||
hx-delete="{{ delete_all_url }}"
|
||||
hx-trigger="click"
|
||||
hx-target="#modals-here"
|
||||
hx-swap="innerHTML"
|
||||
hx-confirm="Are you sure you wish to delete all {{ context_object_name }}?"
|
||||
class="button">
|
||||
<span class="icon-text">
|
||||
<span class="icon">
|
||||
<i class="fa-solid fa-xmark"></i>
|
||||
</span>
|
||||
<span>Delete all {{ context_object_name }} </span>
|
||||
</span>
|
||||
</button>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
{% include detail_template %}
|
||||
|
@ -0,0 +1,45 @@
|
||||
{% include 'partials/notify.html' %}
|
||||
{% if page_title is not None %}
|
||||
<h1 class="title is-4">{{ page_title }}</h1>
|
||||
{% endif %}
|
||||
{% if page_subtitle is not None %}
|
||||
<h1 class="subtitle">{{ page_subtitle }}</h1>
|
||||
{% endif %}
|
||||
<div class="buttons">
|
||||
|
||||
{% if submit_url is not None %}
|
||||
<button
|
||||
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
|
||||
hx-get="{{ submit_url }}"
|
||||
hx-trigger="click"
|
||||
hx-target="#modals-here"
|
||||
class="button">
|
||||
<span class="icon-text">
|
||||
<span class="icon">
|
||||
<i class="fa-solid fa-plus"></i>
|
||||
</span>
|
||||
<span>{{ title_singular }}</span>
|
||||
</span>
|
||||
</button>
|
||||
{% endif %}
|
||||
{% if delete_all_url is not None %}
|
||||
<button
|
||||
hx-headers='{"X-CSRFToken": "{{ csrf_token }}"}'
|
||||
hx-delete="{{ delete_all_url }}"
|
||||
hx-trigger="click"
|
||||
hx-target="#modals-here"
|
||||
hx-swap="innerHTML"
|
||||
hx-confirm="Are you sure you wish to delete all {{ context_object_name }}?"
|
||||
class="button">
|
||||
<span class="icon-text">
|
||||
<span class="icon">
|
||||
<i class="fa-solid fa-xmark"></i>
|
||||
</span>
|
||||
<span>Delete all {{ context_object_name }} </span>
|
||||
</span>
|
||||
</button>
|
||||
{% endif %}
|
||||
</div>
|
||||
|
||||
{% include list_template %}
|
||||
|
@ -0,0 +1,57 @@
|
||||
from django.contrib.auth.mixins import LoginRequiredMixin
|
||||
from core.forms import NotificationRuleForm, NotificationSettingsForm
|
||||
from core.models import NotificationRule, NotificationSettings
|
||||
|
||||
from core.views.helpers import ObjectCreate, ObjectList, ObjectUpdate, ObjectDelete
|
||||
|
||||
# Notifications - we create a new notification settings object if there isn't one
|
||||
# Hence, there is only an update view, not a create view.
|
||||
class NotificationsUpdate(LoginRequiredMixin, ObjectUpdate):
|
||||
model = NotificationSettings
|
||||
form_class = NotificationSettingsForm
|
||||
|
||||
page_title = "Update your notification settings"
|
||||
page_subtitle = (
|
||||
"At least the topic must be set if you want to receive notifications."
|
||||
)
|
||||
|
||||
submit_url_name = "notifications_update"
|
||||
submit_url_args = ["type"]
|
||||
|
||||
pk_required = False
|
||||
|
||||
hide_cancel = True
|
||||
|
||||
def get_object(self, **kwargs):
|
||||
notification_settings, _ = NotificationSettings.objects.get_or_create(
|
||||
user=self.request.user
|
||||
)
|
||||
return notification_settings
|
||||
|
||||
class RuleList(LoginRequiredMixin, ObjectList):
|
||||
list_template = "partials/rule-list.html"
|
||||
model = NotificationRule
|
||||
page_title = "List of notification rules."
|
||||
|
||||
list_url_name = "rules"
|
||||
list_url_args = ["type"]
|
||||
|
||||
submit_url_name = "rule_create"
|
||||
|
||||
|
||||
class RuleCreate(LoginRequiredMixin, ObjectCreate):
|
||||
model = NotificationRule
|
||||
form_class = NotificationRuleForm
|
||||
|
||||
submit_url_name = "rule_create"
|
||||
|
||||
|
||||
class RuleUpdate(LoginRequiredMixin, ObjectUpdate):
|
||||
model = NotificationRule
|
||||
form_class = NotificationRuleForm
|
||||
|
||||
submit_url_name = "rule_update"
|
||||
|
||||
|
||||
class RuleDelete(LoginRequiredMixin, ObjectDelete):
|
||||
model = NotificationRule
|
Loading…
Reference in New Issue