50 lines
1.5 KiB
Python
50 lines
1.5 KiB
Python
from django.contrib.auth.decorators import login_required
|
|
from django.http import JsonResponse
|
|
from django.shortcuts import render
|
|
from django.views.decorators.http import require_GET, require_POST
|
|
|
|
from notify.models import DeliveryChannel, NotificationLevel
|
|
from notify.services import create_notification
|
|
|
|
|
|
@login_required
|
|
@require_GET
|
|
def inbox(request):
|
|
return render(request, "demo/inbox.html")
|
|
|
|
|
|
@login_required
|
|
@require_POST
|
|
def create_notification_view(request):
|
|
verb = (request.POST.get("verb") or "").strip()
|
|
description = (request.POST.get("description") or "").strip() or None
|
|
level = (request.POST.get("level") or NotificationLevel.INFO).strip()
|
|
url = (request.POST.get("url") or "").strip() or None
|
|
|
|
if level not in NotificationLevel.values:
|
|
return JsonResponse({"ok": False, "error": "Unknown level"}, status=400)
|
|
|
|
attachments = [
|
|
uploaded
|
|
for uploaded in request.FILES.getlist("attachments")
|
|
if getattr(uploaded, "name", "")
|
|
]
|
|
try:
|
|
create_notification(
|
|
verb=verb,
|
|
description=description,
|
|
level=level,
|
|
url=url,
|
|
attachments=attachments,
|
|
recipients=[
|
|
{
|
|
"user": request.user,
|
|
"channels": [{"channel": DeliveryChannel.BROWSER}],
|
|
}
|
|
],
|
|
)
|
|
except ValueError as exc:
|
|
return JsonResponse({"ok": False, "error": str(exc)}, status=400)
|
|
|
|
return JsonResponse({"ok": True})
|