2018-01-26 18:19:16 +01:00
|
|
|
|
from django.conf import settings
|
|
|
|
|
|
from django.contrib import messages
|
|
|
|
|
|
from django.core.mail import EmailMessage
|
|
|
|
|
|
from django.template import loader
|
|
|
|
|
|
from django.urls import reverse_lazy
|
|
|
|
|
|
from django.utils import timezone
|
|
|
|
|
|
from django.views.generic import FormView
|
|
|
|
|
|
|
2018-02-01 14:39:40 +01:00
|
|
|
|
from candidats.forms import EmailBaseForm
|
2018-01-26 18:19:16 +01:00
|
|
|
|
from candidats.models import Candidate
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
class SendConvocationView(FormView):
|
2018-02-01 14:39:40 +01:00
|
|
|
|
template_name = 'email_base.html'
|
|
|
|
|
|
form_class = EmailBaseForm
|
2018-01-26 18:19:16 +01:00
|
|
|
|
success_url = reverse_lazy('admin:candidats_candidate_changelist')
|
|
|
|
|
|
|
2018-02-01 14:39:40 +01:00
|
|
|
|
def get_initial(self):
|
|
|
|
|
|
initial = super().get_initial()
|
2018-01-26 18:19:16 +01:00
|
|
|
|
candidate = Candidate.objects.get(pk=self.kwargs['pk'])
|
2018-02-01 10:47:25 +01:00
|
|
|
|
# Define required documents depending on candidate diploma
|
|
|
|
|
|
common_docs = [
|
2018-01-26 18:19:16 +01:00
|
|
|
|
'registration_form', 'certificate_of_payement', 'police_record', 'cv', 'reflexive_text',
|
2018-02-01 10:47:25 +01:00
|
|
|
|
'has_photo', 'marks_certificate',
|
2018-01-26 18:19:16 +01:00
|
|
|
|
]
|
2018-02-01 10:47:25 +01:00
|
|
|
|
dipl_docs = {
|
|
|
|
|
|
0: [],
|
|
|
|
|
|
1: ['work_certificate'], # CFC ASE
|
|
|
|
|
|
2: ['certif_of_800_childhood', 'work_certificate'],
|
|
|
|
|
|
3: ['certif_of_800_general', 'certif_of_800_childhood', 'work_certificate'],
|
|
|
|
|
|
4: ['certif_of_800_general', 'certif_of_800_childhood', 'work_certificate'],
|
|
|
|
|
|
}[candidate.diploma]
|
|
|
|
|
|
docs_required = dipl_docs + common_docs
|
2018-01-26 18:19:16 +01:00
|
|
|
|
|
|
|
|
|
|
missing_documents = {'documents': ', '.join([
|
2018-02-01 10:47:25 +01:00
|
|
|
|
Candidate._meta.get_field(doc).verbose_name for doc in docs_required
|
|
|
|
|
|
if not getattr(candidate, doc)
|
2018-01-26 18:19:16 +01:00
|
|
|
|
])}
|
|
|
|
|
|
|
|
|
|
|
|
msg_context = {
|
|
|
|
|
|
'candidate_name': " ".join([candidate.civility, candidate.first_name, candidate.last_name]),
|
|
|
|
|
|
'candidate_civility': candidate.civility,
|
2018-02-01 10:47:25 +01:00
|
|
|
|
'option': candidate.get_option_display(),
|
2018-01-26 18:19:16 +01:00
|
|
|
|
'date_lieu_examen': settings.DATE_LIEU_EXAMEN_EDE,
|
|
|
|
|
|
'date_entretien': candidate.interview.date_formatted,
|
|
|
|
|
|
'salle_entretien': candidate.interview.room,
|
|
|
|
|
|
'rappel': loader.render_to_string('email/rappel_document_EDE.txt', missing_documents),
|
|
|
|
|
|
'sender_name': " ".join([self.request.user.first_name, self.request.user.last_name]),
|
|
|
|
|
|
'sender_email': self.request.user.email,
|
|
|
|
|
|
}
|
|
|
|
|
|
|
2018-02-01 14:39:40 +01:00
|
|
|
|
initial.update({
|
2018-01-26 18:19:16 +01:00
|
|
|
|
'id_candidate': candidate.pk,
|
|
|
|
|
|
'cci': self.request.user.email,
|
|
|
|
|
|
'to': candidate.email,
|
|
|
|
|
|
'subject': "Procédure de qualification",
|
|
|
|
|
|
'message': loader.render_to_string('email/candidate_convocation_EDE.txt', msg_context),
|
|
|
|
|
|
'sender': self.request.user.email,
|
|
|
|
|
|
})
|
2018-02-01 14:39:40 +01:00
|
|
|
|
return initial
|
|
|
|
|
|
|
|
|
|
|
|
def get_context_data(self, **kwargs):
|
|
|
|
|
|
context = super().get_context_data(**kwargs)
|
2018-01-26 18:19:16 +01:00
|
|
|
|
context.update({
|
2018-02-01 14:39:40 +01:00
|
|
|
|
'candidat': Candidate.objects.get(pk=self.kwargs['pk']),
|
|
|
|
|
|
'title': "Convocation aux examens d'admission EDE",
|
2018-01-26 18:19:16 +01:00
|
|
|
|
})
|
|
|
|
|
|
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)
|