PageRenderTime 35ms CodeModel.GetById 39ms RepoModel.GetById 4ms app.codeStats 0ms

/lib/registration/forms.py

https://github.com/redduck666/clusterify
Python | 205 lines | 162 code | 13 blank | 30 comment | 3 complexity | 2786e8589e434642bf79af3618a2c2dd MD5 | raw file
  1. """
  2. Forms and validation code for user registration.
  3. """
  4. from django.contrib.auth.models import User
  5. from django import forms
  6. from django.utils.translation import ugettext_lazy as _
  7. from registration.models import RegistrationProfile
  8. # I put this on all required fields, because it's easier to pick up
  9. # on them with CSS or JavaScript if they have a class of "required"
  10. # in the HTML. Your mileage may vary. If/when Django ticket #3515
  11. # lands in trunk, this will no longer be necessary.
  12. attrs_dict = { 'class': 'required' }
  13. class RegistrationForm(forms.Form):
  14. """
  15. Form for registering a new user account.
  16. Validates that the requested username is not already in use, and
  17. requires the password to be entered twice to catch typos.
  18. Subclasses should feel free to add any additional validation they
  19. need, but should either preserve the base ``save()`` or implement
  20. a ``save()`` method which returns a ``User``.
  21. """
  22. username = forms.RegexField(regex=r'^\w+$',
  23. max_length=30,
  24. min_length=3,
  25. widget=forms.TextInput(attrs=attrs_dict),
  26. label=_(u'username'))
  27. email = forms.EmailField(widget=forms.TextInput(attrs=dict(attrs_dict,
  28. maxlength=75)),
  29. label=_(u'email address'))
  30. password1 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
  31. label=_(u'password'))
  32. password2 = forms.CharField(widget=forms.PasswordInput(attrs=attrs_dict, render_value=False),
  33. label=_(u'password (again)'))
  34. def clean_username(self):
  35. """
  36. Validate that the username is alphanumeric and is not already
  37. in use.
  38. """
  39. try:
  40. user = User.objects.get(username__iexact=self.cleaned_data['username'])
  41. except User.DoesNotExist:
  42. return self.cleaned_data['username']
  43. raise forms.ValidationError(_(u'This username is already taken. Please choose another.'))
  44. def clean(self):
  45. """
  46. Verifiy that the values entered into the two password fields
  47. match. Note that an error here will end up in
  48. ``non_field_errors()`` because it doesn't apply to a single
  49. field.
  50. """
  51. if 'password1' in self.cleaned_data and 'password2' in self.cleaned_data:
  52. if self.cleaned_data['password1'] != self.cleaned_data['password2']:
  53. raise forms.ValidationError(_(u'You must type the same password each time'))
  54. return self.cleaned_data
  55. def save(self):
  56. """
  57. Create the new ``User`` and ``RegistrationProfile``, and
  58. returns the ``User`` (by calling
  59. ``RegistrationProfile.objects.create_inactive_user()``).
  60. """
  61. new_user = RegistrationProfile.objects.create_inactive_user(username=self.cleaned_data['username'],
  62. password=self.cleaned_data['password1'],
  63. email=self.cleaned_data['email'])
  64. return new_user
  65. class RegistrationFormTermsOfService(RegistrationForm):
  66. """
  67. Subclass of ``RegistrationForm`` which adds a required checkbox
  68. for agreeing to a site's Terms of Service.
  69. """
  70. tos = forms.BooleanField(widget=forms.CheckboxInput(attrs=attrs_dict),
  71. label=_(u'I have read and agree to the Terms of Service'),
  72. error_messages={ 'required': u"You must agree to the terms to register" })
  73. class RegistrationFormUniqueEmail(RegistrationForm):
  74. """
  75. Subclass of ``RegistrationForm`` which enforces uniqueness of
  76. email addresses.
  77. """
  78. def clean_email(self):
  79. """
  80. Validate that the supplied email address is unique for the
  81. site.
  82. """
  83. if User.objects.filter(email__iexact=self.cleaned_data['email']):
  84. raise forms.ValidationError(_(u'This email address is already in use. Please supply a different email address.'))
  85. return self.cleaned_data['email']
  86. class RegistrationFormNoFreeEmail(RegistrationForm):
  87. """
  88. Subclass of ``RegistrationForm`` which disallows registration with
  89. email addresses from popular free webmail services; moderately
  90. useful for preventing automated spam registrations.
  91. To change the list of banned domains, subclass this form and
  92. override the attribute ``bad_domains``.
  93. """
  94. bad_domains = ['aim.com', 'aol.com', 'email.com', 'gmail.com',
  95. 'googlemail.com', 'hotmail.com', 'hushmail.com',
  96. 'msn.com', 'mail.ru', 'mailinator.com', 'live.com']
  97. def clean_email(self):
  98. """
  99. Check the supplied email address against a list of known free
  100. webmail domains.
  101. """
  102. email_domain = self.cleaned_data['email'].split('@')[1]
  103. if email_domain in self.bad_domains:
  104. raise forms.ValidationError(_(u'Registration using free email addresses is prohibited. Please supply a different email address.'))
  105. return self.cleaned_data['email']
  106. ##############################################################################
  107. # THE FOLLOWING LICENSE APPLIES TO THE REST OF THIS FILE
  108. # (the rest of this file contains additions made to the original
  109. # django-registration module for Clusterify)
  110. """
  111. "The contents of this file are subject to the Common Public Attribution
  112. License Version 1.0 (the "License"); you may not use this file except
  113. in compliance with the License. You may obtain a copy of the License at
  114. http://www.clusterify.com/files/CODE_LICENSE.txt. The License is based
  115. on the Mozilla Public License Version 1.1 but Sections 14 and 15 have
  116. been added to cover use of software over a computer network and provide
  117. for limited attribution for the Original Developer. In addition, Exhibit
  118. A has been modified to be consistent with Exhibit B.
  119. Software distributed under the License is distributed on an "AS IS" basis,
  120. WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
  121. for the specific language governing rights and limitations under the
  122. License.
  123. The Original Code is Clusterify.
  124. The Initial Developer of the Original Code is "the Clusterify.com team",
  125. which is described at http://www.clusterify.com/about/. All portions of
  126. the code written by the Initial Developer are Copyright (c) the Initial
  127. Developer. All Rights Reserved.
  128. """
  129. # MODIF: added this class
  130. class ProfileForm(forms.Form):
  131. description = forms.CharField(
  132. required=False,
  133. widget=forms.widgets.Textarea(
  134. attrs={'class':'markdown_textarea','rows':'20'}),
  135. max_length=5000)
  136. email = forms.EmailField(required=False)
  137. tags = forms.RegexField(
  138. required=False,
  139. regex=r'^[A-Za-z0-9\- ]+$')
  140. location = forms.CharField(required=False)
  141. class OpenIdRegistrationForm(forms.Form):
  142. username = forms.RegexField(regex=r'^\w+$',
  143. max_length=30,
  144. min_length=3,
  145. widget=forms.TextInput(attrs=attrs_dict),
  146. label=_(u'username'))
  147. email = forms.EmailField(required=False, widget=forms.TextInput(attrs=dict(attrs_dict,
  148. maxlength=75)),
  149. label=_(u'email address'))
  150. def clean_username(self):
  151. """
  152. Validate that the username is alphanumeric and is not already
  153. in use.
  154. """
  155. try:
  156. user = User.objects.get(username__iexact=self.cleaned_data['username'])
  157. except User.DoesNotExist:
  158. return self.cleaned_data['username']
  159. raise forms.ValidationError(_(u'This username is already taken. Please choose another.'))