project/kiwipycon/user/forms.py
changeset 94 87e77aa18610
parent 93 e86755df35da
child 95 f94e0cd9a862
equal deleted inserted replaced
93:e86755df35da 94:87e77aa18610
     1 # -*- coding: utf-8 -*-
       
     2 from __future__ import absolute_import
       
     3 
       
     4 #django
       
     5 from django import forms
       
     6 from django.contrib.auth.models import User
       
     7 
       
     8 class RegistrantForm(forms.Form):
       
     9     """Form to register an attendee
       
    10     """
       
    11     username = forms.RegexField(label="Nickname", max_length=30,
       
    12             regex=r'^\w+$',
       
    13         help_text = "30 characters or fewer. Alphanumeric" \
       
    14             + " characters only (letters, digits and underscores).",
       
    15         error_message = "This value must contain only letters, numbers and underscores.")
       
    16     name = forms.CharField(label=u"Name", max_length=50, required=True)
       
    17     email = forms.EmailField(label=u"E-mail", max_length=50, required=True)
       
    18 
       
    19     def clean_email(self):
       
    20         """Validates that the entered e-mail is unique.
       
    21         """
       
    22         email = self.cleaned_data.get("email")
       
    23         if email and User.objects.filter(email=email).count() > 0:
       
    24             raise forms.ValidationError(
       
    25                 u"That email address is already in use. Are you a member of " \
       
    26                  "site? Please log in.")
       
    27 
       
    28         return email
       
    29 
       
    30     def clean_username(self):
       
    31         """Validates that the entered username is unique.
       
    32         """
       
    33         username = self.cleaned_data.get("username")
       
    34         if username and User.objects.filter(username=username).count() > 0:
       
    35             raise forms.ValidationError(
       
    36                 u"That username is already in use.")
       
    37 
       
    38         return username
       
    39 
       
    40 class RegisterForm(forms.Form):
       
    41     """Form to register speaker
       
    42     """
       
    43     username = forms.RegexField(label="Username", max_length=30,
       
    44             regex=r'^\w+$',
       
    45         help_text = "Required. 30 characters or fewer. Alphanumeric" \
       
    46             + " characters only (letters, digits and underscores).",
       
    47         error_message = "This value must contain only letters, numbers and underscores.")
       
    48     first_name = forms.CharField(label=u"First name", max_length=50)
       
    49     last_name = forms.CharField(label=u"Last name", max_length=50)
       
    50     email = forms.EmailField(label=u"E-mail", max_length=50)
       
    51     url = forms.URLField(required=False)
       
    52     about = forms.CharField(label=u'Short Bio', max_length=50, required=False)
       
    53     photo = forms.FileField(label=u'Profile Photo', required=False)
       
    54     password_1 = forms.CharField(
       
    55         label=u"Password", widget=forms.PasswordInput(), max_length=20)
       
    56     password_2 = forms.CharField(
       
    57         label=u"Confirm password", widget=forms.PasswordInput(), max_length=20)
       
    58 
       
    59     def clean_password_2(self):
       
    60         """Validates that password 1 and password 2 are the same.
       
    61         """
       
    62         p1 = self.cleaned_data.get('password_1')
       
    63         p2 = self.cleaned_data.get('password_2')
       
    64 
       
    65         if not (p1 and p2 and p1 == p2):
       
    66             raise forms.ValidationError(u"The two passwords do not match.")
       
    67 
       
    68         return p2
       
    69 
       
    70     def clean_email(self):
       
    71         """Validates that the entered e-mail is unique.
       
    72         """
       
    73         email = self.cleaned_data.get("email")
       
    74         if email and User.objects.filter(email=email).count() > 0:
       
    75             raise forms.ValidationError(
       
    76                 u"That email address is already in use.")
       
    77 
       
    78         return email
       
    79 
       
    80     def clean_username(self):
       
    81         """Validates that the entered username is unique.
       
    82         """
       
    83         username = self.cleaned_data.get("username")
       
    84         if username and User.objects.filter(username=username).count() > 0:
       
    85             raise forms.ValidationError(
       
    86                 u"That username is already in use.")
       
    87 
       
    88         return username
       
    89 
       
    90 class EditProfileForm(forms.Form):
       
    91     """Edit user profile form
       
    92     """
       
    93     first_name = forms.CharField(max_length=50)
       
    94     last_name = forms.CharField(max_length=50)
       
    95     email = forms.EmailField(max_length=50)
       
    96     email2 = forms.CharField(widget=forms.HiddenInput)
       
    97     url = forms.URLField(required=False)
       
    98     about = forms.CharField(label=u'Short Bio',
       
    99             widget=forms.Textarea, required=False)
       
   100     photo = forms.FileField(label=u'Profile Photo',
       
   101             required=False)
       
   102 
       
   103     def clean_email(self):
       
   104         """Validates that the entered e-mail is unique.
       
   105         """
       
   106         email = self.cleaned_data.get("email")
       
   107         email2 = self.data.get("email2").strip()
       
   108         print email, email2
       
   109         if email != email2: # email has been changed
       
   110             if email and User.objects.filter(email=email).count() > 0:
       
   111                 raise forms.ValidationError(
       
   112                     u"That email address is already in use.")
       
   113 
       
   114         return email
       
   115 
       
   116 class UsernameForm(forms.Form):
       
   117     """Form to edit email address
       
   118     """
       
   119     username = forms.RegexField(label="Username", max_length=30,
       
   120             regex=r'^\w+$',
       
   121         help_text = "Required. 30 characters or fewer. Alphanumeric" \
       
   122             + " characters only (letters, digits and underscores).",
       
   123         error_message = "This value must contain only letters, numbers and underscores.")
       
   124 
       
   125     def clean_username(self):
       
   126         """Validates that the entered username is unique.
       
   127         """
       
   128         username = self.cleaned_data.get("username")
       
   129         if username and User.objects.filter(username=username).count() > 0:
       
   130             raise forms.ValidationError(
       
   131                 u"That username is already in use.")
       
   132 
       
   133         return username
       
   134 
       
   135