Просмотр исходного кода

Updated register form for floppy forms. #114

Ralfp 12 лет назад
Родитель
Сommit
ea84464f1e

+ 14 - 28
misago/apps/register/forms.py

@@ -2,20 +2,28 @@ import floppyforms as forms
 from django.core.exceptions import ValidationError
 from django.core.exceptions import ValidationError
 from django.utils.translation import ugettext_lazy as _
 from django.utils.translation import ugettext_lazy as _
 from misago.conf import settings
 from misago.conf import settings
-from misago.forms import Form, QACaptchaField, ReCaptchaField
+from misago.forms import Form, QACaptchaField, ReCaptchaField, ForumTOS
 from misago.models import User
 from misago.models import User
 from misago.utils.timezones import tzlist
 from misago.utils.timezones import tzlist
 from misago.validators import validate_username, validate_password, validate_email
 from misago.validators import validate_username, validate_password, validate_email
 
 
 class UserRegisterForm(Form):
 class UserRegisterForm(Form):
-    username = forms.CharField(max_length=15)
-    email = forms.EmailField(max_length=255)
+    username = forms.CharField(label=_('Username'),
+                               help_text=_("Your displayed username. Between %(min)s and %(max)s characters, only letters and digits are allowed.") % {'min': settings.username_length_min, 'max': settings.username_length_max},
+                               max_length=15)
+    email = forms.EmailField(label=_('E-mail address'),
+                             help_text=_("Working e-mail inbox is required to maintain control over your forum account."),
+                             max_length=255)
     email_rep = forms.EmailField(max_length=255)
     email_rep = forms.EmailField(max_length=255)
-    password = forms.CharField(max_length=255,widget=forms.PasswordInput)
+    password = forms.CharField(label=_('Password'),
+                               help_text=_("Password you will be using to sign in to your account. Make sure it's strong."),
+                               max_length=255,widget=forms.PasswordInput)
     password_rep = forms.CharField(max_length=255,widget=forms.PasswordInput)
     password_rep = forms.CharField(max_length=255,widget=forms.PasswordInput)
     captcha_qa = QACaptchaField()
     captcha_qa = QACaptchaField()
     recaptcha = ReCaptchaField()
     recaptcha = ReCaptchaField()
-    accept_tos = forms.BooleanField(required=True,error_messages={'required': _("Acceptation of board ToS is mandatory for membership.")})
+    accept_tos = forms.BooleanField(label=_("Forum Terms of Service"),
+                                    required=True, widget=ForumTOS,
+                                    error_messages={'required': _("Acceptation of board ToS is mandatory for membership.")})
     
     
     validate_repeats = (('email', 'email_rep'), ('password', 'password_rep'))
     validate_repeats = (('email', 'email_rep'), ('password', 'password_rep'))
     repeats_errors = [{
     repeats_errors = [{
@@ -25,31 +33,9 @@ class UserRegisterForm(Form):
                        'different': _("Entered passwords do not match."),
                        'different': _("Entered passwords do not match."),
                        }]
                        }]
       
       
-    def finalize_form(self):
-        raise NotImplementedError("TODO: Finish #114 in front-end!")
-        self.layout = [
-                      (
-                       None,
-                       [('username', {'label': _('Username'), 'help_text': _("Your displayed username. Between %(min)s and %(max)s characters, only letters and digits are allowed.") % {'min': settings.username_length_min, 'max': settings.username_length_max},'attrs': {'placeholder': _("Enter your desired username")}})]
-                       ),
-                      (
-                       None,
-                       [('nested', [('email', {'label': _('E-mail address'), 'help_text': _("Working e-mail inbox is required to maintain control over your forum account."), 'attrs': {'placeholder': _("Enter your e-mail")}, 'width': 50}), ('email_rep', {'attrs': {'placeholder': _("Repeat your e-mail")}, 'width': 50})]), 
-                       ('nested', [('password', {'label': _('Password'), 'help_text': _("Password you will be using to sign in to your account. Make sure it's strong."), 'has_value': False, 'attrs': {'placeholder': _("Enter your password")}, 'width': 50}), ('password_rep', {'has_value': False, 'attrs': {'placeholder': _("Repeat your password")}, 'width': 50})])]
-                       ),
-                      (
-                       None,
-                       ['captcha_qa', 'recaptcha']
-                       ),
-                      (
-                       None,
-                       [('accept_tos', {'label': _("Forum Terms of Service"), 'widget': 'forumTos'})]
-                       ),
-                      ]
-        
+    def finalize_form(self):        
         if not settings.tos_url and not settings.tos_content:
         if not settings.tos_url and not settings.tos_content:
             del self.fields['accept_tos']
             del self.fields['accept_tos']
