send mail upon modification

This commit is contained in:
Ju Luiselli 2020-02-13 00:11:28 +01:00
parent 1cf12421f0
commit 5b398c223e
6 changed files with 45 additions and 28 deletions

View file

@ -134,3 +134,4 @@ CAS_SERVER_URL = 'https://cas.eleves.ens.fr/'
CAS_VERSION = "2" CAS_VERSION = "2"
EMAIL_BACKEND = 'django.core.mail.backends.console.EmailBackend'

View file

@ -2,4 +2,4 @@ from django.apps import AppConfig
class FichesConfig(AppConfig): class FichesConfig(AppConfig):
name = 'fiches' name = "fiches"

View file

@ -15,13 +15,13 @@ class ProfileForm(forms.ModelForm):
"thurne", "thurne",
"text_field", "text_field",
"printing", "printing",
"keep_me" "keep_me",
] ]
class SearchForm(forms.Form): class SearchForm(forms.Form):
name = forms.CharField(label='Nom/Surnom', max_length=1023, required=False) name = forms.CharField(label="Nom/Surnom", max_length=1023, required=False)
year = forms.IntegerField(label='Promotion', required=False) year = forms.IntegerField(label="Promotion", required=False)
department = forms.ModelMultipleChoiceField( department = forms.ModelMultipleChoiceField(
queryset=Department.objects.all(), required=False queryset=Department.objects.all(), required=False
) )
@ -29,8 +29,8 @@ class SearchForm(forms.Form):
def clean(self): def clean(self):
cleaned_data = super().clean() cleaned_data = super().clean()
if ( if (
not cleaned_data['name'] not cleaned_data["name"]
and not cleaned_data['year'] and not cleaned_data["year"]
and not cleaned_data['department'] 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")

View file

@ -0,0 +1,6 @@
Bonjour {{profile.full_name}},
Ta fiche annuaire a été modifiée !
Cordialement,
le Klub Dev

View file

@ -2,6 +2,6 @@ from django.urls import path
from . import views from . import views
urlpatterns = [ urlpatterns = [
path('<int:id>',views.fiche, name='fiche'), path("<int:id>", views.fiche, name="fiche"),
path('edit',views.fiche_modif, name='fiche_modif') path("edit", views.fiche_modif, name="fiche_modif"),
] ]

View file

@ -7,54 +7,64 @@ from django.urls import reverse
from django.db.models import Q from django.db.models import Q
from django.utils import timezone from django.utils import timezone
from datetime import timedelta from datetime import timedelta
from django.core.mail import send_mail
from django.template.loader import render_to_string
@login_required @login_required
def fiche(request, id): def fiche(request, id):
profile = get_object_or_404(Profile, id=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 @login_required
def fiche_modif(request): def fiche_modif(request):
profile = request.user.profile profile = request.user.profile
if request.method == 'POST': if request.method == "POST":
form = ProfileForm(request.POST, instance=profile) form = ProfileForm(request.POST, instance=profile)
if form.is_valid(): if form.is_valid():
form.save() 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: else:
form = ProfileForm(instance=profile) form = ProfileForm(instance=profile)
return render(request, 'fiches/fiches_modif.html', {"form": form})
return render(request, "fiches/fiches_modif.html", {"form": form})
@login_required @login_required
def home(request): def home(request):
if request.method == 'POST': if request.method == "POST":
form = SearchForm(request.POST) form = SearchForm(request.POST)
if form.is_valid(): if form.is_valid():
result = Profile.objects.filter( result = Profile.objects.filter(
Q(full_name__icontains=form.cleaned_data['name']) Q(full_name__icontains=form.cleaned_data["name"])
| Q(nickname__icontains=form.cleaned_data['name']) | Q(nickname__icontains=form.cleaned_data["name"])
)
return render(
request, 'fiches/home.html', {"form": form, "result": result}
) )
return render(request, "fiches/home.html", {"form": form, "result": result})
else: else:
form = SearchForm() form = SearchForm()
return render(request, 'fiches/home.html', {"form": form}) return render(request, "fiches/home.html", {"form": form})
@login_required @login_required
def birthday(request): def birthday(request):
today = timezone.now() today = timezone.now()
result = list(Profile.objects.filter( result = list(
birth_date__day=today.day, birth_date__month=today.month Profile.objects.filter(birth_date__day=today.day, birth_date__month=today.month)
)) )
for i in range(1, 7): for i in range(1, 7):
today = today + timedelta(days=1) today = today + timedelta(days=1)
result += list(Profile.objects.filter( result += list(
birth_date__day=today.day, birth_date__month=today.month) 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})