Browse Source

prakt 8

master
Janko Hartwig 2 years ago
parent
commit
c30d6213bc
5 changed files with 76 additions and 7 deletions
  1. 1
    0
      djp/djp/settings.py
  2. 7
    0
      djp/polls/serializers.py
  3. 5
    1
      djp/polls/urls.py
  4. 56
    2
      djp/polls/views.py
  5. 7
    4
      djp/templates/polls/index.html

+ 1
- 0
djp/djp/settings.py View File

@@ -31,6 +31,7 @@ ALLOWED_HOSTS = []
# Application definition

INSTALLED_APPS = [
'rest_framework',
'django.contrib.admin',
'django.contrib.auth',
'django.contrib.contenttypes',

+ 7
- 0
djp/polls/serializers.py 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')

+ 5
- 1
djp/polls/urls.py 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 = [
url(r'^$', views.index, name='index'),
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),
]

+ 56
- 2
djp/polls/views.py View File

@@ -1,12 +1,16 @@
from django.http import HttpResponse
from django.http import HttpResponse, JsonResponse
from django.shortcuts import render, redirect
import time
from django.contrib.auth.decorators import login_required
from django.contrib.admin.views.decorators import staff_member_required
from polls.forms import NoticeForm
from django.utils import timezone
from datetime import timedelta
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):
context = {'now' : time.strftime('%H:%M:%S', time.localtime()),
'posts' : Notice.objects.all() }
@@ -16,6 +20,56 @@ def index(request):
# print(n.notice_title)
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
def new(request):
if request.method == "POST":

+ 7
- 4
djp/templates/polls/index.html View File

@@ -10,16 +10,19 @@ Index
<div class="container">
<h1>Index der Polls-Applikation</h1>
{% for post in posts %}
<p>{{ post.notice_title }} {{ post.notice_text }}
<a class="btn btn-info" role="button">delete message</a></p>

<p>{{ post.notice_title }} {{ post.notice_text }}</p>
<p><a href="{% url 'delete' deleteId=post.id %}" class="btn btn-info">delete message </a></p>
{% endfor %}
</div>
</div>

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



Loading…
Cancel
Save