This commit is contained in:
Janko Hartwig 2018-12-10 17:12:26 +01:00
parent 5e5e30306c
commit c30d6213bc
5 changed files with 76 additions and 7 deletions

View File

@ -31,6 +31,7 @@ ALLOWED_HOSTS = []
# Application definition # Application definition
INSTALLED_APPS = [ INSTALLED_APPS = [
'rest_framework',
'django.contrib.admin', 'django.contrib.admin',
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',

7
djp/polls/serializers.py Normal file
View File

@ -0,0 +1,7 @@
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')

View File

@ -1,4 +1,5 @@
from django.conf.urls import url, include, re_path from django.conf.urls import url, include
from django.urls import path
@ -8,4 +9,7 @@ from . import views
urlpatterns = [ urlpatterns = [
url(r'^$', views.index, name='index'), url(r'^$', views.index, name='index'),
url('new', views.new, name='new'), url('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),
] ]

View File

@ -1,12 +1,16 @@
from django.http import HttpResponse from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect from django.shortcuts import render, redirect
import time import time
from django.contrib.auth.decorators import login_required from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required
from polls.forms import NoticeForm from polls.forms import NoticeForm
from django.utils import timezone from django.utils import timezone
from datetime import timedelta from datetime import timedelta
from polls.models import Notice from polls.models import Notice
from polls.serializers import NoticeSerializer
from rest_framework.renderers import JSONRenderer
from rest_framework.parsers import JSONParser
from django.views.decorators.csrf import csrf_exempt
def index(request): def index(request):
context = {'now' : time.strftime('%H:%M:%S', time.localtime()), context = {'now' : time.strftime('%H:%M:%S', time.localtime()),
'posts' : Notice.objects.all() } 'posts' : Notice.objects.all() }
@ -16,6 +20,56 @@ def index(request):
# print(n.notice_title) # print(n.notice_title)
return render(request, 'polls/index.html', context) return render(request, 'polls/index.html', context)
@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 HttpResponse(status=405)
@csrf_exempt
def notice_detail(request, id):
try:
notice = Notice.objects.get(id=id)
except:
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, status=201)
return JsonResponse(serializer.errors, status=400)
elif request.method == 'DELETE':
notice.delete()
return HttpResponse(status=204)
return HttpResponse(status=405)
@staff_member_required
def delete(request, deleteId=None):
if deleteId != None:
deleteNotice = Notice.objects.get(id=deleteId)
if deleteNotice != None:
deleteNotice.delete()
return redirect('index')
@login_required @login_required
def new(request): def new(request):
if request.method == "POST": if request.method == "POST":

View File

@ -10,16 +10,19 @@ Index
<div class="container"> <div class="container">
<h1>Index der Polls-Applikation</h1> <h1>Index der Polls-Applikation</h1>
{% for post in posts %} {% for post in posts %}
<p>{{ post.notice_title }} {{ post.notice_text }} <p>{{ post.notice_title }} {{ post.notice_text }}</p>
<a class="btn btn-info" role="button">delete message</a></p> <p><a href="{% url 'delete' deleteId=post.id %}" class="btn btn-info">delete message </a></p>
{% endfor %} {% endfor %}
</div> </div>
</div> </div>
<p><a href="{% url 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p> <p><a href="{% url 'new' %}" class="btn btn-info" role="button">Neue Nachricht</a></p>
<p><a href="{% url 'logout' %}" class="btn btn-warning">Abmelden</a></p> {% if user.is_authenticated %}
<p><a href="{% url 'logout' %}?next=/polls"
class="btn btn-warning">Abmelden</a></p>
{% endif %}
{% endblock %} {% endblock %}