fix captcha and add email sending

This commit is contained in:
Mathieu Pasquet 2021-04-18 18:00:24 +02:00
parent 44a56385f8
commit 75e2e800b6
2 changed files with 83 additions and 3 deletions

View File

@ -162,6 +162,8 @@ class AdhesionForm(forms.Form):
def reset_captcha(self, captcha_id: int, question: str): def reset_captcha(self, captcha_id: int, question: str):
self.initial['captcha_id'] = captcha_id self.initial['captcha_id'] = captcha_id
self.fields['captcha_id'].initial = captcha_id self.fields['captcha_id'].initial = captcha_id
self.initial['captcha'] = ''
self.fields['captcha'].initial = ''
self.fields['captcha'].help_text = mark_safe( self.fields['captcha'].help_text = mark_safe(
f'\n<br/>Répondez à cette question: {question}' f'\n<br/>Répondez à cette question: {question}'
) )
@ -171,3 +173,19 @@ class AdhesionForm(forms.Form):
if not validate_captcha(cleaned_data): if not validate_captcha(cleaned_data):
self.add_error('captcha', 'Êtes-vous un robot?') self.add_error('captcha', 'Êtes-vous un robot?')
self.reset_captcha(*pick_captcha()) self.reset_captcha(*pick_captcha())
def reset_captcha_form(form: forms.Form):
form_data = {key: value[0] for key, value in dict(form.data).items()}
form_data.pop('captcha', None)
form_data.pop('captcha_id', None)
cap_id, question = pick_captcha()
form_data['captcha_id'] = str(cap_id)
new_form = AdhesionForm(
data=form_data, initial=form.initial
)
new_form.fields['captcha'].help_text = mark_safe(
f'\n<br/>Répondez à cette question: {question}'
)
new_form._errors = form._errors
return new_form

View File

@ -1,7 +1,13 @@
import logging import logging
from aiohttp import ClientError from aiohttp import ClientError
from django.shortcuts import render from django.shortcuts import render
from jabberfr.forms import HebergForm, InscriptionForm, AdhesionForm from django.core.mail import send_mail
from jabberfr.forms import (
HebergForm,
InscriptionForm,
AdhesionForm,
reset_captcha_form,
)
from jabberfr.utils import ( from jabberfr.utils import (
get_chatrooms, get_chatrooms,
get_custom_domains, get_custom_domains,
@ -58,7 +64,30 @@ async def hebergement(request):
if form.is_valid(): if form.is_valid():
errors = await check_dns_config(form) errors = await check_dns_config(form)
if not errors: if not errors:
return render(request, 'hebergement_success.html', context) body = (
f'{form.email} demande que {form.domain} soit hébergé par '
f'JabberFR, type {form.type}, et a dit:\n{form.message}'
)
try:
send_mail(
subject='Demande dhébergement',
message=body,
from_email='hebergement@jabberfr.org',
recipient_list=['association@jabberfr.org'],
)
except BaseException:
log.error(
'ATTENTION: Le message na pas été envoyé:\n%s',
body,
exc_info=True,
)
form.add_error(
'__all__',
'Impossible denvoyer la demande, merci de nous '
'contacter directement.'
)
else:
return render(request, 'hebergement_success.html', context)
context['errors'] = errors context['errors'] = errors
else: else:
form = HebergForm() form = HebergForm()
@ -111,7 +140,40 @@ async def adhesion(request):
form = AdhesionForm(request.POST) form = AdhesionForm(request.POST)
context['form'] = form context['form'] = form
if form.is_valid(): if form.is_valid():
return render(request, 'adhesion_success.html', context=context) data = form.cleaned_data
body = (
f'Nouvelle adhésion à lassociation:\n\n'
f'Nom: {data["surname"]}\nPrénom: {data["name"]}\n'
f'Email: {data["email"]}\nJID: {data["jid"]}\n'
f'----'
)
try:
send_mail(
subject='Nouvelle adhésion à lassociation',
message=body,
from_email='no-reply@jabberfr.org',
recipient_list=['tresorier@jabberfr.org'],
)
except BaseException:
log.error(
'ATTENTION: Erreur denvoi de mail:\n%s',
body,
exc_info=True,
)
form.add_error(
'__all__',
'Impossible denvoyer la demande, merci de nous '
'contacter directement.'
)
context['form'] = reset_captcha_form(form)
else:
return render(
request,
'adhesion_success.html',
context=context
)
else:
context['form'] = reset_captcha_form(form)
else: else:
form = AdhesionForm(captcha=pick_captcha()) form = AdhesionForm(captcha=pick_captcha())
context['form'] = form context['form'] = form