Skip to content

Commit

Permalink
Ajout d'un captcha pour l'inscription
Browse files Browse the repository at this point in the history
Une migration de la base de données est nécessaire à l'installation
Fix #75
  • Loading branch information
Luthaf committed Jun 28, 2014
1 parent b247626 commit 850a63a
Show file tree
Hide file tree
Showing 5 changed files with 22 additions and 18 deletions.
27 changes: 10 additions & 17 deletions generator/forms.py
Original file line number Diff line number Diff line change
Expand Up @@ -25,42 +25,35 @@
from django.conf import settings
from django.contrib.sites.models import Site

from captcha.fields import CaptchaField

from generator.models import Song, Songbook, Layout


class RegisterForm(UserCreationForm):
""" Require email address when a user signs up """
email = forms.EmailField(label='Email address',
max_length=255,
required=True)
captcha = CaptchaField()

def __init__(self, *args, **kwargs):
super(RegisterForm, self).__init__(*args, **kwargs)
self.fields['email'].label = _("Adresse mail")
self.fields['username'].help_text = _("30 caractères maximum.")
self.fields['password2'].help_text = None
self.fields['username'].help_text = _(u"30 caractères maximum.")
self.fields['password2'].help_text = ""
self.fields['email'].required = True

class Meta:
model = User
fields = ('username', 'email', 'password1', 'password2')
fields = ('username', 'email', 'password1', 'password2', 'captcha')

def clean_email(self):
email = self.cleaned_data["email"]
try:
User.objects.get(email=email)
raise forms.ValidationError(_("Cette adresse mail existe déjà. "
"Si vous avez oublié votre mot de passe, "
"vous pouvez le réinitialiser."))
raise forms.ValidationError(_(u"Cette adresse mail existe déjà. "
u"Si vous avez oublié votre mot de passe, "
u"vous pouvez le réinitialiser."))
except User.DoesNotExist:
return email

def save(self, commit=True):
user = super(RegisterForm, self).save(commit=False)
user.email = self.cleaned_data["email"]
if commit:
user.save()
return user


ADMIN_MESSAGE = _(
u'''{user_info} vous a envoyé un message depuis le site {sitename}.
Expand Down
1 change: 1 addition & 0 deletions generator/templates/generator/register.html
Original file line number Diff line number Diff line change
Expand Up @@ -8,4 +8,5 @@
{{ form.as_p }}
<input type="submit" value="{% trans "S'inscrire" %}"/>
</form>
<a href="{% url 'password_reset' %}">{% trans "Mot de passe oublié ?" %}</a>
{% endblock %}
3 changes: 2 additions & 1 deletion generator/views/users.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,12 +29,13 @@
from generator.forms import RegisterForm
from generator.decorators import LoginRequiredMixin

class Register(CreateView):
class Register(FormView):
template_name = 'generator/register.html'
form_class = RegisterForm
success_url = reverse_lazy('home')

def form_valid(self, form):
form.save()
messages.success(self.request, _(u"Vous êtes à présent inscrit."
u"Connectez-vous pour accéder à votre profil."))
return super(Register, self).form_valid(form)
Expand Down
5 changes: 5 additions & 0 deletions patanet/settings.py
Original file line number Diff line number Diff line change
Expand Up @@ -47,6 +47,7 @@
INSTALLED_APPS += (
'south',
'background_task',
'captcha',
)
INSTALLED_APPS += (
'generator',
Expand Down Expand Up @@ -144,6 +145,10 @@

SOUTH_TESTS_MIGRATE = False

CAPTCHA_CHALLENGE_FUNCT = 'captcha.helpers.math_challenge'
CAPTCHA_LETTER_ROTATION = (-20, 20)
CAPTCHA_NOISE_FUNCTIONS = ('captcha.helpers.noise_dots',)

try:
from local_settings import *
except ImportError:
Expand Down
4 changes: 4 additions & 0 deletions patanet/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,10 @@
url(r'^admin/', include(admin.site.urls)),
)

urlpatterns += patterns('',
url(r'^captcha/', include('captcha.urls')),
)

urlpatterns += i18n_patterns('',
url(r'', include('generator.urls')),
)
Expand Down

0 comments on commit 850a63a

Please sign in to comment.