From 5b398c223ef17b00f870dab904eae4f48c9143b3 Mon Sep 17 00:00:00 2001 From: Ju Luiselli Date: Thu, 13 Feb 2020 00:11:28 +0100 Subject: [PATCH] send mail upon modification --- annuaire/settings.py | 1 + fiches/apps.py | 2 +- fiches/forms.py | 14 +++---- fiches/templates/fiches/mail/mail_modif.txt | 6 +++ fiches/urls.py | 6 +-- fiches/views.py | 44 +++++++++++++-------- 6 files changed, 45 insertions(+), 28 deletions(-) create mode 100644 fiches/templates/fiches/mail/mail_modif.txt diff --git a/annuaire/settings.py b/annuaire/settings.py index 0501811..b102e1c 100644 --- a/annuaire/settings.py +++ b/annuaire/settings.py @@ -134,3 +134,4 @@ CAS_SERVER_URL = 'https://cas.eleves.ens.fr/' CAS_VERSION = "2" +EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend' diff --git a/fiches/apps.py b/fiches/apps.py index 929f294..45d45ef 100644 --- a/fiches/apps.py +++ b/fiches/apps.py @@ -2,4 +2,4 @@ from django.apps import AppConfig class FichesConfig(AppConfig): - name = 'fiches' + name = "fiches" diff --git a/fiches/forms.py b/fiches/forms.py index 89ab3c4..934f624 100644 --- a/fiches/forms.py +++ b/fiches/forms.py @@ -15,13 +15,13 @@ class ProfileForm(forms.ModelForm): "thurne", "text_field", "printing", - "keep_me" + "keep_me", ] class SearchForm(forms.Form): - name = forms.CharField(label='Nom/Surnom', max_length=1023, required=False) - year = forms.IntegerField(label='Promotion', required=False) + name = forms.CharField(label="Nom/Surnom", max_length=1023, required=False) + year = forms.IntegerField(label="Promotion", required=False) department = forms.ModelMultipleChoiceField( queryset=Department.objects.all(), required=False ) @@ -29,8 +29,8 @@ class SearchForm(forms.Form): def clean(self): cleaned_data = super().clean() if ( - not cleaned_data['name'] - and not cleaned_data['year'] - and not cleaned_data['department'] + not cleaned_data["name"] + and not cleaned_data["year"] + and not cleaned_data["department"] ): - raise forms.ValidationError(('Tous les champs sont vides'), code='invalid') + raise forms.ValidationError(("Tous les champs sont vides"), code="invalid") diff --git a/fiches/templates/fiches/mail/mail_modif.txt b/fiches/templates/fiches/mail/mail_modif.txt new file mode 100644 index 0000000..0d16f2f --- /dev/null +++ b/fiches/templates/fiches/mail/mail_modif.txt @@ -0,0 +1,6 @@ +Bonjour {{profile.full_name}}, + +Ta fiche annuaire a été modifiée ! + +Cordialement, +le Klub Dev \ No newline at end of file diff --git a/fiches/urls.py b/fiches/urls.py index 918c628..d9a7510 100644 --- a/fiches/urls.py +++ b/fiches/urls.py @@ -2,6 +2,6 @@ from django.urls import path from . import views urlpatterns = [ - path('',views.fiche, name='fiche'), - path('edit',views.fiche_modif, name='fiche_modif') - ] + path("", views.fiche, name="fiche"), + path("edit", views.fiche_modif, name="fiche_modif"), +] diff --git a/fiches/views.py b/fiches/views.py index 6178a14..4d513a3 100644 --- a/fiches/views.py +++ b/fiches/views.py @@ -7,54 +7,64 @@ from django.urls import reverse from django.db.models import Q from django.utils import timezone from datetime import timedelta +from django.core.mail import send_mail +from django.template.loader import render_to_string @login_required def fiche(request, id): profile = get_object_or_404(Profile, id=id) - return render(request, 'fiches/fiche.html', {"profile": profile}) + return render(request, "fiches/fiche.html", {"profile": profile}) @login_required def fiche_modif(request): profile = request.user.profile - if request.method == 'POST': + if request.method == "POST": form = ProfileForm(request.POST, instance=profile) if form.is_valid(): form.save() - return redirect(reverse('fiche', args=(profile.id,))) + send_mail( + "Fiche annuaire modifée", + render_to_string("fiches/mail/mail_modif.txt", {"profile": profile}), + "klub-dev@ens.psl.eu", + ["{}@clipper.ens.psl.eu".format(request.user.username)], + fail_silently=False, + ) + return redirect(reverse("fiche", args=(profile.id,))) + else: form = ProfileForm(instance=profile) - return render(request, 'fiches/fiches_modif.html', {"form": form}) + return render(request, "fiches/fiches_modif.html", {"form": form}) @login_required def home(request): - if request.method == 'POST': + if request.method == "POST": form = SearchForm(request.POST) if form.is_valid(): result = Profile.objects.filter( - Q(full_name__icontains=form.cleaned_data['name']) - | Q(nickname__icontains=form.cleaned_data['name']) - ) - return render( - request, 'fiches/home.html', {"form": form, "result": result} + Q(full_name__icontains=form.cleaned_data["name"]) + | Q(nickname__icontains=form.cleaned_data["name"]) ) + return render(request, "fiches/home.html", {"form": form, "result": result}) else: form = SearchForm() - return render(request, 'fiches/home.html', {"form": form}) + return render(request, "fiches/home.html", {"form": form}) @login_required def birthday(request): today = timezone.now() - result = list(Profile.objects.filter( - birth_date__day=today.day, birth_date__month=today.month - )) + result = list( + Profile.objects.filter(birth_date__day=today.day, birth_date__month=today.month) + ) for i in range(1, 7): today = today + timedelta(days=1) - result += list(Profile.objects.filter( - birth_date__day=today.day, birth_date__month=today.month) + result += list( + Profile.objects.filter( + birth_date__day=today.day, birth_date__month=today.month + ) ) - return render(request, 'fiches/birthday.html', {"result": result}) + return render(request, "fiches/birthday.html", {"result": result})