PageRenderTime 15ms CodeModel.GetById 1ms app.highlight 11ms RepoModel.GetById 1ms app.codeStats 0ms

/forum/forms.py

http://djforum.googlecode.com/
Python | 136 lines | 100 code | 8 blank | 28 comment | 2 complexity | 282e025392a9d4ccdc9760c620d95fd7 MD5 | raw file
  1#coding=utf-8
  2from django.forms import fields,widgets
  3from django.contrib.auth import authenticate
  4
  5from django import forms
  6#from django.core.validators import alnum_re
  7from django.utils.translation import ugettext_lazy as _
  8from django.contrib.auth.models import User
  9
 10from forum.models import RegistrationProfile
 11
 12import re
 13
 14# I put this on all required fields, because it's easier to pick up
 15# on them with CSS or JavaScript if they have a class of "required"
 16# in the HTML. Your mileage may vary. If/when Django ticket #3515
 17# lands in trunk, this will no longer be necessary.
 18attrs_dict = { 'class': 'post' }
 19alnum_re = re.compile(r'^\w+$')
 20
 21class RegistrationForm(forms.Form):
 22    """
 23    Form for registering a new user account.
 24    
 25    Validates that the requested username is not already in use, and
 26    requires the password to be entered twice to catch typos.
 27    
 28    Subclasses should feel free to add any additional validation they
 29    need, but should either preserve the base ``save()`` or implement
 30    a ``save()`` which accepts the ``profile_callback`` keyword
 31    argument and passes it through to
 32    ``RegistrationProfile.objects.create_inactive_user()``.
 33    
 34    """
 35    username = forms.CharField(max_length=30,
 36                               widget=forms.TextInput(attrs=attrs_dict),
 37                               label=_(u'username'))
 38    email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
 39                                                               maxlength=75)),
 40                             label=_(u'email address'))
 41    password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
 42                                label=_(u'password'))
 43    password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
 44                                label=_(u'password (again)'))
 45    
 46    def clean_username(self):
 47        """
 48        Validate that the username is alphanumeric and is not already
 49        in use.
 50        
 51        """
 52        if not alnum_re.search(self.cleaned_data['username']):
 53            raise forms.ValidationError(_(u'Usernames can only contain letters, numbers and underscores'))
 54        try:
 55            user = User.objects.get(username__iexact=self.cleaned_data['username'])
 56        except User.DoesNotExist:
 57            return self.cleaned_data['username']
 58        raise forms.ValidationError(_(u'This username is already taken. Please choose another.'))
 59
 60    def clean(self):
 61        """
 62        Verifiy that the values entered into the two password fields
 63        match. Note that an error here will end up in
 64        ``non_field_errors()`` because it doesn't apply to a single
 65        field.
 66        
 67        """
 68        if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
 69            if self.cleaned_data['password1'] != self.cleaned_data['password2']:
 70                raise forms.ValidationError(_(u'You must type the same password each time'))
 71        return self.cleaned_data
 72    
 73    def save(self, profile_callback=None):
 74        """
 75        Create the new ``User`` and ``RegistrationProfile``, and
 76        returns the ``User``.
 77        
 78        This is essentially a light wrapper around
 79        ``RegistrationProfile.objects.create_inactive_user()``,
 80        feeding it the form data and a profile callback (see the
 81        documentation on ``create_inactive_user()`` for details) if
 82        supplied.
 83        
 84        """
 85        new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
 86                                                                    password=self.cleaned_data['password1'],
 87                                                                    email=self.cleaned_data['email'],
 88                                                                    profile_callback=profile_callback)
 89        return new_user
 90
 91    
 92TYPE_SEX_CHOICES = (
 93    ('????', '????'),
 94    ('????', '????'),
 95)
 96TYPE_CITY_CHOICES = (
 97    ('??', '??'),
 98    ('??', '??'),
 99)
100
101class SettingsForm(forms.Form):
102
103    #city = fields.ChoiceField(label="City", widget=widgets.Select(attrs={'class': 'post'}),choices=TYPE_CITY_CHOICES,required=False)
104
105    #sex = fields.ChoiceField(label="Sex", widget=widgets.Select(attrs={'class': 'post'}),choices=TYPE_SEX_CHOICES, required=False)
106
107    homepage = fields.CharField(label="Homepage", widget=widgets.TextInput(attrs={'size': 50,'class': 'post'}),max_length=500,required=False)
108    
109    description = fields.CharField(label="Description", widget=widgets.Textarea(attrs={'rows': 10, 'cols':50,'class': 'post'}),required=False)
110    
111    email = fields.EmailField(label="Email", widget=widgets.TextInput(attrs={'size': 30,'class': 'post'}),max_length=75,required=True)
112    
113    password = fields.CharField(label="Password", widget=widgets.PasswordInput(attrs={'size': 30, 'class': 'post'}),max_length=30,required=False)
114
115    password1 = fields.CharField(label="Password Confirmation", widget=widgets.PasswordInput(attrs={'size': 30, 'class': 'post'}),max_length=30,required=False)
116
117    
118    
119class LoginForm(forms.Form):
120
121    username = fields.CharField(label="Username", widget=widgets.TextInput(attrs={'size': 30,'class': 'post'}),max_length=30,required=True)
122
123    password = fields.CharField(label="Password", widget=widgets.PasswordInput(attrs={'size': 30, 'class': 'post'}),max_length=30,required=True)
124    
125class NewTopicForm(forms.Form):
126
127    subject = fields.CharField(label="Subject", widget=widgets.TextInput(attrs={'size': 50,'class': 'required'}),max_length=150)
128
129    content = fields.CharField(label="Body", widget=widgets.Textarea(attrs={'rows': 10, 'cols':50,'class': 'wymeditor'}))
130    
131class NewPostForm(forms.Form):
132
133    content = fields.CharField(label="Body", widget=widgets.Textarea(attrs={'rows': 10, 'cols':50,'class': 'wymeditor'}))
134
135
136