/forum/forms.py

http://djforum.googlecode.com/ · Python · 136 lines · 100 code · 8 blank · 28 comment · 3 complexity · 282e025392a9d4ccdc9760c620d95fd7 MD5 · raw file

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