Browse Source

Pra. 9

master
Amad Colovic 5 years ago
parent
commit
5a7e242875
6 changed files with 61 additions and 12 deletions
  1. 7
    1
      news/settings.py
  2. 23
    0
      posts/routing.py
  3. 4
    2
      posts/views.py
  4. 4
    0
      requirements.txt
  5. 10
    0
      templates/base.html
  6. 13
    9
      templates/posts/index.html

+ 7
- 1
news/settings.py View File

@@ -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'

+ 23
- 0
posts/routing.py 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)

})

+ 4
- 2
posts/views.py View File

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

+ 4
- 0
requirements.txt View File

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



+ 10
- 0
templates/base.html View File

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

+ 13
- 9
templates/posts/index.html View File

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




Loading…
Cancel
Save