<s> from django import forms <EOL> from django . utils . translation import ugettext_lazy as _ <EOL> from django . contrib . auth import authenticate <EOL> try : <EOL> from hashlib import sha1 as sha_constructor <EOL> except ImportError : <EOL> from django . utils . hashcompat import sha_constructor <EOL> from userena import settings as userena_settings <EOL> from userena . models import UserenaSignup <EOL> from userena . utils import get_profile_model , get_user_model <EOL> import random <EOL> attrs_dict = { '<STR_LIT:class>' : '<STR_LIT>' } <EOL> USERNAME_RE = r'<STR_LIT>' <EOL> class SignupForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> username = forms . RegexField ( regex = USERNAME_RE , <EOL> max_length = <NUM_LIT:30> , <EOL> widget = forms . TextInput ( attrs = attrs_dict ) , <EOL> label = _ ( "<STR_LIT>" ) , <EOL> error_messages = { '<STR_LIT>' : _ ( '<STR_LIT>' ) } ) <EOL> email = forms . EmailField ( widget = forms . TextInput ( attrs = dict ( attrs_dict , <EOL> maxlength = <NUM_LIT> ) ) , <EOL> label = _ ( "<STR_LIT>" ) ) <EOL> password1 = forms . CharField ( widget = forms . PasswordInput ( attrs = attrs_dict , <EOL> render_value = False ) , <EOL> label = _ ( "<STR_LIT>" ) ) <EOL> password2 = forms . CharField ( widget = forms . PasswordInput ( attrs = attrs_dict , <EOL> render_value = False ) , <EOL> label = _ ( "<STR_LIT>" ) ) <EOL> def clean_username ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> user = get_user_model ( ) . objects . get ( username__iexact = self . cleaned_data [ '<STR_LIT:username>' ] ) <EOL> except get_user_model ( ) . DoesNotExist : <EOL> pass <EOL> else : <EOL> if UserenaSignup . objects . filter ( user__username__iexact = self . cleaned_data [ '<STR_LIT:username>' ] ) . exclude ( activation_key = userena_settings . USERENA_ACTIVATED ) : <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> if self . cleaned_data [ '<STR_LIT:username>' ] . lower ( ) in userena_settings . USERENA_FORBIDDEN_USERNAMES : <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> return self . cleaned_data [ '<STR_LIT:username>' ] <EOL> def clean_email ( self ) : <EOL> """<STR_LIT>""" <EOL> if get_user_model ( ) . objects . filter ( email__iexact = self . cleaned_data [ '<STR_LIT:email>' ] ) : <EOL> if UserenaSignup . objects . filter ( user__email__iexact = self . cleaned_data [ '<STR_LIT:email>' ] ) . exclude ( activation_key = userena_settings . USERENA_ACTIVATED ) : <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> raise forms . ValidationError ( _ ( '<STR_LIT>' ) ) <EOL> return self . cleaned_data [ '<STR_LIT:email>' ] <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <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 ( _ ( '<STR_LIT>' ) ) <EOL> return self . cleaned_data <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> username , email , password = ( self . cleaned_data [ '<STR_LIT:username>' ] , <EOL> self . cleaned_data [ '<STR_LIT:email>' ] , <EOL> self . cleaned_data [ '<STR_LIT>' ] ) <EOL> new_user = UserenaSignup . objects . create_user ( username , <EOL> email , <EOL> password , <EOL> not userena_settings . USERENA_ACTIVATION_REQUIRED , <EOL> userena_settings . USERENA_ACTIVATION_REQUIRED ) <EOL> return new_user <EOL> class SignupFormOnlyEmail ( SignupForm ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( SignupFormOnlyEmail , self ) . __init__ ( * args , ** kwargs ) <EOL> del self . fields [ '<STR_LIT:username>' ] <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> while True : <EOL> username = sha_constructor ( str ( random . random ( ) ) ) . hexdigest ( ) [ : <NUM_LIT:5> ] <EOL> try : <EOL> get_user_model ( ) . objects . get ( username__iexact = username ) <EOL> except get_user_model ( ) . DoesNotExist : break <EOL> self . cleaned_data [ '<STR_LIT:username>' ] = username <EOL> return super ( SignupFormOnlyEmail , self ) . save ( ) <EOL> class SignupFormTos ( SignupForm ) : <EOL> """<STR_LIT>""" <EOL> tos = forms . BooleanField ( widget = forms . CheckboxInput ( attrs = attrs_dict ) , <EOL> label = _ ( u'<STR_LIT>' ) , <EOL> error_messages = { '<STR_LIT>' : _ ( '<STR_LIT>' ) } ) <EOL> def identification_field_factory ( label , error_required ) : <EOL> """<STR_LIT>""" <EOL> return forms . CharField ( label = label , <EOL> widget = forms . TextInput ( attrs = attrs_dict ) , <EOL> max_length = <NUM_LIT> , <EOL> error_messages = { '<STR_LIT>' : _ ( "<STR_LIT>" ) % { '<STR_LIT:error>' : error_required } } ) <EOL> class AuthenticationForm ( forms . Form ) : <EOL> """<STR_LIT>""" <EOL> identification = identification_field_factory ( _ ( u"<STR_LIT>" ) , <EOL> _ ( u"<STR_LIT>" ) ) <EOL> password = forms . CharField ( label = _ ( "<STR_LIT>" ) , <EOL> widget = forms . PasswordInput ( attrs = attrs_dict , render_value = False ) ) <EOL> remember_me = forms . BooleanField ( widget = forms . CheckboxInput ( ) , <EOL> required = False , <EOL> label = _ ( u'<STR_LIT>' ) % { '<STR_LIT>' : _ ( userena_settings . USERENA_REMEMBER_ME_DAYS [ <NUM_LIT:0> ] ) } ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( AuthenticationForm , self ) . __init__ ( * args , ** kwargs ) <EOL> self . fields [ '<STR_LIT>' ] . label = _ ( u'<STR_LIT>' ) % { '<STR_LIT>' : _ ( userena_settings . USERENA_REMEMBER_ME_DAYS [ <NUM_LIT:0> ] ) } <EOL> if userena_settings . USERENA_WITHOUT_USERNAMES : <EOL> self . fields [ '<STR_LIT>' ] = identification_field_factory ( _ ( u"<STR_LIT>" ) , <EOL> _ ( u"<STR_LIT>" ) ) <EOL> def clean ( self ) : <EOL> """<STR_LIT>""" <EOL> identification = self . cleaned_data . get ( '<STR_LIT>' ) <EOL> password = self . cleaned_data . get ( '<STR_LIT:password>' ) <EOL> if identification and password : <EOL> user = authenticate ( identification = identification , password = password ) <EOL> if user is None : <EOL> raise forms . ValidationError ( _ ( u"<STR_LIT>" ) ) <EOL> return self . cleaned_data <EOL> class ChangeEmailForm ( forms . Form ) : <EOL> email = forms . EmailField ( widget = forms . TextInput ( attrs = dict ( attrs_dict , <EOL> maxlength = <NUM_LIT> ) ) , <EOL> label = _ ( u"<STR_LIT>" ) ) <EOL> def __init__ ( self , user , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> super ( ChangeEmailForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if not isinstance ( user , get_user_model ( ) ) : <EOL> raise TypeError , "<STR_LIT>" % get_user_model ( ) . __name__ <EOL> else : self . user = user <EOL> def clean_email ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . cleaned_data [ '<STR_LIT:email>' ] . lower ( ) == self . user . email : <EOL> raise forms . ValidationError ( _ ( u'<STR_LIT>' ) ) <EOL> if get_user_model ( ) . objects . filter ( email__iexact = self . cleaned_data [ '<STR_LIT:email>' ] ) . exclude ( email__iexact = self . user . email ) : <EOL> raise forms . ValidationError ( _ ( u'<STR_LIT>' ) ) <EOL> return self . cleaned_data [ '<STR_LIT:email>' ] <EOL> def save ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . user . userena_signup . change_email ( self . cleaned_data [ '<STR_LIT:email>' ] ) <EOL> class EditProfileForm ( forms . ModelForm ) : <EOL> """<STR_LIT>""" <EOL> first_name = forms . CharField ( label = _ ( u'<STR_LIT>' ) , <EOL> max_length = <NUM_LIT:30> , <EOL> required = False ) <EOL> last_name = forms . CharField ( label = _ ( u'<STR_LIT>' ) , <EOL> max_length = <NUM_LIT:30> , <EOL> required = False ) <EOL> def __init__ ( self , * args , ** kw ) : <EOL> super ( EditProfileForm , self ) . __init__ ( * args , ** kw ) <EOL> new_order = self . fields . keyOrder [ : - <NUM_LIT:2> ] <EOL> new_order . insert ( <NUM_LIT:0> , '<STR_LIT>' ) <EOL> new_order . insert ( <NUM_LIT:1> , '<STR_LIT>' ) <EOL> self . fields . keyOrder = new_order <EOL> class Meta : <EOL> model = get_profile_model ( ) <EOL> exclude = [ '<STR_LIT:user>' ] <EOL> def save ( self , force_insert = False , force_update = False , commit = True ) : <EOL> profile = super ( EditProfileForm , self ) . save ( commit = commit ) <EOL> user = profile . user <EOL> user . first_name = self . cleaned_data [ '<STR_LIT>' ] <EOL> user . last_name = self . cleaned_data [ '<STR_LIT>' ] <EOL> user . save ( ) <EOL> return profile </s>
