Files
2026-10-04 17:09:22 +02:00

23 lines
1.4 KiB
Python

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.')