Esempio n. 1
0
def update_location(request, id=None):

    locations = Location.objects.filter(id=id)
    if len(locations) != 1:
        return HttpResponseBadRequest("Location with id " + id + " does not exist. Cannot update.")
    location = locations[0]

    if request.method == "POST":
        updateData = location.__dict__
        updateFiles = {"picture": location.picture}

        # Set fields the form/request doesn't have set
        if 'description' in request.POST:
            updateData['description'] = request.POST['description']

        if 'type' in request.POST:
            updateData['type'] = request.POST['type']

        if 'picture' in request.FILES:
            updateFiles['picture'] = request.FILES['picture']

        if 'picture-clear' in request.POST:
            updateFiles['picture'] = None
            updateData['picture-clear'] = request.POST['picture-clear']

        form = AddLocation(data=updateData, files=updateFiles, instance=location)
        if form.is_valid():
            location = form.save()
            return HttpResponseRedirect("/location/" + str(location.id))

        return render(request, "update.html", {"form": form, "id": id})
    else:
        form = AddLocation(instance=location)

        return render(request, "update.html",{"form": form, "id": id}) # Adds on the address, latitude, and longitude
Esempio n. 2
0
def update_location(request, id=None):

    locations = Location.objects.filter(id=id)
    if len(locations) != 1:
        return HttpResponseBadRequest("Location with id " + id +
                                      " does not exist. Cannot update.")
    location = locations[0]

    if request.method == "POST":
        updateData = location.__dict__
        updateFiles = {
            "picture1": location.picture1,
            "picture2": location.picture2,
            "picture3": location.picture3,
        }

        # Set fields the form/request doesn't have set
        if 'description' in request.POST:
            updateData['description'] = request.POST['description']

        if 'lot_type' in request.POST:
            updateData['lot_type'] = request.POST['lot_type']

        if 'picture1' in request.FILES:
            updateFiles['picture1'] = request.FILES['picture1']

        if 'picture2' in request.FILES:
            updateFiles['picture2'] = request.FILES['picture2']

        if 'picture1' in request.FILES:
            updateFiles['picture3'] = request.FILES['picture3']


#TODO Must also remember to delete the files from disk
        if 'picture-clear' in request.POST:
            updateFiles['picture1'] = None
            updateFiles['picture2'] = None
            updateFiles['picture3'] = None
            updateData['picture-clear'] = request.POST['picture-clear']

        form = AddLocation(data=updateData,
                           files=updateFiles,
                           instance=location)
        if form.is_valid():
            location = form.save()
            return HttpResponseRedirect("/location/" + str(location.id))

        return render(request, "update.html", {"form": form, "id": id})
    else:
        form = AddLocation(instance=location)

        return render(request, "update.html", {
            "form": form,
            "id": id
        })  # Adds on the address, latitude, and longitude
Esempio n. 3
0
def add_location(request):
    if(request.method == "POST"):
        form = AddLocation(request.POST, request.FILES)

        if(form.is_valid()):
            location = form.save()
            return HttpResponseRedirect("/location/" + str(location.id))

        return render(request, "add.html", {"form":form})
    else:
        form = AddLocation()

        return render(request, "add.html",
            dict({"form":form}.items() + request.GET.items()) # Adds on the address, latitude, and longitude
        )
Esempio n. 4
0
def add_location(request):
    if (request.method == "POST"):
        form = AddLocation(request.POST, request.FILES)

        if (form.is_valid()):
            location = form.save()
            return HttpResponseRedirect("/location/" + str(location.id))

        return render(request, "add.html", {"form": form})
    else:
        form = AddLocation()

        return render(
            request,
            "add.html",
            dict({"form": form}.items() + request.GET.items()
                 )  # Adds on the address, latitude, and longitude
        )