Browse Source

Added CAPTCHA protection to floppyforms. #114

Ralfp 12 years ago
parent
commit
d23647f7f4

+ 4 - 13
misago/apps/activation/forms.py

@@ -1,27 +1,18 @@
 import hashlib
-from django import forms
+import floppyforms as forms
 from django.core.exceptions import ValidationError
 from django.utils.translation import ugettext_lazy as _
 from misago.forms import Form, QACaptchaField, ReCaptchaField
 from misago.models import User
 
 class UserSendActivationMailForm(Form):
-    email = forms.EmailField(max_length=255)
+    email = forms.EmailField(label=_("Your E-mail Address"),
+                             help_text=_("Enter email address send activation e-mail to. It must be valid e-mail you used to register on forums."),
+                             max_length=255)
     captcha_qa = QACaptchaField()
     recaptcha = ReCaptchaField()
     error_source = 'email'
 
-    layout = [
-              (
-               None,
-               [('email', {'label': _("Your E-mail Address"), 'help_text': _("Enter email address send activation e-mail to. It must be valid e-mail you used to register on forums."), 'attrs': {'placeholder': _("Enter your e-mail address.")}})]
-               ),
-              (
-               None,
-               ['captcha_qa', 'recaptcha']
-               ),
-              ]
-
     def clean_email(self):
         try:
             email = self.cleaned_data['email'].lower()

+ 1 - 2
misago/apps/activation/views.py

@@ -3,7 +3,6 @@ from django.utils.translation import ugettext as _
 from misago.apps.errors import error404, error_banned
 from misago.auth import sign_user_in
 from misago.decorators import block_authenticated, block_banned, block_crawlers, block_jammed
-from misago.forms import FormLayout
 from misago.messages import Message
 from misago.models import Ban, User
 from misago.shortcuts import redirect_message, render_to_response
@@ -43,7 +42,7 @@ def form(request):
     return render_to_response('resend_activation.html',
                               {
                                'message': message,
-                               'form': FormLayout(form),
+                               'form': form,
                               },
                               context_instance=RequestContext(request));
 

+ 2 - 1
misago/apps/register/forms.py

@@ -1,4 +1,4 @@
-from django import forms
+import floppyforms as forms
 from django.core.exceptions import ValidationError
 from django.utils.translation import ugettext_lazy as _
 from misago.conf import settings
@@ -26,6 +26,7 @@ class UserRegisterForm(Form):
                        }]
       
     def finalize_form(self):
