first commit
This commit is contained in:
@@ -0,0 +1,3 @@
|
||||
from django.contrib import admin
|
||||
from .models import NotificationPreference
|
||||
admin.site.register(NotificationPreference)
|
||||
@@ -0,0 +1,28 @@
|
||||
# Generated by Django 6.1.1 on 2026-10-04 14:20
|
||||
|
||||
import django.db.models.deletion
|
||||
from django.conf import settings
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
initial = True
|
||||
|
||||
dependencies = [
|
||||
migrations.swappable_dependency(settings.AUTH_USER_MODEL),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.CreateModel(
|
||||
name='NotificationPreference',
|
||||
fields=[
|
||||
('id', models.BigAutoField(auto_created=True, primary_key=True, serialize=False, verbose_name='ID')),
|
||||
('ntfy_enabled', models.BooleanField(default=False)),
|
||||
('ntfy_url', models.URLField(blank=True)),
|
||||
('notify_on_sensor_submission', models.BooleanField(default=True)),
|
||||
('notify_on_manual_submission', models.BooleanField(default=False)),
|
||||
('user', models.OneToOneField(on_delete=django.db.models.deletion.CASCADE, to=settings.AUTH_USER_MODEL)),
|
||||
],
|
||||
),
|
||||
]
|
||||
@@ -0,0 +1,8 @@
|
||||
from django.conf import settings
|
||||
from django.db import models
|
||||
class NotificationPreference(models.Model):
|
||||
user=models.OneToOneField(settings.AUTH_USER_MODEL,on_delete=models.CASCADE)
|
||||
ntfy_enabled=models.BooleanField(default=False)
|
||||
ntfy_url=models.URLField(blank=True)
|
||||
notify_on_sensor_submission=models.BooleanField(default=True)
|
||||
notify_on_manual_submission=models.BooleanField(default=False)
|
||||
@@ -0,0 +1,13 @@
|
||||
import requests
|
||||
from .models import NotificationPreference
|
||||
def send_ntfy(url,message):
|
||||
try:
|
||||
response=requests.post(url,data=message.encode(),timeout=5);response.raise_for_status();return True
|
||||
except requests.RequestException: return False
|
||||
def notify_event(event):
|
||||
if event.source=='sensor' and event.sensor: users=[event.sensor.owner_id]
|
||||
elif event.source=='manual': users=list(NotificationPreference.objects.filter(ntfy_enabled=True,notify_on_manual_submission=True).values_list('user_id',flat=True))
|
||||
else: users=[]
|
||||
for pref in NotificationPreference.objects.filter(user_id__in=users,ntfy_enabled=True).exclude(ntfy_url=''):
|
||||
if event.source=='sensor' and not pref.notify_on_sensor_submission: continue
|
||||
send_ntfy(pref.ntfy_url,f'BoomCast: {event.get_source_display()} #{event.pk} awaits review')
|
||||
@@ -0,0 +1 @@
|
||||
{% extends 'base.html' %}{% block content %}<section class="page-head"><h1>Notifications</h1>{% if error %}<p role="alert">{{ error }}</p>{% endif %}</section><nav class="subnav"><a href="/settings/">General</a><a href="/settings/notifications/">Notifications</a><a href="/settings/sensors/">Sensors</a></nav><form class="card form" method="post" hx-post="/settings/notifications/" hx-target="#content" hx-select="#content" hx-swap="outerHTML">{% csrf_token %}<label><input type="checkbox" name="ntfy_enabled" {% if preference.ntfy_enabled %}checked{% endif %}> Enable ntfy</label><label>ntfy URL <input type="url" name="ntfy_url" value="{{ preference.ntfy_url }}"></label><label><input type="checkbox" name="notify_on_sensor_submission" {% if preference.notify_on_sensor_submission %}checked{% endif %}> Sensor submissions</label><label><input type="checkbox" name="notify_on_manual_submission" {% if preference.notify_on_manual_submission %}checked{% endif %}> Manual reports</label><button class="button">Save</button></form><form method="post" action="/settings/notifications/test/" hx-post="/settings/notifications/test/" hx-target="#test-result">{% csrf_token %}<button>Send test notification</button><span id="test-result" aria-live="polite"></span></form>{% endblock %}
|
||||
@@ -0,0 +1,22 @@
|
||||
from django.contrib.auth.decorators import login_required
|
||||
from django.shortcuts import render,redirect
|
||||
from django.http import HttpResponse
|
||||
from django.core.exceptions import ValidationError
|
||||
from .models import NotificationPreference
|
||||
from .services import send_ntfy
|
||||
@login_required
|
||||
def settings_view(request):
|
||||
pref,_=NotificationPreference.objects.get_or_create(user=request.user)
|
||||
if request.method=='POST':
|
||||
pref.ntfy_enabled='ntfy_enabled' in request.POST;pref.ntfy_url=request.POST.get('ntfy_url','').strip();pref.notify_on_sensor_submission='notify_on_sensor_submission' in request.POST;pref.notify_on_manual_submission='notify_on_manual_submission' in request.POST
|
||||
try: pref.full_clean()
|
||||
except ValidationError as exc: return render(request,'notifications/settings.html',{'preference':pref,'error':'; '.join(exc.messages)},status=400 if not request.headers.get('HX-Request') else 200)
|
||||
pref.save()
|
||||
if not request.headers.get('HX-Request'): return redirect('notification_settings')
|
||||
return render(request,'notifications/settings.html',{'preference':pref})
|
||||
@login_required
|
||||
def test_notification(request):
|
||||
if request.method!='POST': return HttpResponse(status=405)
|
||||
pref,_=NotificationPreference.objects.get_or_create(user=request.user)
|
||||
ok=bool(pref.ntfy_url) and send_ntfy(pref.ntfy_url,'BoomCast test notification')
|
||||
return HttpResponse('Test sent.' if ok else 'Could not send test.')
|
||||
Reference in New Issue
Block a user