Browse Source

praktikum sieben bearbeitung

master
Amad Colovic 5 years ago
parent
commit
7bb3690afe
8 changed files with 62 additions and 1 deletions
  1. 0
    0
      .idea/misc.xml
  2. 0
    0
      .idea/modules.xml
  3. 0
    0
      .idea/news.iml
  4. 0
    0
      .idea/vcs.xml
  5. 2
    0
      news/settings.py
  6. 9
    0
      posts/serializers.py
  7. 2
    0
      posts/urls.py
  8. 49
    1
      posts/views.py

+ 0
- 0
.idea/misc.xml View File


+ 0
- 0
.idea/modules.xml View File


+ 0
- 0
.idea/news.iml View File


+ 0
- 0
.idea/vcs.xml View File


+ 2
- 0
news/settings.py View File

@@ -38,6 +38,8 @@ INSTALLED_APPS = [
'django.contrib.messages',
'django.contrib.staticfiles',
'posts.apps.PostsConfig',
'rest_framework',

]

MIDDLEWARE = [

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



+ 2
- 0
posts/urls.py View File

@@ -5,4 +5,6 @@ urlpatterns = [
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),
]

+ 49
- 1
posts/views.py View File

@@ -1,7 +1,18 @@
from django.shortcuts import render, redirect
from .models import Notice
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








def new(request):
@@ -32,3 +43,40 @@ def delete(request, deleteId=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)

Loading…
Cancel
Save