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.conf.urls.static import static
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
from .views import home, send_push from .views import home
from django.views.generic import TemplateView from django.views.generic import TemplateView
urlpatterns = [ urlpatterns = [
url(r"^", include("djangoldp.urls")), url(r"^", include("djangoldp.urls")),
url(r"^admin/", admin.site.urls), url(r"^admin/", admin.site.urls),
url(r"^", include("djangoldp_webpushnotification.urls")),
path('', home), path('', home),
path('send_push', send_push), path(
path('webpush/', include('webpush.urls')), "sw.js",
path('sw.js', TemplateView.as_view(template_name='sw.js', content_type='application/x-javascript')), TemplateView.as_view(
template_name="sw.js", content_type="application/x-javascript"
),
),
] ]
if settings.DEBUG: 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.http.response import JsonResponse, HttpResponse
from django.views.decorators.http import require_GET, require_POST from django.views.decorators.http import require_GET, require_POST
from django.shortcuts import render, get_object_or_404 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 webpush import send_user_notification
from django.conf import settings from django.conf import settings
import json import json
@require_GET @require_GET
def home(request): def home(request):
webpush_settings = getattr(settings, 'WEBPUSH_SETTINGS', {}) webpush_settings = getattr(settings, 'WEBPUSH_SETTINGS', {})
vapid_key = webpush_settings.get('VAPID_PUBLIC_KEY') vapid_key = webpush_settings.get('VAPID_PUBLIC_KEY')
user = request.user user = request.user
return render(request, 'home.html', {user: user, 'vapid_key': vapid_key}) 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"})