Esempio n. 1
0
def dive_edit(request, dive_id):
    """
    View for editing dive data.
    """
    dive = get_object_or_404(Dive, pk=dive_id, user=request.user)

    # Fetch existing buddies & divemasters for auto-completion
    buddies = (
        Dive.objects.filter(user=request.user)
        .exclude(buddy="")
        .values_list("buddy", flat=True)
        .order_by("buddy")
        .distinct()
    )
    divemasters = (
        Dive.objects.filter(user=request.user)
        .exclude(divemaster="")
        .values_list("divemaster", flat=True)
        .order_by("divemaster")
        .distinct()
    )

    if request.method == "POST":
        form = DiveForm(request.POST, instance=dive)
        if form.is_valid():
            form.save()
            return redirect("divelog_dive_view", dive_id=dive_id)
    else:
        form = DiveForm(instance=dive)

    t = loader.get_template("divelog/dives/edit.html")
    c = RequestContext(request, {"form": form, "buddies": list(buddies), "divemasters": list(divemasters)})
    return HttpResponse(t.render(c))
Esempio n. 2
0
def dive_add(request):
    """
    View for adding a new dive.
    """
    if request.method == "POST":
        form = DiveForm(request.POST)
        form.instance.user = request.user
        form.instance.number = 1
        form.instance.size = 0

        if form.is_valid():
            new_dive = form.save()
            messages.success(request, "New dive added.")
            return redirect("divelog_dive_view", dive_id=new_dive.id)
        else:
            messages.error(request, "Failed saving dive.")
    else:
        form = DiveForm()

    t = loader.get_template("divelog/dives/add.html")
    c = RequestContext(request, {"form": form})
    return HttpResponse(t.render(c))