<s> from os import path <EOL> from urlparse import urlparse <EOL> from random import sample , randrange <EOL> from jinja2 import Environment , FileSystemLoader <EOL> from werkzeug . local import Local , LocalManager <EOL> from werkzeug . utils import cached_property <EOL> from werkzeug . wrappers import Response <EOL> from werkzeug . routing import Map , Rule <EOL> from sqlalchemy import MetaData <EOL> from sqlalchemy . orm import create_session , scoped_session <EOL> TEMPLATE_PATH = path . join ( path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> STATIC_PATH = path . join ( path . dirname ( __file__ ) , '<STR_LIT>' ) <EOL> ALLOWED_SCHEMES = frozenset ( [ '<STR_LIT:http>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] ) <EOL> URL_CHARS = '<STR_LIT>' <EOL> local = Local ( ) <EOL> local_manager = LocalManager ( [ local ] ) <EOL> application = local ( '<STR_LIT>' ) <EOL> metadata = MetaData ( ) <EOL> url_map = Map ( [ Rule ( '<STR_LIT>' , endpoint = '<STR_LIT>' , build_only = True ) ] ) <EOL> session = scoped_session ( lambda : create_session ( application . database_engine , <EOL> autocommit = False , <EOL> autoflush = False ) ) <EOL> jinja_env = Environment ( loader = FileSystemLoader ( TEMPLATE_PATH ) ) <EOL> def expose ( rule , ** kw ) : <EOL> def decorate ( f ) : <EOL> kw [ '<STR_LIT>' ] = f . __name__ <EOL> url_map . add ( Rule ( rule , ** kw ) ) <EOL> return f <EOL> return decorate <EOL> def url_for ( endpoint , _external = False , ** values ) : <EOL> return local . url_adapter . build ( endpoint , values , force_external = _external ) <EOL> jinja_env . globals [ '<STR_LIT>' ] = url_for <EOL> def render_template ( template , ** context ) : <EOL> return Response ( jinja_env . get_template ( template ) . render ( ** context ) , <EOL> mimetype = '<STR_LIT>' ) <EOL> def validate_url ( url ) : <EOL> return urlparse ( url ) [ <NUM_LIT:0> ] in ALLOWED_SCHEMES <EOL> def get_random_uid ( ) : <EOL> return '<STR_LIT>' . join ( sample ( URL_CHARS , randrange ( <NUM_LIT:3> , <NUM_LIT:9> ) ) ) <EOL> class Pagination ( object ) : <EOL> def __init__ ( self , query , per_page , page , endpoint ) : <EOL> self . query = query <EOL> self . per_page = per_page <EOL> self . page = page <EOL> self . endpoint = endpoint <EOL> @ cached_property <EOL> def count ( self ) : <EOL> return self . query . count ( ) <EOL> @ cached_property <EOL> def entries ( self ) : <EOL> return self . query . offset ( ( self . page - <NUM_LIT:1> ) * self . per_page ) . limit ( self . per_page ) . all ( ) <EOL> has_previous = property ( lambda x : x . page > <NUM_LIT:1> ) <EOL> has_next = property ( lambda x : x . page < x . pages ) <EOL> previous = property ( lambda x : url_for ( x . endpoint , page = x . page - <NUM_LIT:1> ) ) <EOL> next = property ( lambda x : url_for ( x . endpoint , page = x . page + <NUM_LIT:1> ) ) <EOL> pages = property ( lambda x : max ( <NUM_LIT:0> , x . count - <NUM_LIT:1> ) // x . per_page + <NUM_LIT:1> ) </s>
