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

5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
5 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. from django.shortcuts import render,redirect
  2. from .models import Notice
  3. from django.utils import timezone
  4. import logging
  5. #Pr8
  6. from django.views.decorators.csrf import csrf_exempt
  7. from django.http import HttpResponse,JsonResponse
  8. from polls.forms import NoticeForm
  9. from polls.models import Notice
  10. from polls.serializers import NoticeSerializer
  11. from rest_framework.parsers import JSONParser
  12. #XXXX
  13. # Create your views here.
  14. logger = None
  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 delate(request, deleteId=None):
  22. if deleteId !=None:
  23. delNotice = Notice.objects.get(id=deleteId)
  24. if delNotice != None:
  25. delNotice.delete()
  26. return redirect ('index')
  27. def index(request):
  28. notices = Notice.objects.all()
  29. notices = notices.filter(pub_start__lte=timezone.now())
  30. notices = notices.filter(pub_end__gte=timezone.now())
  31. #context = {"notices" : notices}
  32. return render(request, 'polls/notice.html',{"notices" : notices})
  33. #login_required
  34. def new(request):
  35. if request.method == "POST":
  36. form = NoticeForm(request.POST)
  37. if form.is_valid():
  38. newNotice = Notice(notice_title=form.cleaned_data['title'],
  39. notice_text=form.cleaned_data['text'],
  40. #pub_start=form.cleaned_data['start'],
  41. pub_start= timezone.now(),
  42. pub_end=form.cleaned_data['end'])
  43. newNotice.save()
  44. return redirect('index')
  45. context = {'form': NoticeForm()}
  46. return render(request, 'polls/edit.html', context)
  47. #staff_member_required
  48. def delete(request, deleteId = None):
  49. if deleteId != None:
  50. delNotice = Notice.objects.get(id=deleteId)
  51. if delNotice != None:
  52. delNotice.delete()
  53. return redirect('index')
  54. def welcome_seite(request):
  55. return render(request, 'polls/index.html')
  56. def about_seite(request):
  57. return render(request, 'polls/about.html')
  58. #csrf_exempt
  59. def notice_list (request):
  60. if request.method == 'GET':
  61. notices = Notice.objects.all()
  62. serializer = NoticeSerializer(notices, many=True)
  63. return JsonResponse(serializer.data, safe=False)
  64. elif request.method == 'POST':
  65. data = JSONParser().parser(request)
  66. serializer = NoticeSerializer(data=data)
  67. if serializer.is_valid():
  68. serializer.save()
  69. return JsonResponse(serializer.data, status=201)
  70. return JsonResponse(serializer.errors, status=400)
  71. #csrf_exempt
  72. def notice_detail(request, id):
  73. try:
  74. notice = Notice.objects.get(id =id)
  75. except Notice.DoesNotExist:
  76. return HttpResponse(status=404)
  77. if request.method == 'GET':
  78. serializer = NoticeSerializer(notice)
  79. return JsonResponse(serializer.data)
  80. elif request.method == 'PUT':
  81. data = JSONParser().parse(request)
  82. serializer = NoticeSerializer(notice, data=data)
  83. if serializer.is_valid():
  84. serializer.save()
  85. return JsonResponse(serializer.data)
  86. return JsonResponse(serializer.errors, status=400)
  87. elif request.method == 'DELETE':
  88. notice.delete()
  89. return HttpResponse(status=204)