# -*- coding: UTF-8 -*-
'''
Created on 2012-11-10

@author: tianwei
'''
#Forms and validation code for user registration.

from django import forms
from django.utils.translation import ugettext_lazy as _
from django.contrib.auth.models import User
from registration.models import RegistrationProfile

attrs_dict = {'class':'required'}

class RegistrationForm(forms.Form):
    """
    Form for registering a new user account.
    """
    username = forms.RegexField(regex = r'^\w+$',
                                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)'))
    
    machinecode = forms.RegexField(regex = r'^\w+$',
                                max_length = 60,
                                widget=forms.TextInput(attrs = attrs_dict),
                                label = _(u'machine code'))
    
    def clear_username(self):
        """
        Validate that the username is alphanumeric and is not already
        in use.
        """
        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
        """
        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,request, profile_callback=None):
        """
        Create the new ``User`` and ``RegistrationProfile``, and
        returns the ``User``.
        """
        new_user = RegistrationProfile.objects.create_inactive_user(request, 
                                                                    username = self.cleaned_data['username'],
                                                                    password = self.cleaned_data['password1'],
                                                                    email = self.cleaned_data['email'],
                                                                    machinecode = self.cleaned_data['machinecode'], 
                                                                    profile_callback = profile_callback)
        
        return new_user
    

class RegistrationFormTermsOfService(RegistrationForm):
    """
    Subclass of ``RegistrationForm`` which adds a required checkbox
    for agreeing to a site's Terms of Service.
    
    """
    tos = forms.BooleanField(widget = forms.CheckboxInput(attrs=attrs_dict),
                             label=_(u'I have read and agree to the Terms of Service'))
    
    def clean_tos(self):
        """
        Validate that the user accepted the Terms of Service.
        
        """
        if self.cleaned_data.get('tos',False):
            return self.cleaned_data['tos']
        raise forms.ValidationError(_(u'You must agree to the terms to register'))        
            
class RegistrationFormUniqueEmail(RegistrationForm):
    """
    Subclass of ``RegistrationForm`` which enforces uniqueness of
    email addresses.
    
    """
    def clean_email(self):
        """
        Validate that the supplied email address is unique for the site.
        """   
        if User.objects.filter(email__iexact=self.cleaned_data['email']):
            raise forms.ValidationError(_(u'This email address is already in use. Please supply a different email address.'))
        return self.cleaned_data['email']
    

    

        
        
        
        
        
        
        
        
@@ -455,35 +455,33 @@
 RegexField(regex
- = 
+=
 r'%5E%5Cw+$',%0A      
@@ -520,15 +520,67 @@
 ngth
- = 
+=
 30,%0A
+                                label=_(u'username'),%0A
     
@@ -630,36 +630,16 @@
 xtInput(
-attrs = attrs_dict),
 %0A       
@@ -667,70 +667,204 @@
     
-label = _(u'username'))%0A    email = forms.EmailField(
+    attrs=%7B'placeholder':_(u'username'),%0A                                        %22class%22:%22required%22%7D))%0A%0A    email = forms.EmailField(label=_(u'email address'),%0A                             
 widget
- = 
+=
 form
@@ -879,143 +879,240 @@
 put(
-attrs=dict(attrs_dict,maxlength=75)),%0A                             label=_(u'email address'))%0A    password1 = forms.CharField(
+%0A                                 attrs=%7B'placeholder':_(u'email address'),%0A                                        %22class%22:%22required%22%7D))%0A%0A    password1 = forms.CharField(label=_(u'password'),%0A                                
 widget
- = 
+=
 form
@@ -1131,46 +1131,141 @@
 put(
-attrs=attrs_dict,render_value = False)
+%0A                                    attrs=%7B'placeholder':_(u'password'),%0A                                        %22class%22:%22required%22%7D
 ,%0A  
@@ -1298,30 +1298,34 @@
     
-label=_(u'password'
+    render_value=False
 ))%0A
+%0A
     
@@ -1348,33 +1348,93 @@
 s.CharField(
+label=_(u'password (again)'),%0A                                
 widget
- = 
+=
 forms.Passwo
@@ -1445,107 +1445,204 @@
 put(
-attrs=attrs_dict,render_value = False),%0A                                label=_(u'password (again)'
+%0A                                    attrs=%7B'placeholder':_(u'password(again)'),%0A                                        %22class%22:%22required%22%7D,%0A                                    render_value=False
 ))%0A 
@@ -1685,19 +1685,17 @@
 ld(regex
- = 
+=
 r'%5E%5Cw+$'
@@ -1742,15 +1742,71 @@
 ngth
- = 
+=
 60,%0A
+                                label=_(u'machine code'),%0A
     
@@ -1860,29 +1860,13 @@
 put(
-attrs = attrs_dict),%0A
+%0A    
     
@@ -1897,16 +1897,29 @@
     
-label = 
+attrs=%7B'placeholder':
 _(u'
@@ -1929,21 +1929,78 @@
 hine
- 
 code')
-)
+,
 %0A    
+                                    %22class%22:%22required%22%7D))%0A
 %0A   
@@ -4942,28 +4942,29 @@
  %0A        %0A        %0A        
+%0A

Add placeholder