You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

views.py 2.8KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. from django.shortcuts import render, redirect
  2. from .models import Notice
  3. from django.utils import timezone
  4. from django.http import HttpResponse
  5. from .forms import NoticeForm
  6. from posts.serializer import NoticeSerializer
  7. from django.http import JsonResponse
  8. from rest_framework.parsers import JSONParser
  9. from django.views.decorators.csrf import csrf_exempt
  10. from django_eventstream import send_event
  11. # Create your views here.
  12. def post(request):
  13. return render(request, "posts/index.html")
  14. def index(request):
  15. notices = Notice.objects.all()
  16. notices = notices.filter(pub_start__lt = timezone.now())
  17. notices = notices.filter(pub_end__gt = timezone.now())
  18. context = { "notices" : notices}
  19. return render(request, 'posts/index.html', context)
  20. def new(request):
  21. if request.method == "POST":
  22. form = NoticeForm(request.POST)
  23. if form.is_valid():
  24. new_Notice = Notice(notice_title=form.cleaned_data['title'],
  25. notice_text=form.cleaned_data['text'],
  26. pub_start=form.cleaned_data['start'],
  27. pub_end=form.cleaned_data['end'])
  28. new_Notice.save()
  29. send_event('notice', 'message', new_Notice.id)
  30. return redirect('index')
  31. context = {'form': NoticeForm()}
  32. return render(request, 'posts/edit.html', context)
  33. def delete(request, deleteId=None):
  34. if deleteId != None:
  35. delNotice = Notice.objects.get(id=deleteId)
  36. if delNotice != None:
  37. send_event('notice', 'message', delNotice.id)
  38. delNotice.delete()
  39. return redirect('index')
  40. @csrf_exempt
  41. def notice_list(request):
  42. if request.method == 'GET':
  43. notices = Notice.objects.all()
  44. serializer = NoticeSerializer(notices, many=True)
  45. return JsonResponse(serializer.data, safe=False)
  46. elif request.method == 'POST':
  47. data = JSONParser().parse(request)
  48. serializer = NoticeSerializer(data=data)
  49. if serializer.is_valid():
  50. serializer.save()
  51. return JsonResponse(serializer.data, status=201)
  52. return JsonResponse(serializer.errors, status=400)
  53. @csrf_exempt
  54. def notice_detail(request, id):
  55. try:
  56. notice = Notice.objects.get(id=id)
  57. except Notice.DoesNotExist:
  58. return HttpResponse(status=404)
  59. if request.method == 'GET':
  60. serializer = NoticeSerializer(notice)
  61. return JsonResponse(serializer.data)
  62. elif request.method == 'PUT':
  63. data = JSONParser().parse(request)
  64. serializer=NoticeSerializer(notice, data=data)
  65. if serializer.is_valid():
  66. serializer.save()
  67. return JsonResponse(serializer.data)
  68. return JsonResponse(serializer.errors, status=400)
  69. elif request.method == 'DELETE':
  70. notice.delete()
  71. return HttpResponse(status=204)