def test_no_first_name_form(self):
     form = PersonForm(data=self.data,
                       selected_person=False,
                       no_first_name_checked=True)
     self.assertTrue(form.is_valid(), form.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,
        }
    )