Move email confirmation base view to stages.base_views
This commit is contained in:
parent
4c39e07867
commit
8225c48da5
6 changed files with 63 additions and 57 deletions
|
|
@ -30,12 +30,3 @@ class CandidateForm(forms.ModelForm):
|
|||
self.cleaned_data['interview'].candidat = obj
|
||||
self.cleaned_data['interview'].save()
|
||||
return obj
|
||||
|
||||
|
||||
class EmailBaseForm(forms.Form):
|
||||
id_candidate = forms.CharField(widget=forms.HiddenInput())
|
||||
sender = forms.CharField(widget=forms.HiddenInput())
|
||||
to = forms.CharField(widget=forms.TextInput(attrs={'size': '60'}))
|
||||
cci = forms.CharField(widget=forms.TextInput(attrs={'size': '60'}))
|
||||
subject = forms.CharField(widget=forms.TextInput(attrs={'size': '60'}))
|
||||
message = forms.CharField(widget=forms.Textarea(attrs={'rows': 25, 'cols': 120}))
|
||||
|
|
|
|||
|
|
@ -2,56 +2,30 @@ 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, reverse_lazy
|
||||
from django.utils import timezone
|
||||
from django.views.generic import FormView
|
||||
|
||||
from candidats.forms import EmailBaseForm
|
||||
from stages.base_views import EmailConfirmationBaseView
|
||||
from candidats.models import Candidate, Interview
|
||||
from .pdf import InscriptionSummaryPDF
|
||||
|
||||
|
||||
class EmailConfirmationBaseView(FormView):
|
||||
template_name = 'email_base.html'
|
||||
form_class = EmailBaseForm
|
||||
class CandidateConfirmationView(EmailConfirmationBaseView):
|
||||
person_model = Candidate
|
||||
success_url = reverse_lazy('admin:candidats_candidate_changelist')
|
||||
success_message = "Le message a été envoyé pour le candidat {candidate}"
|
||||
error_message = "Échec d’envoi pour le candidat {person} ({err})"
|
||||
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
|
||||
def on_success(self, candidate):
|
||||
setattr(candidate, self.candidate_date_field, timezone.now())
|
||||
candidate.save()
|
||||
|
||||
|
||||
class ConfirmationView(EmailConfirmationBaseView):
|
||||
success_message = "Le message de confirmation a été envoyé pour le candidat {candidate}"
|
||||
class ConfirmationView(CandidateConfirmationView):
|
||||
success_message = "Le message de confirmation a été envoyé pour le candidat {person}"
|
||||
candidate_date_field = 'confirmation_date'
|
||||
title = "Confirmation de réception de dossier"
|
||||
|
||||
|
|
@ -86,7 +60,6 @@ class ConfirmationView(EmailConfirmationBaseView):
|
|||
'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),
|
||||
|
|
@ -96,8 +69,8 @@ class ConfirmationView(EmailConfirmationBaseView):
|
|||
return initial
|
||||
|
||||
|
||||
class ValidationView(EmailConfirmationBaseView):
|
||||
success_message = "Le message de validation a été envoyé pour le candidat {candidate}"
|
||||
class ValidationView(CandidateConfirmationView):
|
||||
success_message = "Le message de validation a été envoyé pour le candidat {person}"
|
||||
candidate_date_field = 'validation_date'
|
||||
title = "Validation des examens par les enseignant-e-s EDE"
|
||||
|
||||
|
|
@ -120,7 +93,6 @@ class ValidationView(EmailConfirmationBaseView):
|
|||
'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
|
||||
|
|
@ -132,8 +104,8 @@ class ValidationView(EmailConfirmationBaseView):
|
|||
return initial
|
||||
|
||||
|
||||
class ConvocationView(EmailConfirmationBaseView):
|
||||
success_message = "Le message de convocation a été envoyé pour le candidat {candidate}"
|
||||
class ConvocationView(CandidateConfirmationView):
|
||||
success_message = "Le message de convocation a été envoyé pour le candidat {person}"
|
||||
candidate_date_field = 'convocation_date'
|
||||
title = "Convocation aux examens d'admission EDE"
|
||||
|
||||
|
|
@ -180,7 +152,6 @@ class ConvocationView(EmailConfirmationBaseView):
|
|||
msg_context['rappel'] = loader.render_to_string('email/rappel_document_EDE.txt', missing_documents)
|
||||
|
||||
initial.update({
|
||||
'id_candidate': candidate.pk,
|
||||
'cci': self.request.user.email,
|
||||
'to': candidate.email,
|
||||
'subject': "Procédure d'admission",
|
||||
|
|
|
|||
46
stages/base_views.py
Normal file
46
stages/base_views.py
Normal file
|
|
@ -0,0 +1,46 @@
|
|||
from django.contrib import messages
|
||||
from django.core.mail import EmailMessage
|
||||
from django.urls import reverse_lazy
|
||||
from django.views.generic import FormView
|
||||
|
||||
from stages.forms import EmailBaseForm
|
||||
|
||||
|
||||
class EmailConfirmationBaseView(FormView):
|
||||
template_name = 'email_base.html'
|
||||
form_class = EmailBaseForm
|
||||
title = ''
|
||||
person_model = None # To be defined on subclasses
|
||||
success_url = reverse_lazy('admin:candidats_candidate_changelist')
|
||||
success_message = "Le message a été envoyé pour {person}"
|
||||
error_message = "Échec d’envoi pour {person} ({err})"
|
||||
|
||||
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(';'),
|
||||
)
|
||||
person = self.person_model.objects.get(pk=self.kwargs['pk'])
|
||||
try:
|
||||
email.send()
|
||||
except Exception as err:
|
||||
messages.error(self.request, self.error_message.format(person=person, err=err))
|
||||
else:
|
||||
self.on_success(person)
|
||||
messages.success(self.request, self.success_message.format(person=person))
|
||||
return super().form_valid(form)
|
||||
|
||||
def on_success(self, person):
|
||||
"""Operation to apply if message is successfully sent."""
|
||||
raise NotImplementedError("You should define an on_success method in your view")
|
||||
|
||||
def get_context_data(self, **kwargs):
|
||||
context = super().get_context_data(**kwargs)
|
||||
context.update({
|
||||
'person': self.person_model.objects.get(pk=self.kwargs['pk']),
|
||||
'title': self.title,
|
||||
})
|
||||
return context
|
||||
|
|
@ -41,8 +41,7 @@ class UploadReportForm(forms.Form):
|
|||
upload = forms.FileField(label='Bulletins CLOEE (pdf)')
|
||||
|
||||
|
||||
class EmailStudentBaseForm(forms.Form):
|
||||
id_student = forms.CharField(widget=forms.HiddenInput())
|
||||
class EmailBaseForm(forms.Form):
|
||||
sender = forms.CharField(widget=forms.HiddenInput())
|
||||
to = forms.CharField(widget=forms.TextInput(attrs={'size': '60'}))
|
||||
cci = forms.CharField(widget=forms.TextInput(attrs={'size': '60'}))
|
||||
|
|
|
|||
|
|
@ -27,7 +27,7 @@ from django.utils.text import slugify
|
|||
from django.views.generic import DetailView, FormView, TemplateView, ListView
|
||||
|
||||
from .exports import OpenXMLExport
|
||||
from .forms import EmailStudentBaseForm, PeriodForm, StudentImportForm, UploadHPFileForm, UploadReportForm
|
||||
from .forms import EmailBaseForm, PeriodForm, StudentImportForm, UploadHPFileForm, UploadReportForm
|
||||
from .models import (
|
||||
Klass, Section, Option, Student, Teacher, Corporation, CorpContact, Course, Period,
|
||||
Training, Availability,
|
||||
|
|
@ -596,7 +596,7 @@ class ImportReportsView(FormView):
|
|||
|
||||
class SendStudentReportsView(FormView):
|
||||
template_name = 'email_report.html'
|
||||
form_class = EmailStudentBaseForm
|
||||
form_class = EmailBaseForm
|
||||
|
||||
def get_initial(self):
|
||||
initial = super().get_initial()
|
||||
|
|
@ -613,7 +613,6 @@ class SendStudentReportsView(FormView):
|
|||
}
|
||||
|
||||
initial.update({
|
||||
'id_student': self.student.pk,
|
||||
'cci': self.request.user.email,
|
||||
'to': '; '.join(to),
|
||||
'subject': "Bulletin semestriel",
|
||||
|
|
|
|||
|
|
@ -9,7 +9,7 @@
|
|||
{% endblock %}
|
||||
|
||||
{% block content %}
|
||||
<h2>{{ candidat }}</h2>
|
||||
<h2>{{ person }}</h2>
|
||||
<form name="confirmation" action="." method="post">{% csrf_token %}
|
||||
<table>
|
||||
{{ form.as_table }}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue