|
|
@@ -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": |