<s> from django . contrib . auth . models import User <EOL> from django import forms <EOL> import re <EOL> class RegistrationForm ( forms . ModelForm ) : <EOL> no_password_error = '<STR_LIT>' <EOL> invalid_username_error = '<STR_LIT>' <EOL> reserved_username_error = '<STR_LIT>' <EOL> duplicate_email_error = '<STR_LIT>' <EOL> username_re = re . compile ( '<STR_LIT>' ) <EOL> extra_required = ( '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:email>' ) <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . openid = kwargs . pop ( '<STR_LIT>' ) <EOL> except KeyError : <EOL> self . openid = None <EOL> try : <EOL> self . reserved_usernames = kwargs . pop ( '<STR_LIT>' ) <EOL> except KeyError : <EOL> self . reserved_usernames = [ ] <EOL> try : <EOL> self . no_duplicate_emails = kwargs . pop ( '<STR_LIT>' ) <EOL> except KeyError : <EOL> self . no_duplicate_emails = False <EOL> super ( RegistrationForm , self ) . __init__ ( * args , ** kwargs ) <EOL> for field in self . extra_required : <EOL> self . fields [ field ] . required = True <EOL> class Meta : <EOL> model = User <EOL> fields = ( '<STR_LIT:username>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT:email>' ) <EOL> password = forms . CharField ( <EOL> widget = forms . PasswordInput , <EOL> required = False <EOL> ) <EOL> def clean_username ( self ) : <EOL> username = self . cleaned_data . get ( '<STR_LIT:username>' , '<STR_LIT>' ) <EOL> if not self . username_re . match ( username ) : <EOL> raise forms . ValidationError , self . invalid_username_error <EOL> if username in self . reserved_usernames : <EOL> raise forms . ValidationError , self . reserved_username_error <EOL> return username <EOL> def clean_password ( self ) : <EOL> "<STR_LIT>" <EOL> password = self . cleaned_data . get ( '<STR_LIT:password>' , '<STR_LIT>' ) <EOL> if not self . openid and not password : <EOL> raise forms . ValidationError , self . no_password_error <EOL> return password <EOL> def clean_email ( self ) : <EOL> email = self . cleaned_data . get ( '<STR_LIT:email>' , '<STR_LIT>' ) <EOL> if self . no_duplicate_emails and User . objects . filter ( <EOL> email = email <EOL> ) . count ( ) > <NUM_LIT:0> : <EOL> raise forms . ValidationError , self . duplicate_email_error <EOL> return email <EOL> class RegistrationFormPasswordConfirm ( RegistrationForm ) : <EOL> password_mismatch_error = '<STR_LIT>' <EOL> password2 = forms . CharField ( <EOL> widget = forms . PasswordInput , <EOL> required = False , <EOL> label = "<STR_LIT>" <EOL> ) <EOL> def clean_password2 ( self ) : <EOL> password = self . cleaned_data . get ( '<STR_LIT:password>' , '<STR_LIT>' ) <EOL> password2 = self . cleaned_data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if password and ( password != password2 ) : <EOL> raise forms . ValidationError , self . password_mismatch_error <EOL> return password2 <EOL> class ChangePasswordForm ( forms . Form ) : <EOL> password = forms . CharField ( <EOL> widget = forms . PasswordInput , <EOL> required = True <EOL> ) <EOL> password2 = forms . CharField ( <EOL> widget = forms . PasswordInput , <EOL> required = True , <EOL> label = '<STR_LIT>' <EOL> ) <EOL> password_mismatch_error = '<STR_LIT>' <EOL> def __init__ ( self , user , * args , ** kwargs ) : <EOL> self . user = user <EOL> super ( ChangePasswordForm , self ) . __init__ ( * args , ** kwargs ) <EOL> def clean_password2 ( self ) : <EOL> password = self . cleaned_data . get ( '<STR_LIT:password>' , '<STR_LIT>' ) <EOL> password2 = self . cleaned_data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if password and ( password != password2 ) : <EOL> raise forms . ValidationError , self . password_mismatch_error <EOL> return password2 <EOL> class ChangePasswordVerifyOldForm ( ChangePasswordForm ) : <EOL> """<STR_LIT>""" <EOL> password_incorrect_error = '<STR_LIT>' <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> super ( ChangePasswordVerifyOldForm , self ) . __init__ ( * args , ** kwargs ) <EOL> if self . user . has_usable_password ( ) and self . user . password : <EOL> self . fields [ '<STR_LIT>' ] = forms . CharField ( <EOL> widget = forms . PasswordInput , <EOL> required = True <EOL> ) <EOL> def clean_old_password ( self ) : <EOL> password = self . cleaned_data . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> if not self . user . check_password ( password ) : <EOL> raise forms . ValidationError , self . password_incorrect_error </s>
