<s> from flask import render_template , current_app <EOL> from wtforms import * <EOL> from flask . ext . wtf import Form <EOL> from flask_mail import Message <EOL> from application import mail <EOL> from common . utils import get_signer <EOL> from accounts . models import User <EOL> class LoginForm ( Form ) : <EOL> user = None <EOL> username = TextField ( <EOL> label = u'<STR_LIT>' , <EOL> validators = [ <EOL> validators . required ( ) <EOL> ] <EOL> ) <EOL> password = PasswordField ( <EOL> label = u'<STR_LIT>' , <EOL> validators = [ <EOL> validators . required ( ) <EOL> ] <EOL> ) <EOL> next = HiddenField ( ) <EOL> def validate_username ( form , field ) : <EOL> username = field . data <EOL> try : <EOL> form . user = User . objects . get ( username = username ) <EOL> except : <EOL> raise ValidationError ( u'<STR_LIT>' ) <EOL> def validate_password ( form , field ) : <EOL> password = field . data <EOL> if form . user : <EOL> if not form . user . check_password ( password ) : <EOL> raise ValidationError ( u'<STR_LIT>' ) <EOL> class SignupForm ( Form ) : <EOL> email = TextField ( <EOL> label = u'<STR_LIT>' , <EOL> validators = [ <EOL> validators . required ( ) , <EOL> validators . length ( max = <NUM_LIT:100> ) , <EOL> validators . Email ( ) <EOL> ] <EOL> ) <EOL> def validate_email ( form , field ) : <EOL> email = field . data <EOL> if User . objects . filter ( email = email ) : <EOL> raise ValidationError ( u'<STR_LIT>' ) <EOL> def save ( self ) : <EOL> email = self . email . data <EOL> site_name = current_app . config [ '<STR_LIT>' ] <EOL> site_url = current_app . config [ '<STR_LIT>' ] <EOL> sender = current_app . config [ '<STR_LIT>' ] <EOL> s = get_signer ( ) <EOL> data = { <EOL> '<STR_LIT:email>' : email , <EOL> '<STR_LIT>' : True <EOL> } <EOL> signed_data = s . dumps ( data ) <EOL> context = dict ( <EOL> site_name = site_name , <EOL> site_url = site_url , <EOL> email = email , <EOL> signed_data = signed_data <EOL> ) <EOL> html = render_template ( <EOL> '<STR_LIT>' , ** context <EOL> ) <EOL> msg = Message ( <EOL> u'<STR_LIT>' . format ( site_name ) , <EOL> sender = sender , <EOL> recipients = [ email ] <EOL> ) <EOL> msg . html = html <EOL> mail . send ( msg ) <EOL> class SignupConfirmForm ( Form ) : <EOL> name = TextField ( <EOL> label = u'<STR_LIT:Name>' , <EOL> validators = [ <EOL> validators . required ( ) , <EOL> validators . length ( max = <NUM_LIT:100> ) <EOL> ] <EOL> ) <EOL> username = TextField ( <EOL> label = u'<STR_LIT>' , <EOL> validators = [ <EOL> validators . required ( ) , <EOL> validators . length ( min = <NUM_LIT:3> , max = <NUM_LIT:30> ) , <EOL> validators . Regexp ( <EOL> regex = r'<STR_LIT>' , <EOL> message = u'<STR_LIT>' <EOL> ) <EOL> ] <EOL> ) <EOL> password = PasswordField ( <EOL> label = u'<STR_LIT>' , <EOL> validators = [ <EOL> validators . required ( ) , <EOL> validators . length ( min = <NUM_LIT:6> , max = <NUM_LIT:16> ) <EOL> ] <EOL> ) <EOL> password_confirm = PasswordField ( <EOL> label = u'<STR_LIT>' , <EOL> validators = [ <EOL> validators . required ( ) <EOL> ] <EOL> ) <EOL> next = HiddenField ( ) <EOL> def validate_username ( form , field ) : <EOL> username = field . data <EOL> if User . objects . filter ( username = username ) : <EOL> raise ValidationError ( u'<STR_LIT>' ) <EOL> def validate_password_confirm ( form , field ) : <EOL> password = form . password . data <EOL> password_confirm = field . data <EOL> if password != password_confirm : <EOL> raise ValidationError ( u'<STR_LIT>' ) <EOL> def save ( self , email ) : <EOL> name = self . name . data <EOL> username = self . username . data <EOL> password = self . password . data <EOL> email = email <EOL> user = User ( name = name , username = username , password = password , email = email ) <EOL> user . save ( ) <EOL> return user <EOL> class RecoverPasswordForm ( Form ) : <EOL> email = TextField ( <EOL> label = u'<STR_LIT>' , <EOL> validators = [ <EOL> validators . required ( ) , <EOL> validators . length ( max = <NUM_LIT:100> ) , <EOL> validators . Email ( ) <EOL> ] <EOL> ) <EOL> def validate_email ( form , field ) : <EOL> email = field . data <EOL> if not User . objects . filter ( email = email ) : <EOL> raise ValidationError ( u'<STR_LIT>' ) <EOL> def save ( self ) : <EOL> email = self . email . data <EOL> site_name = current_app . config [ '<STR_LIT>' ] <EOL> site_url = current_app . config [ '<STR_LIT>' ] <EOL> sender = current_app . config [ '<STR_LIT>' ] <EOL> s = get_signer ( ) <EOL> data = { <EOL> '<STR_LIT:email>' : email , <EOL> '<STR_LIT>' : True <EOL> } <EOL> signed_data = s . dumps ( data ) <EOL> context = dict ( <EOL> site_name = site_name , <EOL> site_url = site_url , <EOL> email = email , <EOL> signed_data = signed_data <EOL> ) <EOL> html = render_template ( <EOL> '<STR_LIT>' , ** context <EOL> ) <EOL> msg = Message ( <EOL> u'<STR_LIT>' . format ( site_name ) , <EOL> sender = sender , <EOL> recipients = [ email ] <EOL> ) <EOL> msg . html = html <EOL> mail . send ( msg ) <EOL> class RecoverPasswordConfirmForm ( Form ) : <EOL> password = PasswordField ( <EOL> label = u'<STR_LIT>' , <EOL> validators = [ <EOL> validators . required ( ) , <EOL> validators . length ( min = <NUM_LIT:6> , max = <NUM_LIT:16> ) <EOL> ] <EOL> ) <EOL> password_confirm = PasswordField ( <EOL> label = u'<STR_LIT>' , <EOL> validators = [ <EOL> validators . required ( ) <EOL> ] <EOL> ) <EOL> def validate_password_confirm ( form , field ) : <EOL> password = form . password . data <EOL> password_confirm = field . data <EOL> if password != password_confirm : <EOL> raise ValidationError ( u'<STR_LIT>' ) <EOL> def save ( self ) : <EOL> password = self . password . data <EOL> self . user . set_password ( password ) <EOL> self . user . save ( ) <EOL> return self . user </s>
