<s> from __future__ import unicode_literals <EOL> import balanced <EOL> from flask import session , redirect , url_for , flash , request <EOL> from sqlalchemy . exc import IntegrityError <EOL> from rentmybike . controllers import route , authenticated , validate <EOL> from rentmybike . db import Session <EOL> from rentmybike . forms . accounts import AccountForm , LoginForm <EOL> from rentmybike . models . users import User <EOL> @ route ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def login_show ( login_form = None ) : <EOL> login_form = login_form or LoginForm ( prefix = '<STR_LIT>' ) <EOL> return '<STR_LIT>' , { <EOL> '<STR_LIT>' : login_form , <EOL> } <EOL> @ route ( '<STR_LIT>' , methods = [ '<STR_LIT:POST>' ] ) <EOL> @ validate ( LoginForm , prefix = '<STR_LIT>' ) <EOL> def login ( ** kwargs ) : <EOL> login_form = kwargs . pop ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> if login_form . validate ( ) : <EOL> try : <EOL> user = login_form . login ( ) <EOL> except Exception as ex : <EOL> if ex . message == '<STR_LIT>' : <EOL> flash ( '<STR_LIT>' ) <EOL> return index ( login_form ) <EOL> raise <EOL> if not user : <EOL> flash ( '<STR_LIT>' , '<STR_LIT:error>' ) <EOL> return login_show ( login_form ) <EOL> user . lookup_balanced_customer ( ) <EOL> Session . commit ( ) <EOL> session [ '<STR_LIT>' ] = user . guid <EOL> return redirect ( request . args . get ( '<STR_LIT>' , <EOL> url_for ( '<STR_LIT>' ) ) <EOL> ) <EOL> return login_show ( login_form ) <EOL> @ route ( '<STR_LIT>' ) <EOL> def logout ( login_form = None ) : <EOL> session . pop ( '<STR_LIT>' , None ) <EOL> return redirect ( '<STR_LIT:/>' ) <EOL> @ route ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> @ authenticated ( ) <EOL> def index ( ) : <EOL> return '<STR_LIT>' , { } <EOL> @ route ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def new ( account_form = None ) : <EOL> account_form = account_form or AccountForm ( prefix = '<STR_LIT>' ) <EOL> return '<STR_LIT>' , { <EOL> '<STR_LIT>' : account_form , <EOL> } <EOL> @ route ( '<STR_LIT>' , '<STR_LIT>' , methods = [ '<STR_LIT:POST>' , '<STR_LIT>' ] ) <EOL> @ validate ( AccountForm , prefix = '<STR_LIT>' ) <EOL> def create ( ** kwargs ) : <EOL> account_form = kwargs . pop ( '<STR_LIT>' ) [ <NUM_LIT:0> ] <EOL> if account_form . validate ( ) : <EOL> user = account_form . create_user ( ) <EOL> Session . add ( user ) <EOL> try : <EOL> Session . commit ( ) <EOL> except IntegrityError : <EOL> flash ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> Session . rollback ( ) <EOL> else : <EOL> user . lookup_balanced_customer ( ) <EOL> Session . commit ( ) <EOL> session [ '<STR_LIT>' ] = user . guid <EOL> return redirect ( url_for ( '<STR_LIT>' ) ) <EOL> return new ( account_form ) <EOL> @ route ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> def verify ( ) : <EOL> if '<STR_LIT:email>' not in request . args : <EOL> return redirect ( '<STR_LIT:/>' ) <EOL> email = request . args [ '<STR_LIT:email>' ] <EOL> listing_id = request . args [ '<STR_LIT>' ] <EOL> merchant_href = request . args [ '<STR_LIT>' ] <EOL> marketplace = balanced . Marketplace . my_marketplace <EOL> try : <EOL> account = balanced . Customer ( <EOL> email = email , merchant_href = merchant_href ) . save ( ) <EOL> except balanced . exc . HTTPError as ex : <EOL> if getattr ( ex , '<STR_LIT>' , None ) == '<STR_LIT>' : <EOL> account = marketplace . accounts . filter ( <EOL> email = email ) . one ( ) <EOL> else : <EOL> raise <EOL> if account : <EOL> user = User . create_guest_user ( email = email ) <EOL> user . associate_balanced_customer ( account . href ) <EOL> Session . commit ( ) <EOL> session [ '<STR_LIT:email>' ] = email <EOL> return redirect ( url_for ( '<STR_LIT>' , listing = listing_id ) ) </s>