-            del self.layout[3]
         
         
     def clean_username(self):
     def clean_username(self):
         validate_username(self.cleaned_data['username'])
         validate_username(self.cleaned_data['username'])

+ 1 - 1
misago/forms/__init__.py

@@ -1,4 +1,4 @@
 from misago.forms.fields import ForumChoiceField, ReCaptchaField, QACaptchaField
 from misago.forms.fields import ForumChoiceField, ReCaptchaField, QACaptchaField
 from misago.forms.forms import Form
 from misago.forms.forms import Form
 from misago.forms.layouts import FormLayout, FormFields, FormFieldsets
 from misago.forms.layouts import FormLayout, FormFields, FormFieldsets
-from misago.forms.widgets import ReCaptchaWidget, YesNoSwitch
+from misago.forms.widgets import ReCaptchaWidget, YesNoSwitch, ForumTOS

+ 4 - 0
misago/forms/forms.py

@@ -42,6 +42,10 @@ class Form(forms.Form):
         except KeyError:
         except KeyError:
             pass
             pass
 
 
+    @property
+    def has_captcha(self):
+        return 'recaptcha' in self.fields or 'captcha_qa' in self.fields
+
     def ensure_finalization(self):
     def ensure_finalization(self):
         if not self.form_finalized:
         if not self.form_finalized:
             self.form_finalized = True
             self.form_finalized = True

+ 4 - 0
misago/forms/widgets.py

@@ -10,3 +10,7 @@ class ReCaptchaWidget(forms.TextInput):
 
 
 class YesNoSwitch(forms.CheckboxInput):
 class YesNoSwitch(forms.CheckboxInput):
     pass
     pass
+
+
+class ForumTOS(forms.CheckboxInput):
+    pass

+ 30 - 2
templates/cranefly/register.html

@@ -1,5 +1,5 @@
 {% extends "cranefly/layout.html" %}
 {% extends "cranefly/layout.html" %}
-{% import "cranefly/forms.html" as form_theme with context %}
+{% import "forms.html" as form_theme with context %}
 {% import "cranefly/macros.html" as macros with context %}
 {% import "cranefly/macros.html" as macros with context %}
 
 
 {% block title %}{{ macros.page_title(title=_('Register new account')) }}{% endblock %}
 {% block title %}{{ macros.page_title(title=_('Register new account')) }}{% endblock %}
@@ -22,7 +22,35 @@
       <form action="{{ url('register') }}" method="post">
       <form action="{{ url('register') }}" method="post">
         {{ form_theme.hidden_fields(form) }}
         {{ form_theme.hidden_fields(form) }}
         <div class="form-fields">
         <div class="form-fields">
