views.py 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667
  1. from django.conf import settings
  2. from django.http import JsonResponse
  3. from django.shortcuts import redirect, render
  4. from django.views.decorators.csrf import csrf_exempt
  5. from django.views.generic import TemplateView
  6. from .models import RapportoRicezione, TipoRadio, User
  7. # Create your views here.
  8. class ViewMap(TemplateView):
  9. template_name = "index.html"
  10. def get_context_data(self, **kwargs):
  11. ctx = super().get_context_data(**kwargs)
  12. ctx["tipiradio"] = TipoRadio.objects.all()
  13. return ctx
  14. def add_page(request):
  15. # TODO: replace with /api/tipiradio/get etc.
  16. return render(
  17. request,
  18. template_name="add.html",
  19. context=dict(tipiradio=TipoRadio.objects.all()),
  20. )
  21. def rapporti_get(request):
  22. return JsonResponse(
  23. dict(rapporti=[r.serialize() for r in RapportoRicezione.objects.all()])
  24. )
  25. def rapporto_edit_redirect(request, pk):
  26. r = RapportoRicezione.objects.get(pk=pk) # request.GET["rid"])
  27. return redirect(r.edit_link)
  28. @csrf_exempt
  29. def rapporto_add(request):
  30. user = request.user
  31. if user.is_anonymous:
  32. if settings.RX_ADD_DEFAULT_USER:
  33. user = User.objects.get(pk=int(settings.RX_ADD_DEFAULT_USER))
  34. else:
  35. user = User.objects.filter().order_by("id").first()
  36. r = RapportoRicezione(
  37. author=user,
  38. lat=float(request.POST["lat"]),
  39. lng=float(request.POST["lng"]),
  40. comprensibile=int(request.POST["comprensibile"]),
  41. stabilita=int(request.POST["stabilita"]),
  42. indirizzo=request.POST.get("indirizzo", ""),
  43. tipo_radio=TipoRadio.objects.get(pk=request.POST["tiporadio"]),
  44. )
  45. r.save()
  46. return JsonResponse(r.serialize())
  47. @csrf_exempt
  48. def rapporto_delete(request):
  49. RapportoRicezione.objects.get(pk=request.POST["rid"]).delete()
  50. return JsonResponse(True, safe=False)