<s> from werkzeug import cached_property <EOL> from tipfy import RequestHandler <EOL> from tipfy . auth import ( login_required , user_required , <EOL> UserRequiredIfAuthenticatedMiddleware ) <EOL> from tipfy . auth . facebook import FacebookMixin <EOL> from tipfy . auth . friendfeed import FriendFeedMixin <EOL> from tipfy . auth . google import GoogleMixin <EOL> from tipfy . auth . twitter import TwitterMixin <EOL> from tipfy . sessions import SessionMiddleware <EOL> from tipfy . utils import json_encode <EOL> from tipfyext . jinja2 import Jinja2Mixin <EOL> from tipfyext . wtforms import Form , fields , validators <EOL> REQUIRED = validators . required ( ) <EOL> class LoginForm ( Form ) : <EOL> username = fields . TextField ( '<STR_LIT>' , validators = [ REQUIRED ] ) <EOL> password = fields . PasswordField ( '<STR_LIT>' , validators = [ REQUIRED ] ) <EOL> remember = fields . BooleanField ( '<STR_LIT>' ) <EOL> class SignupForm ( Form ) : <EOL> nickname = fields . TextField ( '<STR_LIT>' , validators = [ REQUIRED ] ) <EOL> class RegistrationForm ( Form ) : <EOL> username = fields . TextField ( '<STR_LIT>' , validators = [ REQUIRED ] ) <EOL> password = fields . PasswordField ( '<STR_LIT>' , validators = [ REQUIRED ] ) <EOL> password_confirm = fields . PasswordField ( '<STR_LIT>' , validators = [ REQUIRED ] ) <EOL> class BaseHandler ( RequestHandler , Jinja2Mixin ) : <EOL> middleware = [ SessionMiddleware ( ) , UserRequiredIfAuthenticatedMiddleware ( ) ] <EOL> @ cached_property <EOL> def messages ( self ) : <EOL> """<STR_LIT>""" <EOL> return self . session . get_flashes ( key = '<STR_LIT>' ) <EOL> def render_response ( self , filename , ** kwargs ) : <EOL> auth_session = None <EOL> if self . auth . session : <EOL> auth_session = self . auth . session <EOL> kwargs . update ( { <EOL> '<STR_LIT>' : auth_session , <EOL> '<STR_LIT>' : self . auth . user , <EOL> '<STR_LIT>' : self . auth . login_url ( ) , <EOL> '<STR_LIT>' : self . auth . logout_url ( ) , <EOL> '<STR_LIT>' : self . request . url , <EOL> } ) <EOL> if self . messages : <EOL> kwargs [ '<STR_LIT>' ] = json_encode ( [ dict ( body = body , level = level ) <EOL> for body , level in self . messages ] ) <EOL> return super ( BaseHandler , self ) . render_response ( filename , ** kwargs ) <EOL> def redirect_path ( self , default = '<STR_LIT:/>' ) : <EOL> if '<STR_LIT>' in self . session : <EOL> url = self . session . pop ( '<STR_LIT>' ) <EOL> else : <EOL> url = self . request . args . get ( '<STR_LIT>' , '<STR_LIT:/>' ) <EOL> if not url . startswith ( '<STR_LIT:/>' ) : <EOL> url = default <EOL> return url <EOL> def _on_auth_redirect ( self ) : <EOL> """<STR_LIT>""" <EOL> if '<STR_LIT>' in self . session : <EOL> url = self . session . pop ( '<STR_LIT>' ) <EOL> else : <EOL> url = '<STR_LIT:/>' <EOL> if not self . auth . user : <EOL> url = self . auth . signup_url ( ) <EOL> return self . redirect ( url ) <EOL> class HomeHandler ( BaseHandler ) : <EOL> def get ( self , ** kwargs ) : <EOL> return self . render_response ( '<STR_LIT>' , section = '<STR_LIT>' ) <EOL> class ContentHandler ( BaseHandler ) : <EOL> @ user_required <EOL> def get ( self , ** kwargs ) : <EOL> return self . render_response ( '<STR_LIT>' , section = '<STR_LIT:content>' ) <EOL> class LoginHandler ( BaseHandler ) : <EOL> def get ( self , ** kwargs ) : <EOL> redirect_url = self . redirect_path ( ) <EOL> if self . auth . user : <EOL> return self . redirect ( redirect_url ) <EOL> opts = { '<STR_LIT>' : self . redirect_path ( ) } <EOL> context = { <EOL> '<STR_LIT>' : self . form , <EOL> '<STR_LIT>' : self . url_for ( '<STR_LIT>' , ** opts ) , <EOL> '<STR_LIT>' : self . url_for ( '<STR_LIT>' , ** opts ) , <EOL> '<STR_LIT>' : self . url_for ( '<STR_LIT>' , ** opts ) , <EOL> '<STR_LIT>' : self . url_for ( '<STR_LIT>' , ** opts ) , <EOL> '<STR_LIT>' : self . url_for ( '<STR_LIT>' , ** opts ) , <EOL> } <EOL> return self . render_response ( '<STR_LIT>' , ** context ) <EOL> def post ( self , ** kwargs ) : <EOL> redirect_url = self . redirect_path ( ) <EOL> if self . auth . user : <EOL> return self . redirect ( redirect_url ) <EOL> if self . form . validate ( ) : <EOL> username = self . form . username . data <EOL> password = self . form . password . data <EOL> remember = self . form . remember . data <EOL> res = self . auth . login_with_form ( username , password , remember ) <EOL> if res : <EOL> self . session . add_flash ( '<STR_LIT>' , '<STR_LIT:success>' , '<STR_LIT>' ) <EOL> return self . redirect ( redirect_url ) <EOL> self . messages . append ( ( '<STR_LIT>' , <EOL> '<STR_LIT:error>' ) ) <EOL> return self . get ( ** kwargs ) <EOL> @ cached_property <EOL> def form ( self ) : <EOL> return LoginForm ( self . request ) <EOL> class LogoutHandler ( BaseHandler ) : <EOL> def get ( self , ** kwargs ) : <EOL> self . auth . logout ( ) <EOL> return self . redirect ( self . redirect_path ( ) ) <EOL> class SignupHandler ( BaseHandler ) : <EOL> @ login_required <EOL> def get ( self , ** kwargs ) : <EOL> if self . auth . user : <EOL> return self . redirect ( self . redirect_path ( ) ) <EOL> return self . render_response ( '<STR_LIT>' , form = self . form ) <EOL> @ login_required <EOL> def post ( self , ** kwargs ) : <EOL> redirect_url = self . redirect_path ( ) <EOL> if self . auth . user : <EOL> return self . redirect ( redirect_url ) <EOL> if self . form . validate ( ) : <EOL> auth_id = self . auth . session . get ( '<STR_LIT:id>' ) <EOL> user = self . auth . create_user ( self . form . nickname . data , auth_id ) <EOL> if user : <EOL> self . auth . login_with_auth_id ( user . auth_id , True ) <EOL> self . session . add_flash ( '<STR_LIT>' , <EOL> '<STR_LIT:success>' , '<STR_LIT>' ) <EOL> return self . redirect ( redirect_url ) <EOL> else : <EOL> self . messages . append ( ( '<STR_LIT>' , <EOL> '<STR_LIT:error>' ) ) <EOL> return self . get ( ** kwargs ) <EOL> self . messages . append ( ( '<STR_LIT>' <EOL> '<STR_LIT>' , '<STR_LIT:error>' ) ) <EOL> return self . get ( ** kwargs ) <EOL> @ cached_property <EOL> def form ( self ) : <EOL> return SignupForm ( self . request ) <EOL> class RegisterHandler ( BaseHandler ) : <EOL> def get ( self , ** kwargs ) : <EOL> redirect_url = self . redirect_path ( ) <EOL> if self . auth . user : <EOL> return self . redirect ( redirect_url ) <EOL> return self . render_response ( '<STR_LIT>' , form = self . form ) <EOL> def post ( self , ** kwargs ) : <EOL> redirect_url = self . redirect_path ( ) <EOL> if self . auth . user : <EOL> return self . redirect ( redirect_url ) <EOL> if self . form . validate ( ) : <EOL> username = self . form . username . data <EOL> password = self . form . password . data <EOL> password_confirm = self . form . password_confirm . data <EOL> if password != password_confirm : <EOL> self . messages . append ( ( "<STR_LIT>" , <EOL> '<STR_LIT:error>' ) ) <EOL> return self . get ( ** kwargs ) <EOL> auth_id = '<STR_LIT>' % username <EOL> user = self . auth . create_user ( username , auth_id , password = password ) <EOL> if user : <EOL> self . auth . login_with_auth_id ( user . auth_id , True ) <EOL> self . session . add_flash ( '<STR_LIT>' , <EOL> '<STR_LIT:success>' , '<STR_LIT>' ) <EOL> return self . redirect ( redirect_url ) <EOL> else : <EOL> self . messages . append ( ( '<STR_LIT>' , <EOL> '<STR_LIT:error>' ) ) <EOL> return self . get ( ** kwargs ) <EOL> self . messages . append ( ( '<STR_LIT>' <EOL> '<STR_LIT>' , '<STR_LIT:error>' ) ) <EOL> return self . get ( ** kwargs ) <EOL> @ cached_property <EOL> def form ( self ) : <EOL> return RegistrationForm ( self . request ) <EOL> class FacebookAuthHandler ( BaseHandler , FacebookMixin ) : <EOL> def head ( self , ** kwargs ) : <EOL> """<STR_LIT>""" <EOL> return self . app . response_class ( '<STR_LIT>' ) <EOL> def get ( self ) : <EOL> url = self . redirect_path ( ) <EOL> if self . auth . session : <EOL> return self . redirect ( url ) <EOL> self . session [ '<STR_LIT>' ] = url <EOL> if self . request . args . get ( '<STR_LIT>' , None ) : <EOL> return self . get_authenticated_user ( self . _on_auth ) <EOL> return self . authenticate_redirect ( ) <EOL> def _on_auth ( self , user ) : <EOL> """<STR_LIT:U+0020>""" <EOL> if not user : <EOL> self . abort ( <NUM_LIT> ) <EOL> username = user . pop ( '<STR_LIT:username>' , None ) <EOL> if not username : <EOL> username = user . pop ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> auth_id = '<STR_LIT>' % username <EOL> self . auth . login_with_auth_id ( auth_id , remember = True , <EOL> session_key = user . get ( '<STR_LIT>' ) ) <EOL> return self . _on_auth_redirect ( ) <EOL> class FriendFeedAuthHandler ( BaseHandler , FriendFeedMixin ) : <EOL> """<STR_LIT:U+0020>""" <EOL> def get ( self ) : <EOL> url = self . redirect_path ( ) <EOL> if self . auth . session : <EOL> return self . redirect ( url ) <EOL> self . session [ '<STR_LIT>' ] = url <EOL> if self . request . args . get ( '<STR_LIT>' , None ) : <EOL> return self . get_authenticated_user ( self . _on_auth ) <EOL> return self . authorize_redirect ( ) <EOL> def _on_auth ( self , user ) : <EOL> if not user : <EOL> self . abort ( <NUM_LIT> ) <EOL> auth_id = '<STR_LIT>' % user . pop ( '<STR_LIT:username>' , '<STR_LIT>' ) <EOL> self . auth . login_with_auth_id ( auth_id , remember = True , <EOL> access_token = user . get ( '<STR_LIT>' ) ) <EOL> return self . _on_auth_redirect ( ) <EOL> class TwitterAuthHandler ( BaseHandler , TwitterMixin ) : <EOL> def get ( self ) : <EOL> url = self . redirect_path ( ) <EOL> if self . auth . user : <EOL> return self . redirect ( url ) <EOL> self . session [ '<STR_LIT>' ] = url <EOL> if self . request . args . get ( '<STR_LIT>' , None ) : <EOL> return self . get_authenticated_user ( self . _on_auth ) <EOL> return self . authorize_redirect ( callback_uri = '<STR_LIT>' ) <EOL> def _on_auth ( self , user ) : <EOL> if not user : <EOL> self . abort ( <NUM_LIT> ) <EOL> auth_id = '<STR_LIT>' % user . pop ( '<STR_LIT:username>' , '<STR_LIT>' ) <EOL> self . auth . login_with_auth_id ( auth_id , remember = True , <EOL> access_token = user . get ( '<STR_LIT>' ) ) <EOL> return self . _on_auth_redirect ( ) <EOL> class GoogleAuthHandler ( BaseHandler , GoogleMixin ) : <EOL> def get ( self ) : <EOL> url = self . redirect_path ( ) <EOL> if self . auth . session : <EOL> return self . redirect ( url ) <EOL> self . session [ '<STR_LIT>' ] = url <EOL> if self . request . args . get ( '<STR_LIT>' , None ) : <EOL> return self . get_authenticated_user ( self . _on_auth ) <EOL> return self . authenticate_redirect ( ) <EOL> def _on_auth ( self , user ) : <EOL> if not user : <EOL> self . abort ( <NUM_LIT> ) <EOL> auth_id = '<STR_LIT>' % user . pop ( '<STR_LIT:email>' , '<STR_LIT>' ) <EOL> self . auth . login_with_auth_id ( auth_id , remember = True ) <EOL> return self . _on_auth_redirect ( ) </s>
