<s> from __future__ import unicode_literals <EOL> import urllib <EOL> from decorator import decorator <EOL> from flask import request , redirect , url_for <EOL> from sqlalchemy . orm . exc import NoResultFound <EOL> from werkzeug . datastructures import ImmutableMultiDict <EOL> from werkzeug . routing import BaseConverter , ValidationError <EOL> from rentmybike import app <EOL> from rentmybike . models import Rental , Listing <EOL> def route ( rule , endpoint = None , ** options ) : <EOL> def wrap ( f ) : <EOL> app . add_url_rule ( rule , endpoint , f , ** options ) <EOL> return f <EOL> return wrap <EOL> def authenticated ( ) : <EOL> def wrap ( controller ) : <EOL> def auth ( * _ , ** __ ) : <EOL> if not request . user or not request . user . is_authenticated : <EOL> url = url_for ( '<STR_LIT>' , done = request . url ) <EOL> return redirect ( url ) <EOL> og_init = controller . __init__ <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> og_init ( self , * args , ** kwargs ) <EOL> self . _before . append ( auth ) <EOL> controller . __init__ = __init__ <EOL> return controller <EOL> return wrap <EOL> def unauthenticated ( redirect_to ) : <EOL> def wrap ( controller ) : <EOL> def auth ( * _ , ** __ ) : <EOL> if request . user . is_authenticated : <EOL> return redirect ( redirect_to ) <EOL> og_init = controller . __init__ <EOL> def __init__ ( self , * args , ** kwargs ) : <EOL> og_init ( self , * args , ** kwargs ) <EOL> self . _before . append ( auth ) <EOL> controller . __init__ = __init__ <EOL> return controller <EOL> return wrap <EOL> def validate ( form_type , prefix = '<STR_LIT>' ) : <EOL> """<STR_LIT>""" <EOL> @ decorator <EOL> def validation_func ( function , * args , ** kwargs ) : <EOL> single_field = request . form . get ( '<STR_LIT>' ) <EOL> existing_forms = kwargs . pop ( '<STR_LIT>' , [ ] ) <EOL> if single_field : <EOL> single_field_value = request . form . get ( single_field ) <EOL> form = form_type ( <EOL> ImmutableMultiDict ( { single_field : single_field_value } ) , <EOL> prefix = prefix , <EOL> ) <EOL> else : <EOL> form = form_type ( request . form , prefix = prefix ) <EOL> form . validate ( ) <EOL> existing_forms . append ( form ) <EOL> return function ( * args , forms = existing_forms , ** kwargs ) <EOL> return validation_func <EOL> def find_form ( forms , form_type ) : <EOL> return [ f for f in forms if isinstance ( f , form_type ) ] [ <NUM_LIT:0> ] <EOL> class ModelConverter ( BaseConverter ) : <EOL> model = None <EOL> name = None <EOL> field = '<STR_LIT:id>' <EOL> def __init__ ( self , url_map , field = None ) : <EOL> super ( ModelConverter , self ) . __init__ ( url_map ) <EOL> self . field = field or self . field <EOL> self . regex = '<STR_LIT>' <EOL> def to_python ( self , value ) : <EOL> field = getattr ( self . model , self . field ) <EOL> q = self . model . query . filter ( field == value ) <EOL> try : <EOL> return q . one ( ) <EOL> except NoResultFound : <EOL> raise ValidationError ( ) <EOL> def to_url ( self , value ) : <EOL> if isinstance ( value , basestring ) : <EOL> return value <EOL> if isinstance ( value , int ) : <EOL> return str ( value ) <EOL> return str ( getattr ( value , self . field ) ) <EOL> class ListingConverter ( ModelConverter ) : <EOL> model = Listing <EOL> name = '<STR_LIT>' <EOL> class RentalConverter ( ModelConverter ) : <EOL> model = Rental <EOL> name = '<STR_LIT>' <EOL> field = '<STR_LIT>' <EOL> app . url_map . converters . update ( { <EOL> ListingConverter . name : ListingConverter , <EOL> RentalConverter . name : RentalConverter , <EOL> } ) <EOL> import accounts <EOL> import list <EOL> import rent <EOL> import transactions </s>
