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) | |||||
}) |
'django.contrib.messages', | 'django.contrib.messages', | ||||
'django.contrib.staticfiles', | 'django.contrib.staticfiles', | ||||
'rest_framework', | 'rest_framework', | ||||
'channels', | |||||
'django_eventstream', | |||||
] | ] | ||||
MIDDLEWARE = [ | MIDDLEWARE = [ | ||||
'django.contrib.auth.middleware.AuthenticationMiddleware', | 'django.contrib.auth.middleware.AuthenticationMiddleware', | ||||
'django.contrib.messages.middleware.MessageMiddleware', | 'django.contrib.messages.middleware.MessageMiddleware', | ||||
'django.middleware.clickjacking.XFrameOptionsMiddleware', | 'django.middleware.clickjacking.XFrameOptionsMiddleware', | ||||
'django_grip.GripMiddleware', | |||||
'django.middleware.security.SecurityMiddleware', | |||||
] | ] | ||||
ROOT_URLCONF = 'PR.urls' | ROOT_URLCONF = 'PR.urls' | ||||
] | ] | ||||
WSGI_APPLICATION = 'PR.wsgi.application' | WSGI_APPLICATION = 'PR.wsgi.application' | ||||
ASGI_APPLICATION = 'PR.routing.application' | |||||
# Database | # Database | ||||
# https://docs.djangoproject.com/en/2.2/ref/settings/#databases | # https://docs.djangoproject.com/en/2.2/ref/settings/#databases |
from polls import views | from polls import views | ||||
urlpatterns = [ | urlpatterns = [ | ||||
#path('', views.index , name='index'), | |||||
path('', views.index , name='index'), | |||||
path('new', views.new, name='new'), | path('new', views.new, name='new'), | ||||
#path('index',views.index, name='index'), | #path('index',views.index, name='index'), | ||||
path('notice',views.index, name = 'index'), | path('notice',views.index, name = 'index'), |
from polls.serializers import NoticeSerializer | from polls.serializers import NoticeSerializer | ||||
from rest_framework.parsers import JSONParser | from rest_framework.parsers import JSONParser | ||||
#XXXX | #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. | # Create your views here. | ||||
logger = None | logger = None | ||||
pub_start= timezone.now(), | pub_start= timezone.now(), | ||||
pub_end=form.cleaned_data['end']) | pub_end=form.cleaned_data['end']) | ||||
newNotice.save() | newNotice.save() | ||||
#Absetzen eines Events | |||||
send_event('notice', 'message', newNotice.id) | |||||
return redirect('index') | return redirect('index') | ||||
context = {'form': NoticeForm()} | context = {'form': NoticeForm()} | ||||
return render(request, 'polls/edit.html', context) | return render(request, 'polls/edit.html', context) | ||||
elif request.method == 'DELETE': | elif request.method == 'DELETE': | ||||
notice.delete() | notice.delete() | ||||
return HttpResponse(status=204) | return HttpResponse(status=204) | ||||
<!DOCTYPE html> | <!DOCTYPE html> | ||||
<html lang="de"> | <html lang="de"> | ||||
<head> | <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="/media/css/style.css" />#} | ||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/3.4.0/css/bootstrap.min.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"> | <meta name="viewport" content="width=device-width, initial-scale=1"> |
{% extends "base.html" %} | {% extends "base.html" %} | ||||
{% block content %} | {% 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> | <h2>Seite Infos</h2> | ||||