diff --git a/candidats/forms.py b/candidats/forms.py index 220860d..6aebc81 100644 --- a/candidats/forms.py +++ b/candidats/forms.py @@ -15,11 +15,8 @@ class CandidateForm(forms.ModelForm): fields = '__all__' def __init__(self, *args, **kwargs): - if kwargs.get('instance'): - try: - kwargs['initial'] = {'interview': kwargs['instance'].interview} - except Interview.DoesNotExist: - pass + if kwargs.get('instance') and kwargs['instance'].has_interview: + kwargs['initial'] = {'interview': kwargs['instance'].interview} return super().__init__(*args, **kwargs) def save(self, **kwargs): diff --git a/candidats/models.py b/candidats/models.py index 749467f..56512a7 100644 --- a/candidats/models.py +++ b/candidats/models.py @@ -150,6 +150,14 @@ class Candidate(models.Model): else: return '{0}, option «{1}»'.format(self.get_section_display(), self.get_option_display()) + @property + def has_interview(self): + try: + self.interview + return True + except Interview.DoesNotExist: + return False + @property def total_result(self): return (self.examination_result or 0) + (self.interview_result or 0) + (self.file_result or 0) diff --git a/candidats/tests.py b/candidats/tests.py index b1e995e..aee888e 100644 --- a/candidats/tests.py +++ b/candidats/tests.py @@ -270,11 +270,10 @@ Sans nouvelles de votre part 5 jours ouvrables avant la date du premier examen, def test_validation_enseignant_ede(self): self.maxDiff = None - ede = Section.objects.create(name='EDE') henri = Candidate.objects.create( first_name='Henri', last_name='Dupond', gender='M', birth_date=date(2000, 5, 15), street="Rue Neuve 3", pcode='2222', city='Petaouchnok', - section=ede, option='ENF', + section='EDE', option='ENF', email='henri@example.org', deposite_date=date.today() ) t1 = Teacher.objects.create( @@ -283,11 +282,14 @@ Sans nouvelles de votre part 5 jours ouvrables avant la date du premier examen, t2 = Teacher.objects.create( first_name='Jeanne', last_name='Dubois', abrev="JDU", email="jeanne@example.org" ) + self.client.login(username='me', password='mepassword') + response = self.client.get(reverse('candidate-validation', args=[henri.pk]), follow=True) + self.assertContains(response, "Aucun interview attribué à ce candidat pour l’instant") + inter = Interview.objects.create( date=datetime(2018, 3, 10, 10, 30), room='B103', candidat=henri, teacher_int=t1, teacher_file=t2, ) - self.client.login(username='me', password='mepassword') response = self.client.get(reverse('candidate-validation', args=[henri.pk])) expected_message = """Bonjour, diff --git a/candidats/views.py b/candidats/views.py index 3f70e84..03732f1 100644 --- a/candidats/views.py +++ b/candidats/views.py @@ -57,7 +57,7 @@ class ConfirmationView(EmailConfirmationBaseView): def get(self, request, *args, **kwargs): candidate = Candidate.objects.get(pk=self.kwargs['pk']) - if candidate.section != 'EDE' and not candidate.section.is_fe(): + if candidate.section != 'EDE' and not candidate.section in {'ASA', 'ASE', 'ASSC'}: messages.error(request, "Ce formulaire n'est disponible que pour les candidats EDE ou FE") elif candidate.confirmation_date: messages.error(request, 'Une confirmation a déjà été envoyée!') @@ -74,7 +74,7 @@ class ConfirmationView(EmailConfirmationBaseView): to = [candidate.email] if candidate.section == 'EDE': src_email = 'email/candidate_confirm_EDE.txt' - elif candidate.section.is_fe(): + elif candidate.section in {'ASA', 'ASE', 'ASSC'}: src_email = 'email/candidate_confirm_FE.txt' if candidate.corporation and candidate.corporation.email: to.append(candidate.corporation.email) @@ -106,6 +106,9 @@ class ValidationView(EmailConfirmationBaseView): if candidate.validation_date: messages.error(request, 'Une validation a déjà été envoyée!') return redirect(reverse("admin:candidats_candidate_change", args=(candidate.pk,))) + elif not candidate.has_interview: + messages.error(request, "Aucun interview attribué à ce candidat pour l’instant") + return redirect(reverse("admin:candidats_candidate_change", args=(candidate.pk,))) return super().get(request, *args, **kwargs) def get_initial(self): @@ -136,9 +139,7 @@ class ConvocationView(EmailConfirmationBaseView): def get(self, request, *args, **kwargs): candidate = Candidate.objects.get(pk=self.kwargs['pk']) - try: - candidate.interview - except Interview.DoesNotExist: + if not candidate.has_interview: messages.error(request, "Impossible de convoquer sans d'abord définir un interview!") return redirect(reverse("admin:candidats_candidate_change", args=(candidate.pk,))) return super().get(request, *args, **kwargs)