Added send_confirmation_mail admin action for candidates
This commit is contained in:
parent
71165d29b8
commit
086d9283c3
3 changed files with 104 additions and 1 deletions
|
|
@ -1,8 +1,11 @@
|
|||
from collections import OrderedDict
|
||||
from datetime import date
|
||||
|
||||
from django import forms
|
||||
from django.contrib import admin
|
||||
from django.core.mail import send_mail
|
||||
from django.db.models import BooleanField
|
||||
from django.template import loader
|
||||
|
||||
from stages.exports import OpenXMLExport
|
||||
from .models import Candidate, GENDER_CHOICES
|
||||
|
|
@ -36,6 +39,36 @@ def export_candidates(modeladmin, request, queryset):
|
|||
export_candidates.short_description = "Exporter les candidats sélectionnés"
|
||||
|
||||
|
||||
def send_confirmation_mail(modeladmin, request, queryset):
|
||||
from_email = request.user.email
|
||||
subject = "Confirmation de votre inscription à l'Ecole Santé-social Pierre-Coullery"
|
||||
|
||||
for candidate in queryset.filter(
|
||||
deposite_date__isnull=False, date_confirmation_mail__isnull=True, canceled_file=False):
|
||||
to = [candidate.email]
|
||||
if candidate.corporation and candidate.corporation.email:
|
||||
to.append(candidate.corporation.email)
|
||||
if candidate.instructor and candidate.instructor.email:
|
||||
to.append(candidate.instructor.email)
|
||||
|
||||
context = {
|
||||
'candidate_name': " ".join([candidate.civility, candidate.first_name, candidate.last_name]),
|
||||
'section': candidate.section,
|
||||
'sender_name': " ".join([request.user.first_name, request.user.last_name]),
|
||||
'sender_email': from_email,
|
||||
}
|
||||
body = loader.render_to_string('email/candidate_confirm.txt', context)
|
||||
try:
|
||||
send_mail(subject, body, from_email, to, fail_silently=False)
|
||||
except Exception as err:
|
||||
self.message_user(request, "Échec d'envoi pour le candidat {0} ({1})".format(candidate, err))
|
||||
else:
|
||||
candidate.date_confirmation_mail = date.today()
|
||||
candidate.save()
|
||||
|
||||
send_confirmation_mail.short_description = "Envoyer email de confirmation"
|
||||
|
||||
|
||||
class CandidateAdminForm(forms.ModelForm):
|
||||
class Meta:
|
||||
model = Candidate
|
||||
|
|
@ -51,7 +84,7 @@ class CandidateAdmin(admin.ModelAdmin):
|
|||
list_display = ('last_name', 'first_name', 'section', 'confirm_mail')
|
||||
list_filter = ('section', 'option')
|
||||
readonly_fields = ('total_result_points', 'total_result_mark', 'date_confirmation_mail')
|
||||
actions = [export_candidates]
|
||||
actions = [export_candidates, send_confirmation_mail]
|
||||
fieldsets = (
|
||||
(None, {
|
||||
'fields': (('first_name', 'last_name', 'gender'),
|
||||
|
|
|
|||
59
candidats/tests.py
Normal file
59
candidats/tests.py
Normal file
|
|
@ -0,0 +1,59 @@
|
|||
from datetime import date
|
||||
|
||||
from django.contrib.auth.models import User
|
||||
from django.core import mail
|
||||
from django.test import TestCase
|
||||
from django.urls import reverse
|
||||
|
||||
from stages.models import Section
|
||||
from .models import Candidate
|
||||
|
||||
|
||||
class CandidateTests(TestCase):
|
||||
@classmethod
|
||||
def setUpTestData(cls):
|
||||
User.objects.create_superuser(
|
||||
'me', 'me@example.org', 'mepassword', first_name='Hans', last_name='Schmid',
|
||||
)
|
||||
|
||||
def test_send_confirmation_mail(self):
|
||||
sect = Section.objects.create(name='EDE')
|
||||
Candidate.objects.bulk_create([
|
||||
# A mail should NOT be sent for those first 4
|
||||
Candidate(
|
||||
first_name='Sara', last_name='Hitz', gender='F', section=sect,
|
||||
deposite_date=None),
|
||||
Candidate(
|
||||
first_name='Jill', last_name='Simth', gender='F', section=sect,
|
||||
date_confirmation_mail=date.today()),
|
||||
Candidate(first_name='Hervé', last_name='Bern', gender='M', section=sect,
|
||||
canceled_file=True),
|
||||
Candidate(first_name='Frank', last_name='Pit', gender='M', section=sect, email=''),
|
||||
# Good
|
||||
Candidate(first_name='Joé', last_name='Glatz', gender='F', section=sect,
|
||||
email='joe@example.org', deposite_date=date.today()),
|
||||
Candidate(first_name='Henri', last_name='Dupond', gender='M', section=sect,
|
||||
email='henri@example.org', deposite_date=date.today()),
|
||||
])
|
||||
change_url = reverse('admin:candidats_candidate_changelist')
|
||||
self.client.login(username='me', password='mepassword')
|
||||
response = self.client.post(change_url, {
|
||||
'action': 'send_confirmation_mail',
|
||||
'_selected_action': Candidate.objects.values_list('pk', flat=True)
|
||||
}, follow=True)
|
||||
self.assertEqual(len(mail.outbox), 2)
|
||||
self.assertEqual(mail.outbox[0].recipients(), ['henri@example.org'])
|
||||
self.assertEqual(mail.outbox[1].recipients(), ['joe@example.org'])
|
||||
self.assertEqual(mail.outbox[0].body, """Madame, Monsieur,
|
||||
|
||||
Nous vous confirmons la bonne réception de l'inscription de Monsieur Henri Dupond dans la filière EDE pour l'année scolaire à venir.
|
||||
|
||||
Nous nous tenons à votre disposition pour tout renseignement complémentaire et vous prions de recevoir, Madame, Monsieur, nos salutations les plus cordiales.
|
||||
|
||||
Secrétariat de l'EPC
|
||||
tél. 032 886 33 00
|
||||
|
||||
Hans Schmid
|
||||
me@example.org
|
||||
""".format()
|
||||
)
|
||||
11
templates/email/candidate_confirm.txt
Normal file
11
templates/email/candidate_confirm.txt
Normal file
|
|
@ -0,0 +1,11 @@
|
|||
Madame, Monsieur,
|
||||
|
||||
Nous vous confirmons la bonne réception de l'inscription de {{ candidate_name }} dans la filière {{ section }} pour l'année scolaire à venir.
|
||||
|
||||
Nous nous tenons à votre disposition pour tout renseignement complémentaire et vous prions de recevoir, Madame, Monsieur, nos salutations les plus cordiales.
|
||||
|
||||
Secrétariat de l'EPC
|
||||
tél. 032 886 33 00
|
||||
|
||||
{{ sender_name }}
|
||||
{{ sender_email }}
|
||||
Loading…
Add table
Add a link
Reference in a new issue