Solutions for MEIM
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

4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
4 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788
  1. from django.http import JsonResponse, HttpResponse
  2. from django.shortcuts import render, redirect
  3. from datetime import timedelta
  4. from django.utils import timezone
  5. from rest_framework.parsers import JSONParser
  6. from django.views.decorators.csrf import csrf_exempt
  7. from posts.models import Notice
  8. from posts.forms import NoticeForm
  9. from django.contrib.auth.decorators import login_required
  10. from django.contrib.admin.views.decorators import staff_member_required
  11. from posts.serializers import NoticeSerializer
  12. # Create your views here.
  13. #Titel und die Texte aller Meldungen, deren Veröffentlichungsdatum vor und der Endedatum nach dem aktuellen Datum liegt.
  14. def home(request, value=""):
  15. now = timezone.now()
  16. notices = Notice.objects.all()
  17. display_notices = list()
  18. for notice in notices:
  19. if notice.pub_start < now and notice.pub_end > now:
  20. display_notices.append(notice)
  21. context = {
  22. "title": "Beboop",
  23. "notices": display_notices
  24. }
  25. return render(request, 'index.html', context)
  26. @login_required
  27. def new(request):
  28. if request.method == "POST":
  29. form = NoticeForm(request.POST)
  30. if form.is_valid():
  31. newNotice = Notice(notice_title=form.cleaned_data['title'],
  32. notice_text=form.cleaned_data['text'],
  33. pub_start=form.cleaned_data['start'],
  34. pub_end=form.cleaned_data['end'])
  35. newNotice.save()
  36. return redirect('index')
  37. context = {'form': NoticeForm()}
  38. return render(request, 'edit.html', context)
  39. @staff_member_required
  40. def delete(request, value):
  41. notice = Notice.objects.get(id = value)
  42. notice.delete()
  43. return redirect('index')
  44. @csrf_exempt
  45. def notice_list(request):
  46. if request.method == "GET":
  47. notices = Notice.objects.all()
  48. serializer = NoticeSerializer(notices, many=True)
  49. return JsonResponse(serializer.data, safe=False)
  50. elif request.method == "POST":
  51. data = JSONParser().parse(request)
  52. serializer = NoticeSerializer(data=data)
  53. if serializer.is_valid():
  54. serializer.save()
  55. return JsonResponse(serializer.data, status=201)
  56. return JsonResponse(serializer.errors, status=400)
  57. return HttpResponse(status=405)
  58. @csrf_exempt
  59. def notice_detail(request, id):
  60. try:
  61. notice = Notice.objects.get(id=id)
  62. except:
  63. return HttpResponse(status=404)
  64. if request.method == 'GET':
  65. serializer = NoticeSerializer(notice)
  66. return JsonResponse(serializer.data)
  67. elif request.method == 'PUT':
  68. data = JSONParser().parse(request)
  69. serializer = NoticeSerializer(notice, data=data)
  70. if serializer.is_valid():
  71. serializer.save()
  72. return JsonResponse(serializer.data)
  73. return JsonResponse(serializer.errors, status=400)
  74. elif request.method == 'DELETE':
  75. notice.delete()
  76. return HttpResponse(status=204)
  77. return HttpResponse(status=405)