Lösungen der Aufgaben vom Kurs MEIM1 - Web Engineering von Prof. Hofmann
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 4.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133
  1. import logging
  2. from django.shortcuts import render, redirect, render_to_response
  3. from django.http import HttpResponse, JsonResponse
  4. from django.utils import timezone
  5. from django.contrib import messages
  6. from django.views.decorators.csrf import csrf_exempt
  7. from django_eventstream import send_event
  8. from rest_framework.renderers import JSONRenderer
  9. from rest_framework.parsers import JSONParser
  10. from .models import Notice
  11. from .forms import NoticeForm, EditNoticeForm
  12. from .serializers import NoticeSerializer
  13. logger = None
  14. def init_loger(logger_type):
  15. global logger
  16. if logger == None:
  17. logger = logging.getLogger()
  18. logger.setLevel(logging.DEBUG)
  19. logger.addHandler(logging.StreamHandler())
  20. # Create your views here.
  21. def index(request):
  22. init_loger('django.db.backends')
  23. notices = Notice.objects.all()
  24. notices = Notice.objects.filter(pub_start__lte=timezone.now(), pub_end__gte=timezone.now())
  25. message = ""
  26. context = {
  27. "notices": notices,
  28. 'message': message,
  29. }
  30. return render(request, 'posts/index.html', context)
  31. #@login_required
  32. def new(request):
  33. if request.method == "POST":
  34. form = NoticeForm(request.POST)
  35. if form.is_valid():
  36. newNotice = Notice(notice_title=form.cleaned_data['title'],
  37. notice_text=form.cleaned_data['text'],
  38. pub_start=form.cleaned_data['start'],
  39. pub_end=form.cleaned_data['end'])
  40. newNotice.save()
  41. send_event('notice', 'message', newNotice.id)
  42. return redirect('posts')
  43. context = {'form' : NoticeForm()}
  44. return render(request, 'posts/edit.html', context)
  45. def delete(request, deleteId=None):
  46. init_loger(__name__)
  47. logger.info(deleteId)
  48. message = ""
  49. if deleteId:
  50. delNotice = Notice.objects.get(id=deleteId)
  51. if delNotice:
  52. delNotice.delete()
  53. message = "Notice has been deleted."
  54. send_event('notice', 'message', "Notice has been deleted.")
  55. return redirect('posts')
  56. def edit(request, editId=None):
  57. init_loger(__name__)
  58. logger.debug(editId)
  59. logger.debug(request.method)
  60. editNotice = None
  61. if editId:
  62. editNotice = Notice.objects.get(id=editId)
  63. logger.debug(editNotice)
  64. if editNotice:
  65. form = EditNoticeForm(editNotice)
  66. #form.title = editNotice.notice_title
  67. #form.notice_to_form(editNotice)
  68. logger.info(form)
  69. #if form.is_valid():
  70. # return HttpResponse(status=204)
  71. context = {'form' : form}
  72. return render(request, 'posts/edit.html', context)
  73. """
  74. newNotice = Notice(notice_title=form.cleaned_data['title'],
  75. notice_text=form.cleaned_data['text'],
  76. pub_start=form.cleaned_data['start'],
  77. pub_end=form.cleaned_data['end'])
  78. newNotice.save()
  79. send_event('notice', 'message', newNotice.id)
  80. return redirect('posts')
  81. """
  82. @csrf_exempt
  83. def notice_list(request):
  84. if request.method == 'GET':
  85. notices = Notice.objects.all()
  86. serializer = NoticeSerializer(notices, many=True)
  87. return JsonResponse(serializer.data, safe=False)
  88. elif request.method == 'POST':
  89. data = JSONParser().parse(request)
  90. serializer = NoticeSerializer(data=data)
  91. if serializer.is_valid():
  92. serializer.save()
  93. return JsonResponse(serializer.data, status=201)
  94. return JsonResponse(serializer.errors, status=400)
  95. @csrf_exempt
  96. def notice_detail(request, id):
  97. try:
  98. notice = Notice.objects.get(id=id)
  99. except Notice.DoesNotExist:
  100. return HttpResponse(status=404)
  101. if request.method == 'GET':
  102. serializer = NoticeSerializer(notice)
  103. return JsonResponse(serializer.data)
  104. elif request.method == 'PUT':
  105. data = JSONParser().parse(request)
  106. serializer = NoticeSerializer(notice, data=data)
  107. if serializer.is_valid():
  108. serializer.save()
  109. return JsonResponse(serializer.data)
  110. return JsonResponse(serializer.errors, status=400)
  111. elif request.method == 'DELETE':
  112. notice.delete()
  113. return HttpResponse(status=204)