@@ -39,7 +39,8 @@ INSTALLED_APPS = [ | |||
'django.contrib.staticfiles', | |||
'posts.apps.PostsConfig', | |||
'rest_framework', | |||
'channels', | |||
'django_eventstream', | |||
] | |||
MIDDLEWARE = [ | |||
@@ -50,6 +51,8 @@ MIDDLEWARE = [ | |||
'django.contrib.auth.middleware.AuthenticationMiddleware', | |||
'django.contrib.messages.middleware.MessageMiddleware', | |||
'django.middleware.clickjacking.XFrameOptionsMiddleware', | |||
'django_grip.GripMiddleware', | |||
'django.middleware.security.SecurityMiddleware', | |||
] | |||
ROOT_URLCONF = 'news.urls' | |||
@@ -122,3 +125,6 @@ USE_TZ = True | |||
# https://docs.djangoproject.com/en/2.2/howto/static-files/ | |||
STATIC_URL = '/static/' | |||
WSGI_APPLICATION = 'posts.wsgi.application' | |||
ASGI_APPLICATION = 'posts.routing.application' |
@@ -0,0 +1,23 @@ | |||
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 | |||
urpatterns = [ | |||
re_path(r'events/', | |||
AuthMiddlewareStack(URLRouter(django_eventstream.routing.urlpatterns)), | |||
{'channels': ['notice']}), | |||
re_path(r'', AsgiHandler), | |||
] | |||
application = ProtocolTypeRouter({ | |||
'http' : URLRouter(urpatterns) | |||
}) |
@@ -7,6 +7,7 @@ from .models import Notice | |||
from .serializers import NoticeSerializer | |||
from rest_framework.parsers import JSONParser | |||
from django.views.decorators.csrf import csrf_exempt | |||
from django_eventstream import send_event | |||
@@ -19,11 +20,12 @@ def new(request): | |||
if request.method == "POST": | |||
form = NoticeForm(request.POST) | |||
if form.is_valid(): | |||
newNotice = Notice(notice_title=form.cleaned_data['title'], | |||
new_notice = Notice(notice_title=form.cleaned_data['title'], | |||
notice_text=form.cleaned_data['text'], | |||
pub_start=form.cleaned_data['start'], | |||
pub_end=form.cleaned_data['end']) | |||
newNotice.save() | |||
new_notice.save() | |||
send_event('notice', 'message', new_notice.id) | |||
return redirect('index') | |||
context = {'form': NoticeForm()} | |||
return render(request, 'posts/edit.html', context) |
@@ -0,0 +1,4 @@ | |||
channels | |||
django-eventstream | |||
@@ -14,6 +14,16 @@ | |||
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" | |||
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" | |||
crossorigin="anonymous"></script> | |||
{% load static %} | |||
<script src="{% static 'django_eventstream/eventsource.min.js' %}"></script> | |||
<script src="{% static 'django_eventstream/reconnecting-eventsource.js' %}"></script> | |||
</head> | |||
<body> | |||
{% block navigation %} Platzhalter {% endblock %} |
@@ -6,21 +6,25 @@ | |||
{% block content %} | |||
<div class="container"> | |||
<script> | |||
var es = new ReconnectingEventSource('/events/'); | |||
es.addEventListener('message', function (e) {console.log(e.data);location.reload();}, false); | |||
{% for notice in notices %} | |||
<h3>{{ notice.notice_title }}</h3> | |||
<p>{{ notice.notice_text }}</p> | |||
<p><a href="{% url 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p> | |||
</script> | |||
<div class="container"> | |||
{% for notice in notices %} | |||
<h3>{{ notice.notice_title }}</h3> | |||
<p>{{ notice.notice_text }}</p> | |||
<p> | |||
<a href="{% url 'delete' deleteId=notice.id %}" class="btn btn-danger"> | |||
Meldung loeschen</a></p> | |||
{% endfor %} | |||
<p><a href="{% url 'delete' deleteId=notice.id %}" class="btn btn-danger"> | |||
Meldung loeschen</a></p> | |||
{% endfor %} | |||
<p><a href="{% url 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p> | |||