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
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",
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue