Browse Source

prakt 8

master
Janko Hartwig 6 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

# 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
- 0
djp/polls/serializers.py View File

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

from django.conf.urls import url, include, re_path
from django.conf.urls import url, include
from django.urls import path






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),
] ]

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

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() }
# 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":

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

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





Loading…
Cancel
Save