Load django-webpush VAPID keys from DB

This commit is contained in:
3wc 2021-04-15 18:57:22 +02:00
parent 0fdbd1f3b2
commit 9caf97966c

View File

@ -5,9 +5,12 @@ from django.http.response import HttpResponse, JsonResponse
from django.shortcuts import get_object_or_404, render from django.shortcuts import get_object_or_404, render
from django.views.decorators.csrf import csrf_exempt from django.views.decorators.csrf import csrf_exempt
from django.views.decorators.http import require_GET, require_POST from django.views.decorators.http import require_GET, require_POST
from djangoldp_account.models import LDPUser
from webpush import send_user_notification from webpush import send_user_notification
from djangoldp_account.models import LDPUser
from djangoldp_webpushnotification.models import VAPIDKeyset
@require_POST @require_POST
@csrf_exempt @csrf_exempt
@ -22,6 +25,12 @@ def send_push(request):
user_id = data["id"] user_id = data["id"]
user = get_object_or_404(LDPUser, pk=user_id) user = get_object_or_404(LDPUser, pk=user_id)
payload = {"head": data["head"], "body": data["body"]} payload = {"head": data["head"], "body": data["body"]}
vapid_key = VAPIDKeyset.objects.first()
settings.WEBPUSH_SETTINGS = {
'VAPID_PUBLIC_KEY': vapid_key.public_key,
'VAPID_PRIVATE_KEY': vapid_key.private_key.tobytes().decode(),
'VAPID_ADMIN_EMAIL': 'foo@bar.com',
}
send_user_notification(user=user, payload=payload, ttl=1000) send_user_notification(user=user, payload=payload, ttl=1000)
return JsonResponse(status=200, data={"message": "Web push successful"}) return JsonResponse(status=200, data={"message": "Web push successful"})