def edit(request, offset):
    # get the alert object
    v = Alert.objects.get(pk=int(offset))
    system_id = int(request.session["system"])

    # populates the form with the object, and if it's a post, fill with the new information
    if request.method == "POST":
        form = AlertForm(system_id, request.POST, instance=v)
    else:
        form = AlertForm(system_id, instance=v)
    # if the form validates, saves the new information
    if form.is_valid():
        v = form.save(commit=False)
        v.destinataries.clear()
        sender = User.objects.get(pk=request.session["user_id"])
        v.sender = sender
        for u in form.data.getlist("destinataries"):
            v.destinataries.add(u)
        v.system_id = system_id
        v.save()
        return HttpResponseRedirect("/alerts/edit/finish")

    if form.fields["vehicle"].queryset:
        vehicles_exist = True
    else:
        vehicles_exist = False

    return render_to_response("alerts/templates/edit.html", locals(), context_instance=RequestContext(request))
def create(request, offset):

    if request.method == "POST":
        form = AlertForm(int(offset), request.POST)

        if form.is_valid():

            system_id = request.session["system"]
            a = form.save(commit=False)
            a.system_id = system_id
            a.save()

            sender = User.objects.get(pk=request.session["user_id"])
            a.sender = sender

            for dest in form.data.getlist("destinataries"):
                a.destinataries.add(dest)

            for vehi in form.data.getlist("vehicle"):
                a.vehicle.add(vehi)

            a.save()

            try:
                for g in form.data.getlist("geofence"):
                    geofence = Geofence.objects.get(pk=g)
                    a.geofence = geofence
            except:
                pass

            a.save()

            return HttpResponseRedirect("/alerts/create/finish")
        else:
            if form.fields["vehicle"].queryset:
                vehicles_exist = True
            else:
                vehicles_exist = False

            return render_to_response(
                "alerts/templates/create.html", locals(), context_instance=RequestContext(request)
            )

    else:
        form = AlertForm(int(offset))

        if form.fields["vehicle"].queryset:
            vehicles_exist = True
        else:
            vehicles_exist = False

        return render_to_response("alerts/templates/create.html", locals(), context_instance=RequestContext(request))