Compare commits
3
Commits
a67575f713
...
master
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
5a7e242875 | ||
|
|
7bb3690afe | ||
|
|
5376f0e654 |
+9
-1
@@ -38,6 +38,9 @@ INSTALLED_APPS = [
|
||||
'django.contrib.messages',
|
||||
'django.contrib.staticfiles',
|
||||
'posts.apps.PostsConfig',
|
||||
'rest_framework',
|
||||
'channels',
|
||||
'django_eventstream',
|
||||
]
|
||||
|
||||
MIDDLEWARE = [
|
||||
@@ -48,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'
|
||||
@@ -105,7 +110,7 @@ AUTH_PASSWORD_VALIDATORS = [
|
||||
# Internationalization
|
||||
# https://docs.djangoproject.com/en/2.2/topics/i18n/
|
||||
|
||||
LANGUAGE_CODE = 'en-us'
|
||||
LANGUAGE_CODE = 'de-de'
|
||||
|
||||
TIME_ZONE = 'UTC'
|
||||
|
||||
@@ -120,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'
|
||||
@@ -1,9 +1,15 @@
|
||||
from django.contrib import admin
|
||||
from django.urls import path, include
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
urlpatterns = [
|
||||
path('posts/', include('posts.urls')),
|
||||
path('admin/', admin.site.urls),
|
||||
|
||||
|
||||
]
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,13 @@
|
||||
from django import forms
|
||||
import datetime
|
||||
|
||||
class NoticeForm(forms.Form):
|
||||
date_formats = ['%d.%m.%Y', '%d.%m.%y']
|
||||
title = forms.CharField(label='Titel', max_length=80)
|
||||
text = forms.CharField(label='Text', max_length=400)
|
||||
start = forms.DateField(label='Von',
|
||||
input_formats=date_formats,
|
||||
initial=datetime.date.today)
|
||||
end = forms.DateField(label='Bis',
|
||||
input_formats=date_formats,
|
||||
initial=datetime.date.today)
|
||||
@@ -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)
|
||||
|
||||
})
|
||||
@@ -0,0 +1,9 @@
|
||||
from rest_framework import serializers
|
||||
from .models import Notice
|
||||
|
||||
class NoticeSerializer(serializers.ModelSerializer):
|
||||
class Meta:
|
||||
model = Notice
|
||||
fields = ('id', 'notice_title', 'notice_text', 'pub_start', 'pub_end')
|
||||
|
||||
|
||||
+5
-1
@@ -2,5 +2,9 @@ from django.urls import path
|
||||
from . import views
|
||||
|
||||
urlpatterns = [
|
||||
path('', views.index, name='index')
|
||||
path('', views.index, name='index'),
|
||||
path('new', views.new, name='new'),
|
||||
path('delete/<int:deleteId>', views.delete, name='delete'),
|
||||
path('notices/', views.notice_list),
|
||||
path('notices/<int:id>/', views.notice_detail),
|
||||
]
|
||||
+75
-4
@@ -1,13 +1,84 @@
|
||||
from django.shortcuts import render
|
||||
from .models import Notice
|
||||
from django.shortcuts import render, redirect
|
||||
|
||||
from django.utils import timezone
|
||||
from .forms import NoticeForm
|
||||
from django.http import JsonResponse, HttpResponse
|
||||
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
|
||||
|
||||
|
||||
|
||||
def index (request):
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
def new(request):
|
||||
if request.method == "POST":
|
||||
form = NoticeForm(request.POST)
|
||||
if form.is_valid():
|
||||
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'])
|
||||
new_notice.save()
|
||||
send_event('notice', 'message', new_notice.id)
|
||||
return redirect('index')
|
||||
context = {'form': NoticeForm()}
|
||||
return render(request, 'posts/edit.html', context)
|
||||
|
||||
|
||||
def index(request):
|
||||
notices = Notice.objects.all()
|
||||
notices = notices.filter(pub_start__lte=timezone.now())
|
||||
notices = notices.filter(pub_end__gte=timezone.now())
|
||||
context = { "notices" : notices }
|
||||
context = {"notices": notices}
|
||||
return render(request, 'posts/index.html', context)
|
||||
|
||||
def delete(request, deleteId=None):
|
||||
if deleteId !=None:
|
||||
delNotice = Notice.objects.get(id=deleteId)
|
||||
if delNotice != None:
|
||||
delNotice.delete()
|
||||
return redirect('index')
|
||||
|
||||
@csrf_exempt
|
||||
def notice_list(request):
|
||||
if request.method == 'GET':
|
||||
notices = Notice.objects.all()
|
||||
serializer = NoticeSerializer(notices, many=True)
|
||||
return JsonResponse(serializer.data, safe=False)
|
||||
|
||||
elif request.method == 'POST':
|
||||
data = JSONParser().parse(request)
|
||||
serializer = NoticeSerializer(data=data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return JsonResponse(serializer.data, status=201)
|
||||
return JsonResponse(serializer.errors, status=400)
|
||||
|
||||
@csrf_exempt
|
||||
def notice_detail(request, id):
|
||||
try:
|
||||
notice = Notice.objects.get(id=id)
|
||||
except Notice.DoesNotExist:
|
||||
return HttpResponse(status=404)
|
||||
|
||||
if request.method == 'GET':
|
||||
serializer = NoticeSerializer(notice)
|
||||
return JsonResponse(serializer.data)
|
||||
|
||||
elif request.method == 'PUT':
|
||||
data = JSONParser().parse(request)
|
||||
serializer = NoticeSerializer(notice, data=data)
|
||||
if serializer.is_valid():
|
||||
serializer.save()
|
||||
return JsonResponse(serializer.data)
|
||||
return JsonResponse(serializer.errors, status=400)
|
||||
|
||||
elif request.method == 'DELETE':
|
||||
notice.delete()
|
||||
return HttpResponse(status=204)
|
||||
@@ -0,0 +1,4 @@
|
||||
channels
|
||||
django-eventstream
|
||||
|
||||
|
||||
+13
-1
@@ -14,9 +14,21 @@
|
||||
<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 %}
|
||||
{% block content %} Platzhalter {% endblock %}
|
||||
</body>
|
||||
</html>
|
||||
</html>
|
||||
|
||||
|
||||
|
||||
@@ -0,0 +1,26 @@
|
||||
{% extends 'base.html' %}
|
||||
|
||||
|
||||
{% block title %}
|
||||
News
|
||||
{% endblock %}
|
||||
|
||||
|
||||
{% block content %}
|
||||
|
||||
<div class="container">
|
||||
|
||||
<h1>Neue Nachricht</h1>
|
||||
<form method="POST">
|
||||
{% csrf_token %}
|
||||
{{ form.as_p }}
|
||||
<button type="submit" class="save btn btn-default">Speichern</button>
|
||||
</form>
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
{% endblock %}
|
||||
|
||||
|
||||
|
||||
+22
-32
@@ -6,45 +6,35 @@
|
||||
|
||||
{% block content %}
|
||||
|
||||
<script>
|
||||
var es = new ReconnectingEventSource('/events/');
|
||||
es.addEventListener('message', function (e) {console.log(e.data);location.reload();}, false);
|
||||
|
||||
|
||||
</script>
|
||||
|
||||
<div class="container">
|
||||
|
||||
{% for notice in notices %}
|
||||
<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>
|
||||
|
||||
|
||||
|
||||
</div>
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
|
||||
{% endblock %}
|
||||
|
||||
|
||||
<!DOCTYPE html>
|
||||
<html>
|
||||
<head>
|
||||
<meta charset="utf-8">
|
||||
<title>Navbar Example</title>
|
||||
<link rel="stylesheet" href="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/css/bootstrap.min.css">
|
||||
</head>
|
||||
<body>
|
||||
|
||||
<h4>.navbar .navbar-expand-sm</h4>
|
||||
|
||||
<!-- A horizontal navbar that becomes vertical on small screens -->
|
||||
<nav class="navbar navbar-expand-sm bg-dark">
|
||||
<!-- Links -->
|
||||
<ul class="navbar-nav">
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="http://th-nuernberg.de">efi-homepage</a>
|
||||
</li>
|
||||
<li class="nav-item">
|
||||
<a class="nav-link" href="http://google.de">Google</a>
|
||||
</li>
|
||||
</ul>
|
||||
</nav>
|
||||
|
||||
<script src="https://code.jquery.com/jquery-3.2.1.slim.min.js"></script>
|
||||
<script src="https://cdnjs.cloudflare.com/ajax/libs/popper.js/1.12.9/umd/popper.min.js"></script>
|
||||
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/4.0.0/js/bootstrap.min.js"></script>
|
||||
</body>
|
||||
</html>
|
||||
<h1> Index der Polls-Applikation</h1>
|
||||
|
||||
Reference in New Issue
Block a user