/forum/forms.py
http://djforum.googlecode.com/ · Python · 136 lines · 100 code · 8 blank · 28 comment · 3 complexity · 282e025392a9d4ccdc9760c620d95fd7 MD5 · raw file
- #coding=utf-8
- from django.forms import fields,widgets
- from django.contrib.auth import authenticate
-
- from django import forms
- #from django.core.validators import alnum_re
- from django.utils.translation import ugettext_lazy as _
- from django.contrib.auth.models import User
-
- from forum.models import RegistrationProfile
-
- import re
-
- # I put this on all required fields, because it's easier to pick up
- # on them with CSS or JavaScript if they have a class of "required"
- # in the HTML. Your mileage may vary. If/when Django ticket #3515
- # lands in trunk, this will no longer be necessary.
- attrs_dict = { 'class': 'post' }
- alnum_re = re.compile(r'^\w+$')
-
- class RegistrationForm(forms.Form):
- """
- Form for registering a new user account.
-
- Validates that the requested username is not already in use, and
- requires the password to be entered twice to catch typos.
-
- Subclasses should feel free to add any additional validation they
- need, but should either preserve the base ``save()`` or implement
- a ``save()`` which accepts the ``profile_callback`` keyword
- argument and passes it through to
- ``RegistrationProfile.objects.create_inactive_user()``.
-
- """
- username = forms.CharField(max_length=30,
- widget=forms.TextInput(attrs=attrs_dict),
- label=_(u'username'))
- email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
- maxlength=75)),
- label=_(u'email address'))
- password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
- label=_(u'password'))
- password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
- label=_(u'password (again)'))
-
- def clean_username(self):
- """
- Validate that the username is alphanumeric and is not already
- in use.
-
- """
- if not alnum_re.search(self.cleaned_data['username']):
- raise forms.ValidationError(_(u'Usernames can only contain letters, numbers and underscores'))
- try:
- user = User.objects.get(username__iexact=self.cleaned_data['username'])
- except User.DoesNotExist:
- return self.cleaned_data['username']
- raise forms.ValidationError(_(u'This username is already taken. Please choose another.'))
-
- def clean(self):
- """
- Verifiy that the values entered into the two password fields
- match. Note that an error here will end up in
- ``non_field_errors()`` because it doesn't apply to a single
- field.
-
- """
- if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
- if self.cleaned_data['password1'] != self.cleaned_data['password2']:
- raise forms.ValidationError(_(u'You must type the same password each time'))
- return self.cleaned_data
-
- def save(self, profile_callback=None):
- """
- Create the new ``User`` and ``RegistrationProfile``, and
- returns the ``User``.
-
- This is essentially a light wrapper around
- ``RegistrationProfile.objects.create_inactive_user()``,
- feeding it the form data and a profile callback (see the
- documentation on ``create_inactive_user()`` for details) if
- supplied.
-
- """
- new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
- password=self.cleaned_data['password1'],
- email=self.cleaned_data['email'],
- profile_callback=profile_callback)
- return new_user
-
-
- TYPE_SEX_CHOICES = (
- ('????', '????'),
- ('????', '????'),
- )
- TYPE_CITY_CHOICES = (
- ('??', '??'),
- ('??', '??'),
- )
-
- class SettingsForm(forms.Form):
-
- #city = fields.ChoiceField(label="City", widget=widgets.Select(attrs={'class': 'post'}),choices=TYPE_CITY_CHOICES,required=False)
-
- #sex = fields.ChoiceField(label="Sex", widget=widgets.Select(attrs={'class': 'post'}),choices=TYPE_SEX_CHOICES, required=False)
-
- homepage = fields.CharField(label="Homepage", widget=widgets.TextInput(attrs={'size': 50,'class': 'post'}),max_length=500,required=False)
-
- description = fields.CharField(label="Description", widget=widgets.Textarea(attrs={'rows': 10, 'cols':50,'class': 'post'}),required=False)
-
- email = fields.EmailField(label="Email", widget=widgets.TextInput(attrs={'size': 30,'class': 'post'}),max_length=75,required=True)
-
- password = fields.CharField(label="Password", widget=widgets.PasswordInput(attrs={'size': 30, 'class': 'post'}),max_length=30,required=False)
-
- password1 = fields.CharField(label="Password Confirmation", widget=widgets.PasswordInput(attrs={'size': 30, 'class': 'post'}),max_length=30,required=False)
-
-
-
- class LoginForm(forms.Form):
-
- username = fields.CharField(label="Username", widget=widgets.TextInput(attrs={'size': 30,'class': 'post'}),max_length=30,required=True)
-
- password = fields.CharField(label="Password", widget=widgets.PasswordInput(attrs={'size': 30, 'class': 'post'}),max_length=30,required=True)
-
- class NewTopicForm(forms.Form):
-
- subject = fields.CharField(label="Subject", widget=widgets.TextInput(attrs={'size': 50,'class': 'required'}),max_length=150)
-
- content = fields.CharField(label="Body", widget=widgets.Textarea(attrs={'rows': 10, 'cols':50,'class': 'wymeditor'}))
-
- class NewPostForm(forms.Form):
-
- content = fields.CharField(label="Body", widget=widgets.Textarea(attrs={'rows': 10, 'cols':50,'class': 'wymeditor'}))