Browse Source

Pra. 9

master
Amad Colovic 4 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

'django.contrib.staticfiles', 'django.contrib.staticfiles',
'posts.apps.PostsConfig', 'posts.apps.PostsConfig',
'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 = 'news.urls' ROOT_URLCONF = 'news.urls'
# 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
- 0
posts/routing.py View File

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

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






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
- 0
requirements.txt View File

channels
django-eventstream



+ 10
- 0
templates/base.html View File

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

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



{% block content %} {% 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