views.py 1.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445
  1. from django.shortcuts import render
  2. from django.http import JsonResponse
  3. from django.views.decorators.csrf import csrf_exempt
  4. from .models import RapportoRicezione, User, TipoRadio
  5. # Create your views here.
  6. def add_page(request):
  7. # TODO: replace with /api/tipiradio/get etc.
  8. return render(
  9. request,
  10. template_name="add.html",
  11. context=dict(tipiradio=TipoRadio.objects.all()),
  12. )
  13. def rapporti_get(request):
  14. return JsonResponse(
  15. dict(rapporti=[r.serialize() for r in RapportoRicezione.objects.all()])
  16. )
  17. @csrf_exempt
  18. def rapporto_add(request):
  19. user = User.objects.filter(is_superuser=True).first()
  20. r = RapportoRicezione(
  21. author=user,
  22. lat=request.POST["lat"],
  23. lng=request.POST["lng"],
  24. comprensibile=request.POST["comprensibile"],
  25. stabilita=request.POST["comprensibile"],
  26. tipo_radio=TipoRadio.objects.get(pk=request.POST["tiporadio"]),
  27. )
  28. r.save()
  29. return JsonResponse(True, safe=False)
  30. @csrf_exempt
  31. def rapporto_delete(request):
  32. RapportoRicezione.objects.get(pk=request.POST["rid"]).delete()
  33. return JsonResponse(True, safe=False)