Add confirmation/validation/convocation views
This commit is contained in:
parent
5a2a5d769f
commit
e62284d55c
8 changed files with 312 additions and 159 deletions
|
|
@ -1,19 +1,145 @@
|
|||
import os
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib import messages
|
||||
from django.core.mail import EmailMessage
|
||||
from django.http import HttpResponse
|
||||
from django.shortcuts import redirect
|
||||
from django.template import loader
|
||||
from django.urls import reverse_lazy
|
||||
from django.urls import reverse, reverse_lazy
|
||||
from django.utils import timezone
|
||||
from django.views.generic import FormView
|
||||
|
||||
from candidats.forms import EmailBaseForm
|
||||
from candidats.models import Candidate
|
||||
from candidats.models import Candidate, Interview
|
||||
from .pdf import InscriptionSummaryPDF
|
||||
|
||||
|
||||
class SendConvocationView(FormView):
|
||||
class EmailConfirmationBaseView(FormView):
|
||||
template_name = 'email_base.html'
|
||||
form_class = EmailBaseForm
|
||||
success_url = reverse_lazy('admin:candidats_candidate_changelist')
|
||||
success_message = "Le message a été envoyé pour le candidat {candidate}"
|
||||
candidate_date_field = None
|
||||
|
||||
def form_valid(self, form):
|
||||
email = EmailMessage(
|
||||
subject=form.cleaned_data['subject'],
|
||||
body=form.cleaned_data['message'],
|
||||
from_email=form.cleaned_data['sender'],
|
||||
to=form.cleaned_data['to'].split(';'),
|
||||
bcc=form.cleaned_data['cci'].split(';'),
|
||||
)
|
||||
candidate = Candidate.objects.get(pk=self.kwargs['pk'])
|
||||
try:
|
||||
email.send()
|
||||
except Exception as err:
|
||||
messages.error(self.request, "Échec d’envoi pour le candidat {0} ({1})".format(candidate, err))
|
||||
else:
|
||||
setattr(candidate, self.candidate_date_field, timezone.now())
|
||||
candidate.save()
|
||||
messages.success(self.request, self.success_message.format(candidate=candidate))
|
||||
return super().form_valid(form)
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context.update({
|
||||
'candidat': Candidate.objects.get(pk=self.kwargs['pk']),
|
||||
'title': self.title,
|
||||
})
|
||||
return context
|
||||
|
||||
|
||||
class ConfirmationView(EmailConfirmationBaseView):
|
||||
success_message = "Le message de confirmation a été envoyé pour le candidat {candidate}"
|
||||
candidate_date_field = 'confirmation_date'
|
||||
title = "Confirmation de réception de dossier"
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
candidate = Candidate.objects.get(pk=self.kwargs['pk'])
|
||||
if candidate.confirmation_date:
|
||||
messages.error(request, 'Une confirmation a déjà été envoyée!')
|
||||
return redirect(reverse("admin:candidats_candidate_change", args=(candidate.pk,)))
|
||||
elif candidate.canceled_file:
|
||||
messages.error(request, 'Ce dossier a été annulé!')
|
||||
return redirect(reverse("admin:candidats_candidate_change", args=(candidate.pk,)))
|
||||
return super().get(request, *args, **kwargs)
|
||||
|
||||
def get_initial(self):
|
||||
initial = super().get_initial()
|
||||
candidate = Candidate.objects.get(pk=self.kwargs['pk'])
|
||||
|
||||
to = [candidate.email]
|
||||
if candidate.section == 'EDE':
|
||||
src_email = 'email/candidate_confirm_EDE.txt'
|
||||
else:
|
||||
src_email = 'email/candidate_confirm_FE.txt'
|
||||
if candidate.corporation and candidate.corporation.email:
|
||||
to.append(candidate.corporation.email)
|
||||
if candidate.instructor and candidate.instructor.email:
|
||||
to.append(candidate.instructor.email)
|
||||
|
||||
msg_context = {
|
||||
'candidate': candidate,
|
||||
'sender': self.request.user,
|
||||
}
|
||||
initial.update({
|
||||
'id_candidate': candidate.pk,
|
||||
'cci': self.request.user.email,
|
||||
'to': '; '.join(to),
|
||||
'subject': "Inscription à la formation {0}".format(candidate.section_option),
|
||||
'message': loader.render_to_string(src_email, msg_context),
|
||||
'sender': self.request.user.email,
|
||||
})
|
||||
return initial
|
||||
|
||||
|
||||
class ValidationView(EmailConfirmationBaseView):
|
||||
success_message = "Le message de validation a été envoyé pour le candidat {candidate}"
|
||||
candidate_date_field = 'validation_date'
|
||||
title = "Validation des examens par les enseignant-e-s EDE"
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
candidate = Candidate.objects.get(pk=self.kwargs['pk'])
|
||||
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,)))
|
||||
return super().get(request, *args, **kwargs)
|
||||
|
||||
def get_initial(self):
|
||||
initial = super().get_initial()
|
||||
candidate = Candidate.objects.get(pk=self.kwargs['pk'])
|
||||
|
||||
msg_context = {
|
||||
'candidate': candidate,
|
||||
'sender': self.request.user,
|
||||
}
|
||||
initial.update({
|
||||
'id_candidate': candidate.pk,
|
||||
'cci': self.request.user.email,
|
||||
'to': ';'.join([
|
||||
candidate.interview.teacher_int.email, candidate.interview.teacher_file.email
|
||||
]),
|
||||
'subject': "Validation de l'entretien d'admission",
|
||||
'message': loader.render_to_string('email/validation_enseignant_EDE.txt', msg_context),
|
||||
'sender': self.request.user.email,
|
||||
})
|
||||
return initial
|
||||
|
||||
|
||||
class ConvocationView(EmailConfirmationBaseView):
|
||||
success_message = "Le message de convocation a été envoyé pour le candidat {candidate}"
|
||||
candidate_date_field = 'convocation_date'
|
||||
title = "Convocation aux examens d'admission EDE"
|
||||
|
||||
def get(self, request, *args, **kwargs):
|
||||
candidate = Candidate.objects.get(pk=self.kwargs['pk'])
|
||||
try:
|
||||
candidate.interview
|
||||
except Interview.DoesNotExist:
|
||||
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)
|
||||
|
||||
def get_initial(self):
|
||||
initial = super().get_initial()
|
||||
|
|
@ -59,31 +185,16 @@ class SendConvocationView(FormView):
|
|||
})
|
||||
return initial
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context.update({
|
||||
'candidat': Candidate.objects.get(pk=self.kwargs['pk']),
|
||||
'title': "Convocation aux examens d'admission EDE",
|
||||
})
|
||||
return context
|
||||
|
||||
def form_valid(self, form):
|
||||
email = EmailMessage(
|
||||
subject=form.cleaned_data['subject'],
|
||||
body=form.cleaned_data['message'],
|
||||
from_email=form.cleaned_data['sender'],
|
||||
to=form.cleaned_data['to'].split(';'),
|
||||
bcc=form.cleaned_data['cci'].split(';'),
|
||||
)
|
||||
candidate = Candidate.objects.get(pk=self.kwargs['pk'])
|
||||
try:
|
||||
email.send()
|
||||
except Exception as err:
|
||||
messages.error(self.request, "Échec d’envoi pour le candidat {0} ({1})".format(candidate, err))
|
||||
else:
|
||||
candidate.convocation_date = timezone.now()
|
||||
candidate.save()
|
||||
messages.success(self.request,
|
||||
"Le message de convocation a été envoyé pour le candidat {0}".format(candidate)
|
||||
)
|
||||
return super().form_valid(form)
|
||||
def inscription_summary(request, pk):
|
||||
"""
|
||||
Print a PDF summary of inscription
|
||||
"""
|
||||
candidat = Candidate.objects.get(pk=pk)
|
||||
pdf = InscriptionSummaryPDF(candidat)
|
||||
pdf.produce(candidat)
|
||||
|
||||
with open(pdf.filename, mode='rb') as fh:
|
||||
response = HttpResponse(fh.read(), content_type='application/pdf')
|
||||
response['Content-Disposition'] = 'attachment; filename="{0}"'.format(os.path.basename(pdf.filename))
|
||||
return response
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue