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

5 years ago
5 years ago
5 years ago
5 years ago
12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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 rest_framework.renderers import JSONRenderer
  7. from rest_framework.parsers import JSONParser
  8. from .models import Notice
  9. from .forms import NoticeForm
  10. from .serializers import NoticeSerializer
  11. logger = None
  12. def init_loger(logger_type):
  13. global logger
  14. if logger == None:
  15. logger = logging.getLogger()
  16. logger.setLevel(logging.DEBUG)
  17. logger.addHandler(logging.StreamHandler())
  18. # Create your views here.
  19. def index(request):
  20. init_loger('django.db.backends')
  21. notices = Notice.objects.all()
  22. notices = Notice.objects.filter(pub_start__lte=timezone.now(), pub_end__gte=timezone.now())
  23. message = ""
  24. context = {
  25. "notices": notices,
  26. 'message': message,
  27. }
  28. return render(request, 'posts/index.html', context)
  29. def new(request):
  30. if request.method == "POST":
  31. form = NoticeForm(request.POST)
  32. if form.is_valid():
  33. newNotice = Notice(notice_title=form.cleaned_data['title'],
  34. notice_text=form.cleaned_data['text'],
  35. pub_start=form.cleaned_data['start'],
  36. pub_end=form.cleaned_data['end'])
  37. newNotice.save()
  38. return redirect('posts')
  39. context = {'form' : NoticeForm()}
  40. return render(request, 'posts/edit.html', context)
  41. def delete(request, deleteId=None):
  42. init_loger(__name__)
  43. logger.info(deleteId)
  44. message = ""
  45. if deleteId:
  46. delNotice = Notice.objects.get(id=deleteId)
  47. if delNotice:
  48. delNotice.delete()
  49. message = "Notice has been deleted."
  50. return redirect('posts')
  51. def notice_list(request):
  52. if request.method == 'GET':
  53. notices = Notice.objects.all()
  54. serializer = NoticeSerializer(notices, many=True)
  55. return JsonResponse(serializer.data, safe=False)
  56. elif request.method == 'POST':
  57. data = JSONParser().parse(request)
  58. serializer = NoticeSerializer(data=data)
  59. if serializer.is_valid():
  60. serializer.save()
  61. return JsonResponse(serializer.data, status=201)
  62. return JsonResponse(serializer.errors, status=400)
  63. def notice_detail(request, id):
  64. try:
  65. notice = Notice.objects.get(id=id)
  66. except Notice.DoesNotExist:
  67. return HttpResponse(status=404)
  68. if request.method == 'GET':
  69. serializer = NoticeSerializer(notice)
  70. return JsonResponse(serializer.data)
  71. elif request.method == 'PUT':
  72. data = JSONParser().parse(request)
  73. serializer = NoticeSerializer(notice, data=data)
  74. if serializer.is_valid():
  75. serializer.save()
  76. return JsonResponse(serializer.data)
  77. return JsonResponse(serializer.errors, status=400)
  78. elif request.method == 'DELETE':
  79. notice.delete()
  80. return HttpResponse(status=204)