def test_participant_required_fields(self):
     self.client.force_login(self.manager.person.user)
     form = AdmissionForm(data=self.data, user=self.manager.person.user)
     admission_participant_required_fields = [
         'citizenship',
         'phone_mobile',
         'high_school_diploma',
         'last_degree_level',
         'last_degree_field',
         'last_degree_institution',
         'last_degree_graduation_year',
         'professional_status',
         'current_occupation',
         'current_employer',
         'activity_sector',
         'motivation',
         'professional_personal_interests',
         'formation',
         'email',
     ]
     for required_field in admission_participant_required_fields:
         with self.subTest(required_field=required_field):
             self.assertEqual(
                 form.fields[required_field].widget.attrs['class'],
                 'participant_required')
 def test_not_valid_wrong_phone_format(self):
     wrong_numbers = [
         '1234567891', '00+32474945669', '0+32474123456', '(32)1234567891',
         '0474.12.34.56', '0474 123456'
     ]
     short_numbers = ['0032123', '+321234', '0123456']
     long_numbers = [
         '003212345678912456', '+3212345678912345', '01234567891234567'
     ]
     self.data['phone_mobile'] = random.choice(wrong_numbers +
                                               short_numbers + long_numbers)
     form = AdmissionForm(data=self.data,
                          user=self.training_manager.person.user)
     self.assertFalse(form.is_valid(), form.errors)
     self.assertDictEqual(
         form.errors, {
             'phone_mobile': [
                 _("Phone number must start with 0 or 00 or '+' followed by at least "
                   "7 digits and up to 15 digits.")
             ],
         })
 def test_academic_year_field_not_required(self):
     self.client.force_login(self.manager.person.user)
     for state in [
             REJECTED, WAITING, DRAFT, SUBMITTED, CANCELLED,
             CANCELLED_NO_REGISTRATION_REQUIRED
     ]:
         with self.subTest(state=state):
             self.admission.state = state
             self.admission.save()
             form = AdmissionForm(data=self.data,
                                  user=self.manager.person.user,
                                  instance=self.admission)
             self.assertFalse(form.fields['academic_year'].required)
 def test_academic_year_field_required(self):
     self.client.force_login(self.manager.person.user)
     for state in [
             ACCEPTED, ACCEPTED_NO_REGISTRATION_REQUIRED,
             REGISTRATION_SUBMITTED, VALIDATED
     ]:
         with self.subTest(state=state):
             self.admission.state = state
             self.admission.save()
             form = AdmissionForm(data=self.data,
                                  user=self.manager.person.user,
                                  instance=self.admission)
             self.assertTrue(form.fields['academic_year'].required)
 def test_valid_form_for_training_managers(self):
     form = AdmissionForm(data=self.data,
                          user=self.training_manager.person.user)
     self.assertTrue(form.is_valid(), form.errors)
 def test_valid_form_for_managers(self):
     self.client.force_login(self.manager.person.user)
     form = AdmissionForm(data=self.data, user=self.manager.person.user)
     self.assertTrue(form.is_valid(), form.errors)
 def test_manager_email_field_required(self):
     self.client.force_login(self.manager.person.user)
     form = AdmissionForm(data=self.data, user=self.manager.person.user)
     self.assertTrue(form.fields['email'].required)
def admission_detail(request, admission_id):
    user_is_continuing_education_student_worker = is_continuing_education_student_worker(request.user)

    admission = get_object_or_404(
        Admission.objects.select_related(
            'billing_address__country',
            'address__country',
            'person_information__person',
            'person_information__birth_country',
            'citizenship',
            'formation__education_group',
        ).prefetch_related('formation__managers'),
        pk=admission_id
    )
    if not user_is_continuing_education_student_worker:
        can_access_admission(request.user, admission)

    files = AdmissionFile.objects.filter(admission=admission_id)
    accepted_states = admission_state_choices.NEW_ADMIN_STATE[admission.state]
    states = _get_states_choices(accepted_states, admission, request)

    adm_form = AdmissionForm(
        request.POST or None,
        instance=admission,
    )

    version_list = get_versions(admission)

    if request.method == 'POST' and request.FILES:
        return _upload_file(request, admission)

    rejected_adm_form = RejectedAdmissionForm(
        request.POST or None,
        instance=admission,
        prefix='rejected',
    )

    waiting_adm_form = WaitingAdmissionForm(
        request.POST or None,
        instance=admission,
    )

    condition_acceptance_adm_form = ConditionAcceptanceAdmissionForm(
        request.POST or None,
        instance=admission,
    )

    cancel_adm_form = CancelAdmissionForm(
        request.POST or None,
        instance=admission,
    )

    admission._original_state = admission.state
    if adm_form.is_valid():
        forms = (adm_form, waiting_adm_form, rejected_adm_form, condition_acceptance_adm_form, cancel_adm_form)
        return _change_state(request, forms, accepted_states, admission)

    _display_adapted_ucl_registration_message(admission, request)

    return render(
        request, "admission_detail.html",
        {
            'admission': admission,
            'files': files,
            'states': states,
            'admission_form': adm_form,
            'rejected_adm_form': rejected_adm_form,
            'waiting_adm_form': waiting_adm_form,
            'cancel_adm_form': cancel_adm_form,
            'file_categories_choices': _get_file_category_choices_with_disabled_parameter(admission),
            'invoice': file_category_choices.INVOICE,
            'condition_acceptance_adm_form': condition_acceptance_adm_form,
            'user_is_continuing_education_student_worker': user_is_continuing_education_student_worker,
            'version': version_list,
            'MAX_UPLOAD_SIZE': MAX_UPLOAD_SIZE,
            'opened_tab': request.GET.get('opened_tab'),
            'injection_not_rejected': admission.ucl_registration_complete != UCLRegistrationState.REJECTED.name
        }
    )
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,
        }
    )