diff --git a/news/settings.py b/news/settings.py index ad5128f..57eef51 100755 --- a/news/settings.py +++ b/news/settings.py @@ -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' \ No newline at end of file diff --git a/posts/routing.py b/posts/routing.py new file mode 100644 index 0000000..d1d8792 --- /dev/null +++ b/posts/routing.py @@ -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) + +}) \ No newline at end of file diff --git a/posts/views.py b/posts/views.py index 7e7b143..21986f1 100755 --- a/posts/views.py +++ b/posts/views.py @@ -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) diff --git a/requirements.txt b/requirements.txt new file mode 100644 index 0000000..9b773a7 --- /dev/null +++ b/requirements.txt @@ -0,0 +1,4 @@ +channels +django-eventstream + + diff --git a/templates/base.html b/templates/base.html index 9c1f600..2594d6b 100755 --- a/templates/base.html +++ b/templates/base.html @@ -14,6 +14,16 @@ + +{% load static %} + + + + + + + + {% block navigation %} Platzhalter {% endblock %} diff --git a/templates/posts/index.html b/templates/posts/index.html index 74cd076..d2230ba 100755 --- a/templates/posts/index.html +++ b/templates/posts/index.html @@ -6,24 +6,28 @@ {% block content %} + +
{% for notice in notices %}

{{ notice.notice_title }}

-

{{ notice.notice_text }}

+

{{ notice.notice_text }}

+

+ + Meldung loeschen

+ {% endfor %} +

Neue Nachricht

- -

- Meldung loeschen

- {% endfor %} - - - -