<s> import re <EOL> from django import forms <EOL> from django . conf import settings <EOL> from django . core . mail import send_mail <EOL> from django . template . loader import render_to_string <EOL> from django . utils . translation import ugettext_lazy as _ , ugettext <EOL> from django . utils . http import int_to_base36 <EOL> from django . contrib import messages <EOL> from django . contrib . auth import authenticate <EOL> from django . contrib . auth . models import User <EOL> from django . contrib . auth . tokens import default_token_generator <EOL> from django . contrib . sites . models import Site <EOL> from emailconfirmation . models import EmailAddress <EOL> from timezones . forms import TimeZoneField <EOL> from account . models import Account , PasswordReset <EOL> from account . utils import perform_login <EOL> alnum_re = re . compile ( r"<STR_LIT>" ) <EOL> REQUIRED_EMAIL = getattr ( settings , "<STR_LIT>" , False ) <EOL> EMAIL_VERIFICATION = getattr ( settings , "<STR_LIT>" , False ) <EOL> EMAIL_AUTHENTICATION = getattr ( settings , "<STR_LIT>" , False ) <EOL> UNIQUE_EMAIL = getattr ( settings , "<STR_LIT>" , False ) <EOL> class GroupForm ( forms . Form ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . group = kwargs . pop ( "<STR_LIT>" , None ) <EOL> super ( GroupForm , self ) . __init__ ( * args , ** kwargs ) <EOL> class LoginForm ( GroupForm ) : <EOL> password = forms . CharField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ( render_value = False ) <EOL> ) <EOL> remember = forms . BooleanField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> help_text = _ ( "<STR_LIT>" ) , <EOL> required = False <EOL> ) <EOL> user = None <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( LoginForm , self ) . __init__ ( * args , ** kwargs ) <EOL> ordering = [ ] <EOL> if EMAIL_AUTHENTICATION : <EOL> self . fields [ "<STR_LIT:email>" ] = forms . EmailField ( <EOL> label = ugettext ( "<STR_LIT>" ) , <EOL> ) <EOL> ordering . append ( "<STR_LIT:email>" ) <EOL> else : <EOL> self . fields [ "<STR_LIT:username>" ] = forms . CharField ( <EOL> label = ugettext ( "<STR_LIT>" ) , <EOL> max_length = <NUM_LIT:30> , <EOL> ) <EOL> ordering . append ( "<STR_LIT:username>" ) <EOL> ordering . extend ( [ "<STR_LIT:password>" , "<STR_LIT>" ] ) <EOL> self . fields . keyOrder = ordering <EOL> def user_credentials ( self ) : <EOL> """<STR_LIT>""" <EOL> credentials = { } <EOL> if EMAIL_AUTHENTICATION : <EOL> credentials [ "<STR_LIT:email>" ] = self . cleaned_data [ "<STR_LIT:email>" ] <EOL> else : <EOL> credentials [ "<STR_LIT:username>" ] = self . cleaned_data [ "<STR_LIT:username>" ] <EOL> credentials [ "<STR_LIT:password>" ] = self . cleaned_data [ "<STR_LIT:password>" ] <EOL> return credentials <EOL> def clean ( self ) : <EOL> if self . _errors : <EOL> return <EOL> user = authenticate ( ** self . user_credentials ( ) ) <EOL> if user : <EOL> if user . is_active : <EOL> self . user = user <EOL> else : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> else : <EOL> if EMAIL_AUTHENTICATION : <EOL> error = _ ( <EOL> "<STR_LIT>" ) <EOL> else : <EOL> error = _ ( <EOL> "<STR_LIT>" ) <EOL> raise forms . ValidationError ( error ) <EOL> return self . cleaned_data <EOL> def login ( self , request ) : <EOL> perform_login ( request , self . user ) <EOL> if self . cleaned_data [ "<STR_LIT>" ] : <EOL> request . session . set_expiry ( <NUM_LIT> * <NUM_LIT> * <NUM_LIT> * <NUM_LIT:7> * <NUM_LIT:3> ) <EOL> else : <EOL> request . session . set_expiry ( <NUM_LIT:0> ) <EOL> class SignupForm ( GroupForm ) : <EOL> username = forms . CharField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> max_length = <NUM_LIT:30> , <EOL> widget = forms . TextInput ( ) <EOL> ) <EOL> password1 = forms . CharField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ( render_value = False ) <EOL> ) <EOL> password2 = forms . CharField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ( render_value = False ) <EOL> ) <EOL> email = forms . EmailField ( widget = forms . TextInput ( ) ) <EOL> confirmation_key = forms . CharField ( <EOL> max_length = <NUM_LIT> , <EOL> required = False , <EOL> widget = forms . HiddenInput ( ) <EOL> ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( SignupForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if REQUIRED_EMAIL or EMAIL_VERIFICATION or EMAIL_AUTHENTICATION : <EOL> self . fields [ "<STR_LIT:email>" ] . label = ugettext ( "<STR_LIT>" ) <EOL> self . fields [ "<STR_LIT:email>" ] . required = True <EOL> else : <EOL> self . fields [ "<STR_LIT:email>" ] . label = ugettext ( "<STR_LIT>" ) <EOL> self . fields [ "<STR_LIT:email>" ] . required = False <EOL> def clean_username ( self ) : <EOL> if not alnum_re . search ( self . cleaned_data [ "<STR_LIT:username>" ] ) : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> try : <EOL> user = User . objects . get ( <EOL> username__iexact = self . cleaned_data [ "<STR_LIT:username>" ] ) <EOL> except User . DoesNotExist : <EOL> return self . cleaned_data [ "<STR_LIT:username>" ] <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> def clean_email ( self ) : <EOL> value = self . cleaned_data [ "<STR_LIT:email>" ] <EOL> if UNIQUE_EMAIL or EMAIL_AUTHENTICATION : <EOL> try : <EOL> User . objects . get ( email__iexact = value ) <EOL> except User . DoesNotExist : <EOL> return value <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> return value <EOL> def clean ( self ) : <EOL> if "<STR_LIT>" in self . cleaned_data and "<STR_LIT>" in self . cleaned_data : <EOL> if self . cleaned_data [ "<STR_LIT>" ] != self . cleaned_data [ "<STR_LIT>" ] : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> return self . cleaned_data <EOL> def create_user ( self , username = None , commit = True ) : <EOL> user = User ( ) <EOL> if username is None : <EOL> raise NotImplementedError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> user . username = username <EOL> user . email = self . cleaned_data [ "<STR_LIT:email>" ] . strip ( ) . lower ( ) <EOL> password = self . cleaned_data . get ( "<STR_LIT>" ) <EOL> if password : <EOL> user . set_password ( password ) <EOL> else : <EOL> user . set_unusable_password ( ) <EOL> if commit : <EOL> user . save ( ) <EOL> return user <EOL> def login ( self , request , user ) : <EOL> user . backend = "<STR_LIT>" <EOL> perform_login ( request , user ) <EOL> def save ( self , request = None ) : <EOL> username = self . cleaned_data . get ( "<STR_LIT:username>" ) <EOL> email = self . cleaned_data [ "<STR_LIT:email>" ] <EOL> if self . cleaned_data [ "<STR_LIT>" ] : <EOL> from friends . models import JoinInvitation <EOL> try : <EOL> join_invitation = JoinInvitation . objects . get ( <EOL> confirmation_key = self . cleaned_data [ "<STR_LIT>" ] ) <EOL> confirmed = True <EOL> except JoinInvitation . DoesNotExist : <EOL> confirmed = False <EOL> else : <EOL> confirmed = False <EOL> if confirmed : <EOL> if email == join_invitation . contact . email : <EOL> new_user = self . create_user ( username ) <EOL> join_invitation . accept ( new_user ) <EOL> if request : <EOL> messages . add_message ( <EOL> request , messages . INFO , <EOL> ugettext ( u"<STR_LIT>" ) ) <EOL> EmailAddress ( user = new_user , email = email , <EOL> verified = True , primary = True ) . save ( ) <EOL> else : <EOL> new_user = self . create_user ( username ) <EOL> join_invitation . accept ( new_user ) <EOL> if email : <EOL> if request : <EOL> messages . add_message ( <EOL> request , messages . INFO , <EOL> ugettext ( u"<STR_LIT>" ) % { <EOL> "<STR_LIT:email>" : email , <EOL> } ) <EOL> EmailAddress . objects . add_email ( new_user , email ) <EOL> else : <EOL> new_user = self . create_user ( username ) <EOL> if email : <EOL> if request and not EMAIL_VERIFICATION : <EOL> messages . add_message ( <EOL> request , messages . INFO , <EOL> ugettext ( u"<STR_LIT>" ) % { <EOL> "<STR_LIT:email>" : email , <EOL> } ) <EOL> EmailAddress . objects . add_email ( new_user , email ) <EOL> if EMAIL_VERIFICATION : <EOL> new_user . is_active = False <EOL> new_user . save ( ) <EOL> self . after_signup ( new_user ) <EOL> return new_user <EOL> def after_signup ( self , user , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> class OpenIDSignupForm ( SignupForm ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . openid = kwargs . pop ( "<STR_LIT>" , None ) <EOL> kwargs . pop ( "<STR_LIT>" , [ ] ) <EOL> kwargs . pop ( "<STR_LIT>" , False ) <EOL> super ( OpenIDSignupForm , self ) . __init__ ( * args , ** kwargs ) <EOL> del self . fields [ "<STR_LIT>" ] <EOL> del self . fields [ "<STR_LIT>" ] <EOL> class UserForm ( forms . Form ) : <EOL> def __init__ ( self , user = None , * args , ** kwargs ) : <EOL> self . user = user <EOL> super ( UserForm , self ) . __init__ ( * args , ** kwargs ) <EOL> class AccountForm ( UserForm ) : <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( AccountForm , self ) . __init__ ( * args , ** kwargs ) <EOL> try : <EOL> self . account = Account . objects . get ( user = self . user ) <EOL> except Account . DoesNotExist : <EOL> self . account = Account ( user = self . user ) <EOL> class AddEmailForm ( UserForm ) : <EOL> email = forms . EmailField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = True , <EOL> widget = forms . TextInput ( attrs = { "<STR_LIT:size>" : "<STR_LIT>" } ) <EOL> ) <EOL> def clean_email ( self ) : <EOL> value = self . cleaned_data [ "<STR_LIT:email>" ] <EOL> errors = { <EOL> "<STR_LIT>" : _ ( "<STR_LIT>" ) , <EOL> "<STR_LIT>" : _ ( "<STR_LIT>" ) , <EOL> } <EOL> if UNIQUE_EMAIL : <EOL> try : <EOL> email = EmailAddress . objects . get ( email__iexact = value ) <EOL> except EmailAddress . DoesNotExist : <EOL> return value <EOL> if email . user == self . user : <EOL> raise forms . ValidationError ( errors [ "<STR_LIT>" ] ) <EOL> raise forms . ValidationError ( errors [ "<STR_LIT>" ] ) <EOL> else : <EOL> try : <EOL> EmailAddress . objects . get ( user = self . user , email__iexact = value ) <EOL> except EmailAddress . DoesNotExist : <EOL> return value <EOL> raise forms . ValidationError ( errors [ "<STR_LIT>" ] ) <EOL> def save ( self ) : <EOL> return EmailAddress . objects . add_email ( self . user , self . cleaned_data [ "<STR_LIT:email>" ] ) <EOL> class ChangePasswordForm ( UserForm ) : <EOL> oldpassword = forms . CharField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ( render_value = False ) <EOL> ) <EOL> password1 = forms . CharField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ( render_value = False ) <EOL> ) <EOL> password2 = forms . CharField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ( render_value = False ) <EOL> ) <EOL> def clean_oldpassword ( self ) : <EOL> if not self . user . check_password ( self . cleaned_data . get ( "<STR_LIT>" ) ) : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> return self . cleaned_data [ "<STR_LIT>" ] <EOL> def clean_password2 ( self ) : <EOL> if "<STR_LIT>" in self . cleaned_data and "<STR_LIT>" in self . cleaned_data : <EOL> if self . cleaned_data [ "<STR_LIT>" ] != self . cleaned_data [ "<STR_LIT>" ] : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> return self . cleaned_data [ "<STR_LIT>" ] <EOL> def save ( self ) : <EOL> self . user . set_password ( self . cleaned_data [ "<STR_LIT>" ] ) <EOL> self . user . save ( ) <EOL> class SetPasswordForm ( UserForm ) : <EOL> password1 = forms . CharField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ( render_value = False ) <EOL> ) <EOL> password2 = forms . CharField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ( render_value = False ) <EOL> ) <EOL> def clean_password2 ( self ) : <EOL> if "<STR_LIT>" in self . cleaned_data and "<STR_LIT>" in self . cleaned_data : <EOL> if self . cleaned_data [ "<STR_LIT>" ] != self . cleaned_data [ "<STR_LIT>" ] : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> return self . cleaned_data [ "<STR_LIT>" ] <EOL> def save ( self ) : <EOL> self . user . set_password ( self . cleaned_data [ "<STR_LIT>" ] ) <EOL> self . user . save ( ) <EOL> class ResetPasswordForm ( forms . Form ) : <EOL> email = forms . EmailField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = True , <EOL> widget = forms . TextInput ( attrs = { "<STR_LIT:size>" : "<STR_LIT>" } ) <EOL> ) <EOL> def clean_email ( self ) : <EOL> if EmailAddress . objects . filter ( email__iexact = self . cleaned_data [ "<STR_LIT:email>" ] , verified = True ) . count ( ) == <NUM_LIT:0> : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> return self . cleaned_data [ "<STR_LIT:email>" ] <EOL> def save ( self , ** kwargs ) : <EOL> email = self . cleaned_data [ "<STR_LIT:email>" ] <EOL> token_generator = kwargs . get ( <EOL> "<STR_LIT>" , default_token_generator ) <EOL> for user in User . objects . filter ( email__iexact = email ) : <EOL> temp_key = token_generator . make_token ( user ) <EOL> password_reset = PasswordReset ( user = user , temp_key = temp_key ) <EOL> password_reset . save ( ) <EOL> current_site = Site . objects . get_current ( ) <EOL> domain = unicode ( current_site . domain ) <EOL> subject = _ ( "<STR_LIT>" ) <EOL> message = render_to_string ( "<STR_LIT>" , { <EOL> "<STR_LIT:user>" : user , <EOL> "<STR_LIT>" : int_to_base36 ( user . id ) , <EOL> "<STR_LIT>" : temp_key , <EOL> "<STR_LIT>" : domain , <EOL> } ) <EOL> send_mail ( <EOL> subject , message , settings . DEFAULT_FROM_EMAIL , [ user . email ] ) <EOL> return self . cleaned_data [ "<STR_LIT:email>" ] <EOL> class ResetPasswordKeyForm ( forms . Form ) : <EOL> password1 = forms . CharField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ( render_value = False ) <EOL> ) <EOL> password2 = forms . CharField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ( render_value = False ) <EOL> ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> self . user = kwargs . pop ( "<STR_LIT:user>" , None ) <EOL> self . temp_key = kwargs . pop ( "<STR_LIT>" , None ) <EOL> super ( ResetPasswordKeyForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean_password2 ( self ) : <EOL> if "<STR_LIT>" in self . cleaned_data and "<STR_LIT>" in self . cleaned_data : <EOL> if self . cleaned_data [ "<STR_LIT>" ] != self . cleaned_data [ "<STR_LIT>" ] : <EOL> raise forms . ValidationError ( <EOL> _ ( "<STR_LIT>" ) ) <EOL> return self . cleaned_data [ "<STR_LIT>" ] <EOL> def save ( self ) : <EOL> user = self . user <EOL> user . set_password ( self . cleaned_data [ "<STR_LIT>" ] ) <EOL> user . save ( ) <EOL> PasswordReset . objects . filter ( temp_key = self . temp_key ) . update ( reset = True ) <EOL> class ChangeTimezoneForm ( AccountForm ) : <EOL> timezone = TimeZoneField ( label = _ ( "<STR_LIT>" ) , required = True ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ChangeTimezoneForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . initial . update ( { "<STR_LIT>" : self . account . timezone } ) <EOL> def save ( self ) : <EOL> self . account . timezone = self . cleaned_data [ "<STR_LIT>" ] <EOL> self . account . save ( ) <EOL> class ChangeLanguageForm ( AccountForm ) : <EOL> language = forms . ChoiceField ( <EOL> label = _ ( "<STR_LIT>" ) , <EOL> required = True , <EOL> choices = settings . LANGUAGES <EOL> ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ChangeLanguageForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . initial . update ( { "<STR_LIT>" : self . account . language } ) <EOL> def save ( self ) : <EOL> self . account . language = self . cleaned_data [ "<STR_LIT>" ] <EOL> self . account . save ( ) </s>
