This commit is contained in:
Laura 2019-12-10 15:03:12 +01:00
parent b9dfad7a72
commit 85576f05cc
6 changed files with 49 additions and 2 deletions

18
PR/routing.py Normal file
View File

@ -0,0 +1,18 @@
from channels.routing import ProtocolTypeRouter, URLRouter
from django.urls import re_path
from channels.routing import URLRouter
from channels.http import AsgiHandler
from channels.auth import AuthMiddlewareStack
import django_eventstream
urlpatterns = [
#idz lt. URL ein Eventstream auszuliefern
re_path(r'^events/',
AuthMiddlewareStack(URLRouter(django_eventstream.routing.urlpatterns)),
{'channels': ['notice']}),
#bei allen anderen Anfragen werden die Standart-Router verwendet
re_path(r'', AsgiHandler),
]
application = ProtocolTypeRouter({
'http': URLRouter(urlpatterns)
})

View File

@ -39,6 +39,8 @@ INSTALLED_APPS = [
'django.contrib.messages',
'django.contrib.staticfiles',
'rest_framework',
'channels',
'django_eventstream',
]
MIDDLEWARE = [
@ -49,6 +51,9 @@ MIDDLEWARE = [
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'django.middleware.clickjacking.XFrameOptionsMiddleware',
'django_grip.GripMiddleware',
'django.middleware.security.SecurityMiddleware',
]
ROOT_URLCONF = 'PR.urls'
@ -71,7 +76,7 @@ TEMPLATES = [
]
WSGI_APPLICATION = 'PR.wsgi.application'
ASGI_APPLICATION = 'PR.routing.application'
# Database
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases

View File

@ -19,7 +19,7 @@ from django.urls import path
from polls import views
urlpatterns = [
#path('', views.index , name='index'),
path('', views.index , name='index'),
path('new', views.new, name='new'),
#path('index',views.index, name='index'),
path('notice',views.index, name = 'index'),

View File

@ -11,7 +11,20 @@ from polls.models import Notice
from polls.serializers import NoticeSerializer
from rest_framework.parsers import JSONParser
#XXXX
from django.contrib.admin.views.decorators import staff_member_required
from django.contrib.auth.decorators import login_required
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect
from django.utils import timezone
from django.views.decorators.csrf import csrf_exempt
from django_eventstream import send_event
from rest_framework.parsers import JSONParser
from polls.forms import NoticeForm
from polls.models import Notice
# Create your views here.
from polls.serializers import NoticeSerializer
# Create your views here.
logger = None
@ -48,6 +61,8 @@ def new(request):
pub_start= timezone.now(),
pub_end=form.cleaned_data['end'])
newNotice.save()
#Absetzen eines Events
send_event('notice', 'message', newNotice.id)
return redirect('index')
context = {'form': NoticeForm()}
return render(request, 'polls/edit.html', context)
@ -103,3 +118,4 @@ def notice_detail(request, id):
elif request.method == 'DELETE':
notice.delete()
return HttpResponse(status=204)

View File

@ -4,6 +4,10 @@
<!DOCTYPE html>
<html lang="de">
<head>
{% load static %}
<script src="{% static 'django_eventstream/eventsource.min.js' %}"></script>
<script src="{% static 'django_eventstream/reconnecting-eventsource.js' %}"></script>
{# <link rel="stylesheet" href="/media/css/style.css" />#}
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.css">
<meta name="viewport" content="width=device-width, initial-scale=1">

View File

@ -1,6 +1,10 @@
{% extends "base.html" %}
{% block content %}
<script>
var es = new ReconnectingEventSource('/events/');
es.addEventListener('message', function (e) {console.log(e.data);location.reload();}, false);
</script>
<h2>Seite Infos</h2>