This commit is contained in:
Amad Colovic 2019-12-10 15:21:44 +01:00
parent 7bb3690afe
commit 5a7e242875
6 changed files with 61 additions and 12 deletions

View File

@ -39,7 +39,8 @@ INSTALLED_APPS = [
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'posts.apps.PostsConfig', 'posts.apps.PostsConfig',
'rest_framework', 'rest_framework',
'channels',
'django_eventstream',
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@ -50,6 +51,8 @@ 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 = 'news.urls' ROOT_URLCONF = 'news.urls'
@ -122,3 +125,6 @@ USE_TZ = True
# https://docs.djangoproject.com/en/2.2/howto/static-files/ # https://docs.djangoproject.com/en/2.2/howto/static-files/
STATIC_URL = '/static/' STATIC_URL = '/static/'
WSGI_APPLICATION = 'posts.wsgi.application'
ASGI_APPLICATION = 'posts.routing.application'

23
posts/routing.py Normal file
View File

@ -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)
})

View File

@ -7,6 +7,7 @@ from .models import Notice
from .serializers import NoticeSerializer from .serializers import NoticeSerializer
from rest_framework.parsers import JSONParser from rest_framework.parsers import JSONParser
from django.views.decorators.csrf import csrf_exempt 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": if request.method == "POST":
form = NoticeForm(request.POST) form = NoticeForm(request.POST)
if form.is_valid(): 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'], notice_text=form.cleaned_data['text'],
pub_start=form.cleaned_data['start'], pub_start=form.cleaned_data['start'],
pub_end=form.cleaned_data['end']) pub_end=form.cleaned_data['end'])
newNotice.save() new_notice.save()
send_event('notice', 'message', new_notice.id)
return redirect('index') return redirect('index')
context = {'form': NoticeForm()} context = {'form': NoticeForm()}
return render(request, 'posts/edit.html', context) return render(request, 'posts/edit.html', context)

4
requirements.txt Normal file
View File

@ -0,0 +1,4 @@
channels
django-eventstream

View File

@ -14,6 +14,16 @@
<script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js" <script src="https://stackpath.bootstrapcdn.com/bootstrap/4.3.1/js/bootstrap.min.js"
integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM" integrity="sha384-JjSmVgyd0p3pXB1rRibZUAYoIIy6OrQ6VrjIEaFf/nJGzIxFDsf4x0xIM+B07jRM"
crossorigin="anonymous"></script> crossorigin="anonymous"></script>
{% load static %}
<script src="{% static 'django_eventstream/eventsource.min.js' %}"></script>
<script src="{% static 'django_eventstream/reconnecting-eventsource.js' %}"></script>
</head> </head>
<body> <body>
{% block navigation %} Platzhalter {% endblock %} {% block navigation %} Platzhalter {% endblock %}

View File

@ -6,24 +6,28 @@
{% block content %} {% block content %}
<script>
var es = new ReconnectingEventSource('/events/');
es.addEventListener('message', function (e) {console.log(e.data);location.reload();}, false);
</script>
<div class="container"> <div class="container">
{% for notice in notices %} {% for notice in notices %}
<h3>{{ notice.notice_title }}</h3> <h3>{{ notice.notice_title }}</h3>
<p>{{ notice.notice_text }}</p> <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 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p> <p><a href="{% url 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p>
<p><a href="{% url 'delete' deleteId=notice.id %}" class="btn btn-danger">
Meldung loeschen</a></p>
{% endfor %}
</div> </div>