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

5 years ago
1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889
  1. from django.shortcuts import render, redirect
  2. from django.http import HttpResponse, JsonResponse
  3. from .models import Notice
  4. from django.utils import timezone
  5. import logging
  6. from .forms import NoticeForm
  7. from .serializers import NoticeSerializer
  8. from rest_framework.parsers import JSONParser
  9. from django.views.decorators.csrf import csrf_exempt
  10. from django_eventstream import send_event
  11. logger = None
  12. # Create your views here.
  13. def home(request):
  14. return HttpResponse('Es gibt nichts hier.... bye bye')
  15. def initLogger():
  16. global logger
  17. if logger == None:
  18. logger = logging.getLogger('django.db.backends')
  19. logger.setLevel(logging.DEBUG)
  20. logger.addHandler(logging.StreamHandler())
  21. def index(request):
  22. initLogger()
  23. notices = Notice.objects.all()
  24. notices = notices.filter(pub_start__lte=timezone.now() )
  25. notices = notices.filter(pub_end__gte=timezone.now())
  26. context = { "notices" : notices }
  27. return render(request, 'posts/index.html', context)
  28. def new(request):
  29. if request.method == "POST":
  30. form = NoticeForm(request.POST)
  31. if form.is_valid():
  32. newNotice = Notice(notice_title=form.cleaned_data['title'],
  33. notice_text=form.cleaned_data['text'],
  34. pub_start=form.cleaned_data['start'],
  35. pub_end=form.cleaned_data['end'])
  36. newNotice.save()
  37. context = {'form' : NoticeForm()}
  38. return render(request, 'posts/edit.html', context)
  39. def delete(request, deleteId=None):
  40. if deleteId != None:
  41. delNotice = Notice.objects.get(id=deleteId)
  42. if delNotice != None:
  43. delNotice.delete()
  44. return redirect('index')
  45. @csrf_exempt
  46. def notice_list(request):
  47. if request.method == 'GET':
  48. notices = Notice.objects.all()
  49. serializer = NoticeSerializer(notices, many=True)
  50. return JsonResponse(serializer.data, safe=False)
  51. elif request.method == 'POST':
  52. data = JSONParser().parse(request)
  53. serializer = NoticeSerializer(data=data)
  54. if serializer.is_valid():
  55. serializer.save()
  56. return JsonResponse(serializer.data, status=201)
  57. return JsonResponse(serializer.errors, status=400)
  58. @csrf_exempt
  59. def notice_detail(request, id):
  60. try:
  61. notice = Notice.objects.get(id=id)
  62. except Notice.DoesNotExist:
  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)