Point to URLs in ..webpushnotification

This commit is contained in:
3wc 2021-04-08 15:35:03 +02:00
parent b61470c26e
commit d48fc90b9b
2 changed files with 9 additions and 25 deletions

View File

@ -3,16 +3,20 @@ from django.conf.urls import include, url
from django.conf.urls.static import static
from django.contrib import admin
from django.urls import path, include
from .views import home, send_push
from .views import home
from django.views.generic import TemplateView
urlpatterns = [
url(r"^", include("djangoldp.urls")),
url(r"^admin/", admin.site.urls),
url(r"^", include("djangoldp_webpushnotification.urls")),
path('', home),
path('send_push', send_push),
path('webpush/', include('webpush.urls')),
path('sw.js', TemplateView.as_view(template_name='sw.js', content_type='application/x-javascript')),
path(
"sw.js",
TemplateView.as_view(
template_name="sw.js", content_type="application/x-javascript"
),
),
]
if settings.DEBUG:

View File

@ -3,34 +3,14 @@ from django.views.decorators.http import require_GET
from django.http.response import JsonResponse, HttpResponse
from django.views.decorators.http import require_GET, require_POST
from django.shortcuts import render, get_object_or_404
from djangoldp_account.models import LDPUser
from django.views.decorators.csrf import csrf_exempt
from webpush import send_user_notification
from django.conf import settings
import json
@require_GET
def home(request):
webpush_settings = getattr(settings, 'WEBPUSH_SETTINGS', {})
vapid_key = webpush_settings.get('VAPID_PUBLIC_KEY')
user = request.user
return render(request, 'home.html', {user: user, 'vapid_key': vapid_key})
@require_POST
@csrf_exempt
def send_push(request):
try:
body = request.body
data = json.loads(body)
if 'head' not in data or 'body' not in data or 'id' not in data:
return JsonResponse(status=400, data={"message": "Invalid data format"})
user_id = data['id']
user = get_object_or_404(LDPUser, pk=user_id)
payload = {'head': data['head'], 'body': data['body']}
send_user_notification(user=user, payload=payload, ttl=1000)
return JsonResponse(status=200, data={"message": "Web push successful"})
except TypeError:
return JsonResponse(status=500, data={"message": "An error occurred"})