Browse Source

Update for home use

master
Lukas Weber 3 years ago
parent
commit
0277a717ac
4 changed files with 55 additions and 2 deletions
  1. 7
    0
      posts/serializers.py
  2. 4
    1
      posts/urls.py
  3. 43
    1
      posts/views.py
  4. 1
    0
      webengdjango/settings.py

+ 7
- 0
posts/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')

+ 4
- 1
posts/urls.py View File

urlpatterns = [ urlpatterns = [
path('edit', views.new, name='new'), path('edit', views.new, name='new'),
path('', views.index, name = 'index'), path('', views.index, name = 'index'),
path('delete/<int:deleteId>/', views.delete, name ='delete')
path('delete/<int:deleteId>/', views.delete, name ='delete'),

path('notices/', views.notice_list),
path('notices/<int:id>/', views.notice_detail),
] ]

+ 43
- 1
posts/views.py View File

from django.http import JsonResponse
from django.shortcuts import render, redirect, HttpResponse from django.shortcuts import render, redirect, HttpResponse
from django.utils import timezone from django.utils import timezone
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 django.contrib.admin.views.decorators import staff_member_required
from rest_framework.parsers import JSONParser
from django.views.decorators.csrf import csrf_exempt


from .forms import NoticeForm from .forms import NoticeForm
from .models import Notice from .models import Notice
from .serializers import NoticeSerializer




def index(request): def index(request):
delNotice = Notice.objects.get(id=deleteId) delNotice = Notice.objects.get(id=deleteId)
if delNotice != None: if delNotice != None:
delNotice.delete(()) delNotice.delete(())
return redirect('index')
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 == 'POST':
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)

+ 1
- 0
webengdjango/settings.py View File

'django.contrib.sessions', 'django.contrib.sessions',
'django.contrib.messages', 'django.contrib.messages',
'django.contrib.staticfiles', 'django.contrib.staticfiles',
'rest_framework',
] ]


MIDDLEWARE = [ MIDDLEWARE = [

Loading…
Cancel
Save