2019-10-04 15:36:55 +02:00
|
|
|
|
import io
|
2018-06-16 19:57:27 +02:00
|
|
|
|
import os
|
|
|
|
|
|
import tempfile
|
|
|
|
|
|
import zipfile
|
|
|
|
|
|
|
2018-04-12 11:01:45 +02:00
|
|
|
|
from django.contrib import messages
|
|
|
|
|
|
from django.core.mail import EmailMessage
|
2019-10-04 15:36:55 +02:00
|
|
|
|
from django.http import FileResponse, HttpResponse
|
2018-04-12 11:01:45 +02:00
|
|
|
|
from django.urls import reverse_lazy
|
2018-06-16 19:57:27 +02:00
|
|
|
|
from django.views.generic import FormView, View
|
2018-04-12 11:01:45 +02:00
|
|
|
|
|
|
|
|
|
|
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})"
|
|
|
|
|
|
|
2020-04-21 15:29:28 +02:00
|
|
|
|
def get_person(self):
|
|
|
|
|
|
return self.person_model.objects.get(pk=self.kwargs['pk'])
|
|
|
|
|
|
|
2018-04-12 11:01:45 +02:00
|
|
|
|
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(';'),
|
|
|
|
|
|
)
|
2020-04-21 15:29:28 +02:00
|
|
|
|
person = self.get_person()
|
2018-04-12 11:01:45 +02:00
|
|
|
|
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({
|
2020-04-21 15:29:28 +02:00
|
|
|
|
'person': self.get_person(),
|
2018-04-12 11:01:45 +02:00
|
|
|
|
'title': self.title,
|
|
|
|
|
|
})
|
|
|
|
|
|
return context
|
2018-06-16 19:57:27 +02:00
|
|
|
|
|
|
|
|
|
|
|
2019-10-04 15:36:55 +02:00
|
|
|
|
class PDFBaseView(View):
|
|
|
|
|
|
pdf_class = None
|
|
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
|
obj = self.get_object()
|
|
|
|
|
|
buff = io.BytesIO()
|
|
|
|
|
|
pdf = self.pdf_class(buff, obj)
|
|
|
|
|
|
pdf.produce()
|
|
|
|
|
|
buff.seek(0)
|
|
|
|
|
|
return FileResponse(buff, as_attachment=True, filename=self.filename(obj))
|
|
|
|
|
|
|
|
|
|
|
|
|
2018-06-16 19:57:27 +02:00
|
|
|
|
class ZippedFilesBaseView(View):
|
|
|
|
|
|
"""A base class to return a .zip file containing a compressed list of files."""
|
|
|
|
|
|
filename = 'to_be_defined.zip'
|
|
|
|
|
|
|
|
|
|
|
|
def generate_files(self):
|
2019-10-04 14:38:09 +02:00
|
|
|
|
"""Generator yielding (file_name, file_data) tuples."""
|
2018-06-16 19:57:27 +02:00
|
|
|
|
raise NotImplementedError()
|
|
|
|
|
|
|
|
|
|
|
|
def get(self, request, *args, **kwargs):
|
|
|
|
|
|
tmp_file = tempfile.NamedTemporaryFile()
|
|
|
|
|
|
with zipfile.ZipFile(tmp_file, mode='w', compression=zipfile.ZIP_DEFLATED) as filezip:
|
2019-10-04 14:38:09 +02:00
|
|
|
|
for file_name, file_data in self.generate_files():
|
|
|
|
|
|
filezip.writestr(file_name, file_data)
|
2018-06-16 19:57:27 +02:00
|
|
|
|
|
|
|
|
|
|
with open(filezip.filename, mode='rb') as fh:
|
|
|
|
|
|
response = HttpResponse(fh.read(), content_type='application/zip')
|
|
|
|
|
|
response['Content-Disposition'] = 'attachment; filename="%s"' % self.filename
|
|
|
|
|
|
return response
|