from django.http import HttpResponseForbidden, JsonResponse from django.shortcuts import get_object_or_404 from django.utils.html import linebreaks from django.views.decorators.csrf import csrf_exempt from rest_framework import serializers from korektury.utils import send_email_notification_komentar from korektury.models import Oprava, KorekturovanePDF, Komentar from personalni.models import Organizator, Osoba def korektury_stav_view(request, pdf_id: int, **kwargs): q = request.POST pdf = get_object_or_404(KorekturovanePDF, id=pdf_id) status = q.get('state') if status is not None: assert status in KorekturovanePDF.STATUS.values pdf.status = status pdf.save() return JsonResponse({'status': pdf.status}) def oprava_stav_view(request, **kwargs): q = request.POST op_id_str = q.get('id') assert op_id_str is not None op_id = int(op_id_str) op = get_object_or_404(Oprava, id=op_id) status = q.get('action') if status is not None: assert status in Oprava.STATUS.values op.status = status op.save() return JsonResponse({'status': op.status}) class KomentarSerializer(serializers.ModelSerializer): class Meta: model = Komentar fields = '__all__' def to_representation(self, instance): ret = super().to_representation(instance) ret["autor"] = str(instance.autor) ret["text"] = linebreaks(ret["text"], autoescape=True) # Autora není třeba escapovat, ten se vkládá jako text. return ret class OpravaSerializer(serializers.ModelSerializer): class Meta: model = Oprava fields = '__all__' def to_representation(self, instance): ret = super().to_representation(instance) ret["komentare"] = [KomentarSerializer(komentar).data for komentar in instance.komentar_set.all()] return ret # komentar_set = serializers.ListField(child=KomentarSerializer()) def opravy_a_komentare_view(request, pdf_id: int, **kwargs): q = request.POST opravy = Oprava.objects.filter(pdf=pdf_id).all() # Serializovat list je prý security vulnerability, tedy je přidán slovník pro bezpečnost return JsonResponse({"context": [OpravaSerializer(oprava).data for oprava in opravy]})