12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667 |
- from django.conf import settings
- from django.http import JsonResponse
- from django.shortcuts import redirect, render
- from django.views.decorators.csrf import csrf_exempt
- from django.views.generic import TemplateView
- from .models import RapportoRicezione, TipoRadio, User
- # Create your views here.
- class ViewMap(TemplateView):
- template_name = "index.html"
- def get_context_data(self, **kwargs):
- ctx = super().get_context_data(**kwargs)
- ctx["tipiradio"] = TipoRadio.objects.all()
- return ctx
- 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 = request.user
- if user.is_anonymous:
- if settings.RX_ADD_DEFAULT_USER:
- user = User.objects.get(pk=int(settings.RX_ADD_DEFAULT_USER))
- else:
- user = User.objects.filter().order_by("id").first()
- r = RapportoRicezione(
- author=user,
- lat=float(request.POST["lat"]),
- lng=float(request.POST["lng"]),
- comprensibile=int(request.POST["comprensibile"]),
- stabilita=int(request.POST["stabilita"]),
- indirizzo=request.POST.get("indirizzo", ""),
- 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)
|