-          {{ form_theme.form_widget(form, width=8) }}
+          <fieldset class="first">
+            {{ form_theme.row(form.username, attrs={'class': 'span8'}) }}
+          </fieldset>
+          <fieldset>
+            {{ form_theme.repeat(form,
+                                 (form.email, form.email_rep),
+                                 attrs=(
+                                        {'placeholder': _("Enter your e-mail"), 'class': 'span4'},
+                                        {'placeholder': _("Repeat your e-mail"), 'class': 'span4'}
+                                       )) }}
+          </fieldset>
+          <fieldset>
+            {{ form_theme.repeat(form,
+                                 (form.password, form.password_rep),
+                                 attrs=(
+                                        {'placeholder': _("Enter your password"), 'class': 'span4'},
+                                        {'placeholder': _("Repeat your password"), 'class': 'span4'}
+                                       )) }}
+          </fieldset>
+          {% if form.has_captcha %}
+          <fieldset{% if 'accept_tos' not in form.fields %} class="last"{% endif %}>
+            {{ form_theme(form, 8) }}
+          </fieldset>
+          {% endif %}
+          {% if 'accept_tos' in form.fields %}
+          <fieldset class="last">
+            {{ form_theme.row(form.accept_tos) }}
+          </fieldset>
+          {% endif %}
         </div>
         </div>
         <div class="form-actions">
         <div class="form-actions">
           <button type="submit" class="btn btn-primary">{% trans %}Register account{% endtrans %}</button>
           <button type="submit" class="btn btn-primary">{% trans %}Register account{% endtrans %}</button>

+ 51 - 13
templates/forms.html

@@ -12,7 +12,8 @@
     <label class="control-label" for="id_{{ _field.html_name }}">{% if label %}{{ label }}{% elif _field.label %}{{ _field.label }}{% else %}{{ _field.html_name }}{% endif %}</label>
     <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">
     <div class="controls">
       {% if attrs == None %}{% set attrs = {} %}{% endif %}
       {% if attrs == None %}{% set attrs = {} %}{% endif %}
-      {% if _field.field.widget.__class__.__name__ == 'CheckboxInput' %}
+      {% if _field.field.widget.__class__.__name__ == 'ForumTOS' %}{% do attrs.update({'inline': make_tos_label()}) %}{% endif %}
+      {% if _field.field.widget.__class__.__name__ in ('CheckboxInput', 'ForumTOS') %}
         <label class="checkbox">
         <label class="checkbox">
           {{ field(_field, width=width, attrs=attrs)|trim }}
           {{ field(_field, width=width, attrs=attrs)|trim }}
           {% if 'inline' in attrs %}
           {% if 'inline' in attrs %}
@@ -27,21 +28,58 @@
         </label>
         </label>
       {% else %}
       {% else %}
         {{ field(_field, width=width, attrs=attrs)|trim }}
         {{ field(_field, width=width, attrs=attrs)|trim }}
-        {% endif %}
-        {% for error in _field.errors %}
-        <p class="help-block" style="font-weight: bold;">{{ error }}</p>
-        {% endfor %}
-        {% if 'inline' in attrs or _field.field.widget.__class__.__name__ != 'CheckboxInput' %}
-        {% if help_text %}
-        <p class="help-block">{{ help_text }}</p>
-        {% elif _field.help_text %}
-        <p class="help-block">{{ _field.help_text }}</p>
-        {% endif %}
+      {% endif %}
+      {% for error in _field.errors %}
+      <p class="help-block" style="font-weight: bold;">{{ error }}</p>
+      {% endfor %}
+      {% if 'inline' in attrs or _field.field.widget.__class__.__name__ not in ('CheckboxInput', 'ForumTOS') %}
+      {% if help_text %}
+      <p class="help-block">{{ help_text }}</p>
+      {% elif _field.help_text %}
+      <p class="help-block">{{ _field.help_text }}</p>
+      {% endif %}
+      {% endif %}
+    </div>
+  </div>
+{%- endmacro %}
+
+{% macro repeat(form, fields, label=None, help_text=None, attrs=None) -%}
+  <div id="{{ fields[0].html_name }}-control-group" class="control-group{% if fields[0].errors or fields[1].errors or (fields[0].name ~ '_' ~ fields[1].name) in form.errors %} error{% endif %}">
+    <label class="control-label" for="id_{{ fields[0].html_name }}">{% if label %}{{ label }}{% elif fields[0].label %}{{ fields[0].label }}{% else %}{{ fields[0].html_name }}{% endif %}</label>
+    <div class="controls controls-nested">
+      {% if attrs == None %}{% set attrs = ({}, {}) %}{% endif %}
+      <div class="row">
+        <div{% if 'class' in attrs[0] %} class="{{ attrs[0].class }}"{% endif %}>{{ field(fields[0], attrs=attrs[0]) }}</div>
+        <div{% if 'class' in attrs[1] %} class="{{ attrs[1].class }}"{% endif %}>{{ field(fields[1], attrs=attrs[1]) }}</div>
+      </div>
+      {% for error in form.errors[(fields[0].name ~ '_' ~ fields[1].name)] %}
+      <p class="help-block" style="font-weight: bold;">{{ error }}</p>
+      {% endfor %}
+      {% for error in fields[0].errors %}
+      <p class="help-block" style="font-weight: bold;">{{ error }}</p>
+      {% endfor %}
+      {% for error in fields[1].errors %}
+      {% if not error in fields[0].errors %}
+      <p class="help-block" style="font-weight: bold;">{{ error }}</p>
+      {% endif %}
+      {% endfor %}
+      {% if help_text %}
+      <p class="help-block">{{ help_text }}</p>
+      {% elif fields[0].help_text %}
+      <p class="help-block">{{ fields[0].help_text }}</p>
       {% endif %}
       {% endif %}
     </div>
     </div>
   </div>
   </div>
 {%- endmacro %}
 {%- endmacro %}
 
 
