from django.shortcuts import render,redirect from django.http import JsonResponse from django.views.decorators.csrf import csrf_exempt from .models import RapportoRicezione, User, TipoRadio # Create your views here. def add_page(request): # TODO: replace with /api/tipiradio/get etc. return render( request, template_name="add.html", context=dict(tipiradio=TipoRadio.objects.all()), ) def rapporti_get(request): return JsonResponse( dict(rapporti=[r.serialize() for r in RapportoRicezione.objects.all()]) ) def rapporto_edit_redirect(request, pk): r = RapportoRicezione.objects.get(pk=pk) # request.GET["rid"]) return redirect(r.edit_link) @csrf_exempt def rapporto_add(request): user = User.objects.filter(is_superuser=True).first() r = RapportoRicezione( author=user, lat=float(request.POST["lat"]), lng=float(request.POST["lng"]), comprensibile=int(request.POST["comprensibile"]), stabilita=int(request.POST["comprensibile"]), tipo_radio=TipoRadio.objects.get(pk=request.POST["tiporadio"]), ) r.save() return JsonResponse(r.serialize()) @csrf_exempt def rapporto_delete(request): RapportoRicezione.objects.get(pk=request.POST["rid"]).delete() return JsonResponse(True, safe=False)