Compare commits

...
3 Commits
Author SHA1 Message Date
colovicam63900 5a7e242875 Pra. 9 2019-12-10 15:21:44 +01:00
colovicam63900 7bb3690afe praktikum sieben bearbeitung 2019-12-03 15:17:37 +01:00
colovicam63900 5376f0e654 praktikum Sieben bearbeitung 2019-11-26 15:30:55 +01:00
15 changed files with 205 additions and 39 deletions
Generated Regular → Executable
View File
Generated Regular → Executable
View File
Generated Regular → Executable
View File
Generated Regular → Executable
View File
+9 -1
View File
@@ -38,6 +38,9 @@ INSTALLED_APPS = [
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'posts.apps.PostsConfig', 'posts.apps.PostsConfig',
'rest_framework',
'channels',
'django_eventstream',
] ]
MIDDLEWARE = [ MIDDLEWARE = [
@@ -48,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'
@@ -105,7 +110,7 @@ AUTH_PASSWORD_VALIDATORS = [
# Internationalization # Internationalization
# https://docs.djangoproject.com/en/2.2/topics/i18n/ # https://docs.djangoproject.com/en/2.2/topics/i18n/
LANGUAGE_CODE = 'en-us' LANGUAGE_CODE = 'de-de'
TIME_ZONE = 'UTC' TIME_ZONE = 'UTC'
@@ -120,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'
+6
View File
@@ -1,9 +1,15 @@
from django.contrib import admin from django.contrib import admin
from django.urls import path, include from django.urls import path, include
urlpatterns = [ urlpatterns = [
path('posts/', include('posts.urls')), path('posts/', include('posts.urls')),
path('admin/', admin.site.urls), path('admin/', admin.site.urls),
] ]
+13
View File
@@ -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)
+23
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)
})
+9
View File
@@ -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
View File
@@ -2,5 +2,9 @@ from django.urls import path
from . import views from . import views
urlpatterns = [ 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
View File
@@ -1,13 +1,84 @@
from django.shortcuts import render from django.shortcuts import render, redirect
from .models import Notice
from django.utils import timezone 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 = Notice.objects.all()
notices = notices.filter(pub_start__lte=timezone.now()) notices = notices.filter(pub_start__lte=timezone.now())
notices = notices.filter(pub_end__gte=timezone.now()) notices = notices.filter(pub_end__gte=timezone.now())
context = { "notices" : notices } context = {"notices": notices}
return render(request, 'posts/index.html', context) 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)
+4
View File
@@ -0,0 +1,4 @@
channels
django-eventstream
+13 -1
View File
@@ -14,9 +14,21 @@
<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 %}
{% block content %} Platzhalter {% endblock %} {% block content %} Platzhalter {% endblock %}
</body> </body>
</html> </html>
+26
View File
@@ -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
View File
@@ -6,45 +6,35 @@
{% 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 %} {% endfor %}
<p><a href="{% url 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p>
</div> </div>
{% endblock %} {% 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>