+{% macro make_tos_label() -%}
+{% trans forum_tos=make_tos_link()|safe %}I have read and accept this forums {{forum_tos}}.{% endtrans %}
+{%- endmacro %}
+
+{% macro make_tos_link() -%}
+<a href="{% if settings.tos_url %}{{ settings.tos_url }}{% else %}{{ url('tos') }}{% endif %}">{% if settings.tos_title %}{{ settings.tos_title }}{% else %}{% trans %}Terms of Service{% endtrans %}{% endif %}</a>
+{%- endmacro %}
+
 {% macro captcha(form, width=9) -%}
 {% macro captcha(form, width=9) -%}
 {% if 'recaptcha' in form.fields %}
 {% if 'recaptcha' in form.fields %}
 {{ row(form.recaptcha) }}
 {{ row(form.recaptcha) }}
@@ -54,7 +92,7 @@
 {% macro field(_field, attrs=None, width=9) -%}
 {% macro field(_field, attrs=None, width=9) -%}
 {% set widget = _field.field.widget.__class__.__name__ %}
 {% set widget = _field.field.widget.__class__.__name__ %}
 {% set context = _field.field.widget.get_context(_field.html_name, _field.value(), attrs=attrs) %}
 {% set context = _field.field.widget.get_context(_field.html_name, _field.value(), attrs=attrs) %}
-{% if not 'class' in context['attrs'] and not widget == 'CheckboxInput' %}
+{% if not 'class' in context['attrs'] and widget not in ('CheckboxInput', 'ForumTOS') %}
 {% do context['attrs'].update({'class': ('span' ~ width)}) %}
 {% do context['attrs'].update({'class': ('span' ~ width)}) %}
 {% endif %}
 {% endif %}
 {% if 'inline' in context.attrs %}{% do context.attrs.pop('inline') %}{% endif %}
 {% if 'inline' in context.attrs %}{% do context.attrs.pop('inline') %}{% endif %}
@@ -74,7 +112,7 @@
 {%- endmacro %}
 {%- endmacro %}
 
 
 {% macro _input(_field, context) -%}
 {% macro _input(_field, context) -%}
-<input type="{{ context.type }}" id="id_{{ context.name }}" name="{{ context.name }}" {{ attributes(context.attrs)|trim }}{% if 'value' in context and context.value|length > 0 %} value="{{ context.value }}"{% endif %}>
+<input type="{{ context.type }}" id="id_{{ context.name }}" name="{{ context.name }}" {{ attributes(context.attrs)|trim }}{% if context.type != 'password' and 'value' in context and context.value|length > 0 %} value="{{ context.value }}"{% endif %}>
 {%- endmacro %}
 {%- endmacro %}
 
 
 {% macro _textarea(_field, context) -%}
 {% macro _textarea(_field, context) -%}