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 billing_edit(request, admission_id):
    admission = get_object_or_404(Admission, pk=admission_id)
    can_access_admission(request.user, admission)

    if admission.is_draft() or admission.formation.registration_required:
        raise PermissionDenied

    registration_form = RegistrationForm(request.POST or None,
                                         instance=admission,
                                         only_billing=True,
                                         user=request.user)
    address = admission.address
    billing_address = admission.billing_address
    billing_address_form = AddressForm(request.POST or None,
                                       instance=admission.billing_address,
                                       prefix="billing")

    errors = []
    if registration_form.is_valid() and billing_address_form.is_valid():
        billing_address = _update_or_create_specific_address(
            address, billing_address, billing_address_form,
            not registration_form.cleaned_data['use_address_for_billing'])

        admission = registration_form.save(commit=False)
        admission.billing_address = billing_address
        admission.save()

        return redirect(
            reverse('admission_detail', kwargs={'admission_id': admission_id})
            + "#billing")
    else:
        errors.append(
            billing_address_form.errors.update(registration_form.errors))

    return render(
        request, 'admission_billing_form.html', {
            'admission': admission,
            'registration_form': registration_form,
            'billing_address_form': billing_address_form,
            'errors': errors,
        })
예제 #3
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
        }
    )
예제 #4
0
def registration_edit(request, admission_id):
    admission = get_object_or_404(
        Admission.objects.select_related(
            'billing_address',
            'residence_address',
            'address'
        ),
        pk=admission_id
    )
    can_access_admission(request.user, admission)

    if admission.is_draft():
        raise PermissionDenied

    address = admission.address
    billing_address = admission.billing_address
    residence_address = admission.residence_address
    registration_form = RegistrationForm(request.POST or None, instance=admission, user=request.user)
    billing_address_form = AddressForm(request.POST or None, instance=admission.billing_address, prefix="billing")
    residence_address_form = AddressForm(request.POST or None, instance=admission.residence_address, prefix="residence")

    errors = []
    if registration_form.is_valid() and billing_address_form.is_valid() and residence_address_form.is_valid():
        billing_address = _update_or_create_specific_address(
            admission.address,
            billing_address,
            billing_address_form,
            not registration_form.cleaned_data['use_address_for_billing']
        )
        residence_address = _update_or_create_specific_address(
            admission.address,
            residence_address,
            residence_address_form,
            not registration_form.cleaned_data['use_address_for_post']
        )
        admission = registration_form.save(commit=False)
        admission.address = address
        admission.billing_address = billing_address
        admission.residence_address = residence_address
        message = get_appropriate_revision_message(registration_form)
        save_and_create_revision(message, admission, request.user)

        return redirect(reverse('admission_detail', kwargs={'admission_id': admission_id}) + "#registration")
    else:
        errors.append(registration_form.errors)

    return render(
        request,
        'registration_form.html',
        {
            'admission': admission,
            'registration_form': registration_form,
            'billing_address_form': billing_address_form,
            'residence_address_form': residence_address_form,
            'errors': errors,
        }
    )
def admission_form(request, admission_id=None):
    admission = get_object_or_404(Admission, pk=admission_id) if admission_id else None
    if admission:
        can_access_admission(request.user, admission)
        if admission.is_draft():
            raise PermissionDenied
    selected_person = bool(request.POST.get('person_information', False))
    states = admission_state_choices.NEW_ADMIN_STATE[admission.state].get('choices', ()) if admission else None
    base_person = admission.person_information.person if admission else None
    base_person_form = PersonForm(
        data=request.POST or None,
        instance=base_person,
        selected_person=selected_person,
        no_first_name_checked=request.POST.get('no_first_name', False)
    )
    person_information = ContinuingEducationPerson.objects.filter(person=base_person).first()
    # TODO :: get last admission address if it exists instead of None
    address = admission.address if admission else None
    state = admission.state if admission else SUBMITTED
    adm_form = AdmissionForm(
        data=request.POST or None,
        user=request.user,
        instance=admission,
        initial={
            'state': state
        }
    )
    person_form = ContinuingEducationPersonForm(
        data=request.POST or None,
        instance=person_information,
        selected_person=selected_person
    )
    address_form = AddressForm(request.POST or None, instance=address)
    state = admission.state if admission else None
    if adm_form.is_valid() and person_form.is_valid() and address_form.is_valid() and base_person_form.is_valid():
        if address:
            address = address_form.save()
        else:
            address = Address(**address_form.cleaned_data)
            address.save()

        person = person_form.save(commit=False)
        base_person = base_person_form.save()
        person.person_id = base_person.pk
        person.save()

        admission = adm_form.save(commit=False)
        admission.address = address
        admission.billing_address = address
        admission.residence_address = address
        if not admission.person_information:
            admission.person_information = person
        if admission_id:
            admission.save()
        else:
            save_and_create_revision(get_revision_messages(ADMISSION_CREATION), admission, request.user)
        if admission.state == DRAFT:
            return redirect(reverse('admission'))
        return redirect(reverse('admission_detail', kwargs={'admission_id': admission.pk}))

    else:
        errors = list(itertools.product(adm_form.errors, person_form.errors, address_form.errors))
        display_errors(request, errors)

    return render(
        request,
        'admission_form.html',
        {
            'admission': admission,
            'admission_form': adm_form,
            'person_form': person_form,
            'address_form': address_form,
            'base_person_form': base_person_form,
            'state': state,
            'states': states,
            'selected_person': selected_person,
        }
    )