mappa-ricezione-radio/rxmap/rxmapp/views.py
2020-03-08 01:02:10 +01:00

57 lines
1.6 KiB
Python

from django.conf import settings
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 = 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)