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.4KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. from django.http import JsonResponse
  2. from django.shortcuts import render, redirect
  3. from rest_framework.parsers import JSONParser
  4. from .models import Notice
  5. from django.utils import timezone
  6. from .forms import NoticeForm
  7. from django.contrib.auth.decorators import login_required
  8. from django.views.decorators.csrf import csrf_exempt
  9. from django.contrib import messages
  10. from .serializers import NoticeSerializer
  11. @login_required
  12. def new(request):
  13. if request.method == 'POST':
  14. form = NoticeForm(request.POST)
  15. if form.is_valid():
  16. notice = Notice()
  17. notice.notice_title = form.cleaned_data['notice_title']
  18. notice.notice_text = form.cleaned_data['notice_text']
  19. notice.pub_start = form.cleaned_data['pub_start']
  20. notice.pub_end = form.cleaned_data['pub_end']
  21. notice.user_id = request.user.id
  22. notice.save()
  23. messages.success(request, f"Neue Nachricht erstellt")
  24. return redirect('index')
  25. else:
  26. for msg in form.error_messages:
  27. messages.error(request, f"{msg}: {form.error_messages[msg]}")
  28. context = {'form': NoticeForm()}
  29. return render(request, 'polls/edit.html', context)
  30. @login_required
  31. def delete(request, deleteId=None):
  32. if deleteId is not None:
  33. try:
  34. notice = Notice.objects.get(pk=deleteId)
  35. if request.user.id == notice.user_id or request.user.is_staff:
  36. notice.delete()
  37. messages.success(request, f"Nachricht cht")
  38. else:
  39. messages.warning(request, f"Keine Berechtigung")
  40. except:
  41. messages.warning(request, f"Nachricht nicht gefunden")
  42. return redirect('index')
  43. def index(request):
  44. notices = Notice.objects.all()
  45. notices = notices.filter(pub_start__lte=timezone.now())
  46. notices = notices.filter(pub_end__gte=timezone.now())
  47. context = {'notices': notices}
  48. return render(request, 'polls/index.html', context)
  49. @csrf_exempt
  50. def notice_list(request):
  51. if request.method == 'GET':
  52. notices = Notice.objects.all()
  53. serializer = NoticeSerializer(notices, many=True)
  54. return JsonResponse(serializer.data, safe=False)
  55. elif request.method == 'POST':
  56. data = JSONParser().parse(request)
  57. serializer = NoticeSerializer(data=data)
  58. if serializer.is_valid():
  59. serializer.save()
  60. return JsonResponse(serializer.data, status=201)
  61. return JsonResponse(serializer.errors)
  62. else:
  63. return JsonResponse({'error': 'GET or POST request required'}, status=400)
  64. @csrf_exempt
  65. def notice_detail(request, id):
  66. try:
  67. notice = Notice.objects.get(pk=id)
  68. except Notice.DoesNotExist:
  69. return JsonResponse({'error': 'Notice not found'}, status=404)
  70. if request.method == 'GET':
  71. serializer = NoticeSerializer(notice)
  72. return JsonResponse(serializer.data)
  73. elif request.method == 'PUT':
  74. data = JSONParser().parse(request)
  75. serializer = NoticeSerializer(notice, data=data)
  76. if serializer.is_valid():
  77. serializer.save()
  78. return JsonResponse(serializer.data)
  79. return JsonResponse(serializer.errors, status=400)
  80. elif request.method == 'DELETE':
  81. notice.delete()
  82. return JsonResponse({'message': 'Notice was deleted successfully!'}, status=204)
  83. else:
  84. return JsonResponse({'error': 'GET, PUT or DELETE request required'}, status=400)