12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697 |
- from django.http import JsonResponse
- from django.shortcuts import render, redirect
- from rest_framework.parsers import JSONParser
-
- from .models import Notice
- from django.utils import timezone
- from .forms import NoticeForm
- from django.contrib.auth.decorators import login_required
- from django.views.decorators.csrf import csrf_exempt
-
-
-
- from django.contrib import messages
- from .serializers import NoticeSerializer
-
-
- @login_required
- def new(request):
- if request.method == 'POST':
- form = NoticeForm(request.POST)
- if form.is_valid():
- notice = Notice()
- notice.notice_title = form.cleaned_data['notice_title']
- notice.notice_text = form.cleaned_data['notice_text']
- notice.pub_start = form.cleaned_data['pub_start']
- notice.pub_end = form.cleaned_data['pub_end']
- notice.user_id = request.user.id
- notice.save()
- messages.success(request, f"Neue Nachricht erstellt")
- return redirect('index')
- else:
- for msg in form.error_messages:
- messages.error(request, f"{msg}: {form.error_messages[msg]}")
- context = {'form': NoticeForm()}
- return render(request, 'polls/edit.html', context)
-
- @login_required
- def delete(request, deleteId=None):
- if deleteId is not None:
- try:
- notice = Notice.objects.get(pk=deleteId)
- if request.user.id == notice.user_id or request.user.is_staff:
- notice.delete()
- messages.success(request, f"Nachricht cht")
- else:
- messages.warning(request, f"Keine Berechtigung")
- except:
- messages.warning(request, f"Nachricht nicht gefunden")
- return redirect('index')
-
-
- def index(request):
- notices = Notice.objects.all()
- notices = notices.filter(pub_start__lte=timezone.now())
- notices = notices.filter(pub_end__gte=timezone.now())
- context = {'notices': notices}
- return render(request, 'polls/index.html', context)
-
-
-
- @csrf_exempt
- def notice_list(request):
- if request.method == 'GET':
- notices = Notice.objects.all()
- serializer = NoticeSerializer(notices, many=True)
- return JsonResponse(serializer.data, safe=False)
- elif request.method == 'POST':
- data = JSONParser().parse(request)
- serializer = NoticeSerializer(data=data)
- if serializer.is_valid():
- serializer.save()
- return JsonResponse(serializer.data, status=201)
- return JsonResponse(serializer.errors)
- else:
- return JsonResponse({'error': 'GET or POST request required'}, status=400)
-
- @csrf_exempt
- def notice_detail(request, id):
- try:
- notice = Notice.objects.get(pk=id)
- except Notice.DoesNotExist:
- return JsonResponse({'error': 'Notice not found'}, status=404)
- if request.method == 'GET':
- serializer = NoticeSerializer(notice)
- return JsonResponse(serializer.data)
- elif request.method == 'PUT':
- data = JSONParser().parse(request)
- serializer = NoticeSerializer(notice, data=data)
- if serializer.is_valid():
- serializer.save()
- return JsonResponse(serializer.data)
- return JsonResponse(serializer.errors, status=400)
- elif request.method == 'DELETE':
- notice.delete()
- return JsonResponse({'message': 'Notice was deleted successfully!'}, status=204)
- else:
- return JsonResponse({'error': 'GET, PUT or DELETE request required'}, status=400)
|