def formation_edit(request, formation_id):
    formation = get_object_or_404(ContinuingEducationTraining, pk=formation_id)
    if _can_edit_formation(request, formation):
        form = ContinuingEducationTrainingForm(request.POST or None,
                                               user=request.user,
                                               instance=formation)
        address_form = AddressForm(request.POST or None,
                                   instance=formation.postal_address)
        if all([form.is_valid(), address_form.is_valid()]):
            address = address_form.save()
            formation = form.save(commit=False)
            formation.postal_address = address
            formation.save()
            return redirect(
                reverse('formation_detail',
                        kwargs={'formation_id': formation.education_group.id}))
        return render(request, "formation_form.html", {
            'formation': formation,
            'form': form,
            'address_form': address_form
        })
    else:
        display_error_messages(
            request, _("You are not authorized to edit this training"))
        return redirect(
            reverse('formation_detail',
                    kwargs={'formation_id': formation.education_group.id}))
示例#2
0
def formation_edit(request, formation_id):
    formation = get_object_or_404(
        ContinuingEducationTraining.objects.select_related('postal_address', 'education_group'),
        pk=formation_id
    )
    form = ContinuingEducationTrainingForm(request.POST or None, user=request.user, instance=formation)
    address_form = AddressForm(request.POST or None, instance=formation.postal_address)
    if all([form.is_valid(), address_form.is_valid()]):
        address = address_form.save()
        formation = form.save(commit=False)
        formation.postal_address = address
        formation.save()
        return redirect(reverse('formation_detail', kwargs={'formation_id': formation.education_group.id}))
    return render(
        request,
        "formation_form.html",
        {
            'formation': formation,
            'form': form,
            'address_form': address_form
        }
    )