+        raise NotImplementedError("TODO: Finish #114 in front-end!")
         self.layout = [
                       (
                        None,

+ 1 - 2
misago/apps/register/views.py

@@ -6,7 +6,6 @@ from django.utils.translation import ugettext as _
 from misago.auth import sign_user_in
 from misago.conf import settings
 from misago.decorators import block_authenticated, block_banned, block_crawlers, block_jammed
-from misago.forms import FormLayout
 from misago.messages import Message
 from misago.models import SignInAttempt, User
 from misago.shortcuts import render_to_response
@@ -80,7 +79,7 @@ def form(request):
     return render_to_response('register.html',
                               {
                               'message': message,
-                              'form': FormLayout(form),
+                              'form': form,
                               'hide_signin': True, 
                               },
                               context_instance=RequestContext(request));

+ 5 - 14
misago/apps/resetpswd/forms.py

@@ -1,27 +1,18 @@
 import hashlib
-from django import forms
+import floppyforms as forms
 from django.core.exceptions import ValidationError
 from django.utils.translation import ugettext_lazy as _
 from misago.forms import Form, QACaptchaField, ReCaptchaField
 from misago.models import User
     
 class UserResetPasswordForm(Form):
-    email = forms.EmailField(max_length=255)
+    email = forms.EmailField(label=_("Your E-mail Address"),
+                             help_text=_("Enter email address password reset confirmation e-mail will be sent to. It must be valid e-mail you used to register on forums."),
+                             max_length=255)
     captcha_qa = QACaptchaField()
     recaptcha = ReCaptchaField()
     error_source = 'email'
-    
-    layout = [
-              (
-               None,
-               [('email', {'label': _("Your E-mail Address"), 'help_text': _("Enter email address password reset confirmation e-mail will be sent to. It must be valid e-mail you used to register on forums."), 'attrs': {'placeholder': _("Enter your e-mail address.")}})]
-               ),
-              (
-               None,
-               ['captcha_qa', 'recaptcha']
-               ),
-              ]
-    
+        
     def clean_email(self):
         try:
             email = self.cleaned_data['email'].lower()

+ 1 - 2
misago/apps/resetpswd/views.py

@@ -2,7 +2,6 @@ from django.template import RequestContext
 from django.utils.translation import ugettext as _
 from misago.apps.errors import error404, error_banned
 from misago.decorators import block_authenticated, block_banned, block_crawlers, block_jammed
-from misago.forms import FormLayout
 from misago.messages import Message
 from misago.models import Ban, Session, Token, User
 from misago.shortcuts import render_to_response
@@ -45,7 +44,7 @@ def form(request):
     return render_to_response('reset_password.html',
                               {
                               'message': message,
-                              'form': FormLayout(form),
+                              'form': form,
                               },
                               context_instance=RequestContext(request));
 

+ 4 - 2
misago/forms/fields.py

@@ -21,8 +21,10 @@ class ForumChoiceField(TreeNodeChoiceField):
 class ReCaptchaField(fields.CharField):
     widget = ReCaptchaWidget
     api_error = None
-    def __init__(self, label=_("Verification Code"), *args, **kwargs):
-        kwargs['label'], kwargs['required'] = label, False
+    def __init__(self, *args, **kwargs):
+        kwargs['label'] = _("Verification Code")
+        kwargs['help_text'] = _("Enter the code from image into the text field.")
+        kwargs['required'] = False
         super(ReCaptchaField, self).__init__(*args, **kwargs)
 
 

+ 5 - 1
misago/forms/widgets.py

@@ -1,7 +1,11 @@
 import floppyforms as forms
+from recaptcha.client.captcha import displayhtml
+from misago.conf import settings
 
 class ReCaptchaWidget(forms.TextInput):
-    pass
+    def render(self):
+        return displayhtml(settings.recaptcha_public,
+                           settings.recaptcha_ssl)
 
 
 class YesNoSwitch(forms.CheckboxInput):

+ 2 - 1
templates/cranefly/register.html

@@ -1,5 +1,5 @@
 {% extends "cranefly/layout.html" %}
-{% import "_forms.html" as form_theme with context %}
+{% import "cranefly/forms.html" as form_theme with context %}
 {% import "cranefly/macros.html" as macros with context %}
 
 {% block title %}{{ macros.page_title(title=_('Register new account')) }}{% endblock %}
@@ -20,6 +20,7 @@
       {% endif %}
 
       <form action="{{ url('register') }}" method="post">
+        {{ form_theme.hidden_fields(form) }}
         <div class="form-fields">
           {{ form_theme.form_widget(form, width=8) }}
         </div>

+ 4 - 2
templates/cranefly/resend_activation.html

@@ -1,5 +1,5 @@
 {% extends "cranefly/layout.html" %}
-{% import "_forms.html" as form_theme with context %}
+{% import "forms.html" as form_theme with context %}
 {% import "cranefly/macros.html" as macros with context %}
 
 {% block title %}{{ macros.page_title(title=_('Request new Activation E-mail')) }}{% endblock %}
@@ -20,8 +20,10 @@
       {% endif %}
 
       <form action="{{ url('send_activation') }}" method="post">
+        {{ form_theme.hidden_fields(form) }}
         <div class="form-fields">
-          {{ form_theme.form_widget(form, width=6) }}
+          {{ form_theme.row(form.email, attrs={'class': 'span6', 'placeholder': _("Enter your e-mail address.")}) }}
+          {{ form_theme.captcha(form, width=6) }}
         </div>
         <div class="form-actions">
           <button type="submit" class="btn btn-primary">{% trans %}Request new E-mail{% endtrans %}</button>

+ 4 - 2
templates/cranefly/reset_password.html

@@ -1,5 +1,5 @@
 {% extends "cranefly/layout.html" %}
-{% import "_forms.html" as form_theme with context %}
+{% import "forms.html" as form_theme with context %}
 {% import "cranefly/macros.html" as macros with context %}
 
 {% block title %}{{ macros.page_title(title=_('Request New Password')) }}{% endblock %}
@@ -20,8 +20,10 @@
       {% endif %}
 
       <form action="{{ url('forgot_password') }}" method="post">
+        {{ form_theme.hidden_fields(form) }}
         <div class="form-fields">
-          {{ form_theme.form_widget(form, width=6) }}
+          {{ form_theme.row(form.email, attrs={'class': 'span6', 'placeholder': _("Enter your e-mail address.")}) }}
+          {{ form_theme.captcha(form, width=6) }}
         </div>
         <div class="form-actions">
           <button type="submit" class="btn btn-primary">{% trans %}Reset Password{% endtrans %}</button>

+ 13 - 2
templates/forms.html

@@ -8,8 +8,8 @@
 {%- endmacro %}
 
 {% macro row(_field, label=None, help_text=None, width=9, attrs=None) -%}
-  <div id="{{ field.html_name }}-control-group" class="control-group{% if _field.errors %} error{% endif %}">
-    <label class="control-label" for="id_{{ field.html_name }}">{% if label %}{{ label }}{% elif _field.label %}{{ _field.label }}{% else %}{{ _field.html_name }}{% endif %}</label>
+  <div id="{{ _field.html_name }}-control-group" class="control-group{% if _field.errors %} error{% endif %}">
+    <label class="control-label" for="id_{{ _field.html_name }}">{% if label %}{{ label }}{% elif _field.label %}{{ _field.label }}{% else %}{{ _field.html_name }}{% endif %}</label>
     <div class="controls">
       {% if attrs == None %}{% set attrs = {} %}{% endif %}
       {% if _field.field.widget.__class__.__name__ == 'CheckboxInput' %}
@@ -42,6 +42,15 @@
   </div>
 {%- endmacro %}
 
+{% macro captcha(form, width=9) -%}
+{% if 'recaptcha' in form.fields %}
+{{ row(form.recaptcha) }}
+{% endif %}
+{% if 'captcha_qa' in form.fields %}
+{{ row(form.captcha_qa, width=width) }}
+{% endif %}
+{%- endmacro %}
+
 {% macro field(_field, attrs=None, width=9) -%}
 {% set widget = _field.field.widget.__class__.__name__ %}
 {% set context = _field.field.widget.get_context(_field.html_name, _field.value(), attrs=attrs) %}
@@ -53,6 +62,8 @@
 {{ _textarea(_field, context) }}
 {% elif widget == 'Select' %}
 {{ _select(_field, context) }}
+{% elif widget == 'ReCaptchaWidget' %}
+{{ _field.field.widget.render()|safe }}
 {% else %}
 {{ _input(_field, context) }}
 {% endif %}