Ejemplo n.º 1
0
def app_add(request):
    """View for adding an ``Application``."""
    if request.method == "POST":
        form = ApplicationForm(request.POST, request.FILES)
        if form.is_valid():
            instance = form.save(commit=False)
            instance.owner = request.user
            instance.save()
            form.save_m2m()
            messages.success(request, 'The application "%s" has been added.' % instance.name)
            return redirect(instance.get_absolute_url())
    else:
        form = ApplicationForm()
    context = {"form": form}
    return TemplateResponse(request, "apps/object_add.html", context)
Ejemplo n.º 2
0
def app_edit(request, slug):
    app = get_object_or_404(Application.active, slug__exact=slug)
    if not app.is_editable_by(request.user):
        raise Http404
    if request.method == "POST":
        form = ApplicationForm(request.POST, request.FILES, instance=app)
        link_formset = ApplicationLinkFormSet(request.POST, instance=app)
        image_formset = ApplicationMediaFormSet(request.POST, request.FILES, instance=app)
        if form.is_valid() and link_formset.is_valid() and image_formset.is_valid():
            instance = form.save()
            link_formset.save()
            image_formset.save()
            messages.success(request, 'The application "%s" has been updated.' % instance.name)
            return redirect(instance.get_absolute_url())
    else:
        form = ApplicationForm(instance=app)
        link_formset = ApplicationLinkFormSet(instance=app)
        image_formset = ApplicationMediaFormSet(instance=app)
    context = {"object": app, "form": form, "link_formset": link_formset, "image_formset": image_formset}
    return TemplateResponse(request, "apps/object_edit.html", context)