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 3.0KB

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