<s> """<STR_LIT>""" <EOL> from gluon . storage import Storage , List <EOL> from gluon . streamer import streamer , stream_file_or_304_or_206 , DEFAULT_CHUNK_SIZE <EOL> from gluon . xmlrpc import handler <EOL> from gluon . contenttype import contenttype <EOL> from gluon . html import xmlescape , TABLE , TR , PRE , URL <EOL> from gluon . http import HTTP , redirect <EOL> from gluon . fileutils import up <EOL> from gluon . serializers import json , custom_json <EOL> import gluon . settings as settings <EOL> from gluon . utils import web2py_uuid , secure_dumps , secure_loads <EOL> from gluon . settings import global_settings <EOL> import hashlib <EOL> import portalocker <EOL> import cPickle <EOL> from pickle import Pickler , MARK , DICT , EMPTY_DICT <EOL> from types import DictionaryType <EOL> import cStringIO <EOL> import datetime <EOL> import re <EOL> import Cookie <EOL> import os <EOL> import sys <EOL> import traceback <EOL> import threading <EOL> import cgi <EOL> import copy <EOL> import tempfile <EOL> from gluon . cache import CacheInRam <EOL> from gluon . fileutils import copystream <EOL> FMT = '<STR_LIT>' <EOL> PAST = '<STR_LIT>' <EOL> FUTURE = '<STR_LIT>' <EOL> try : <EOL> from gluon . contrib . minify import minify <EOL> have_minify = True <EOL> except ImportError : <EOL> have_minify = False <EOL> try : <EOL> import simplejson as sj <EOL> except : <EOL> try : <EOL> import json as sj <EOL> except : <EOL> import gluon . contrib . simplejson as sj <EOL> regex_session_id = re . compile ( '<STR_LIT>' ) <EOL> __all__ = [ '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ] <EOL> current = threading . local ( ) <EOL> css_template = '<STR_LIT>' <EOL> js_template = '<STR_LIT>' <EOL> coffee_template = '<STR_LIT>' <EOL> typescript_template = '<STR_LIT>' <EOL> less_template = '<STR_LIT>' <EOL> css_inline = '<STR_LIT>' <EOL> js_inline = '<STR_LIT>' <EOL> class SortingPickler ( Pickler ) : <EOL> def save_dict ( self , obj ) : <EOL> self . write ( EMPTY_DICT if self . bin else MARK + DICT ) <EOL> self . memoize ( obj ) <EOL> self . _batch_setitems ( [ ( key , obj [ key ] ) for key in sorted ( obj ) ] ) <EOL> SortingPickler . dispatch = copy . copy ( Pickler . dispatch ) <EOL> SortingPickler . dispatch [ DictionaryType ] = SortingPickler . save_dict <EOL> def sorting_dumps ( obj , protocol = None ) : <EOL> file = cStringIO . StringIO ( ) <EOL> SortingPickler ( file , protocol ) . dump ( obj ) <EOL> return file . getvalue ( ) <EOL> def copystream_progress ( request , chunk_size = <NUM_LIT:10> ** <NUM_LIT:5> ) : <EOL> """<STR_LIT>""" <EOL> env = request . env <EOL> if not env . get ( '<STR_LIT>' , None ) : <EOL> return cStringIO . StringIO ( ) <EOL> source = env [ '<STR_LIT>' ] <EOL> try : <EOL> size = int ( env [ '<STR_LIT>' ] ) <EOL> except ValueError : <EOL> raise HTTP ( <NUM_LIT> , "<STR_LIT>" ) <EOL> try : <EOL> dest = tempfile . NamedTemporaryFile ( ) <EOL> except NotImplementedError : <EOL> dest = tempfile . TemporaryFile ( ) <EOL> if not '<STR_LIT>' in request . get_vars : <EOL> copystream ( source , dest , size , chunk_size ) <EOL> return dest <EOL> cache_key = '<STR_LIT>' + request . get_vars [ '<STR_LIT>' ] <EOL> cache_ram = CacheInRam ( request ) <EOL> cache_ram ( cache_key + '<STR_LIT>' , lambda : size , <NUM_LIT:0> ) <EOL> cache_ram ( cache_key + '<STR_LIT>' , lambda : <NUM_LIT:0> , <NUM_LIT:0> ) <EOL> while size > <NUM_LIT:0> : <EOL> if size < chunk_size : <EOL> data = source . read ( size ) <EOL> cache_ram . increment ( cache_key + '<STR_LIT>' , size ) <EOL> else : <EOL> data = source . read ( chunk_size ) <EOL> cache_ram . increment ( cache_key + '<STR_LIT>' , chunk_size ) <EOL> length = len ( data ) <EOL> if length > size : <EOL> ( data , length ) = ( data [ : size ] , size ) <EOL> size -= length <EOL> if length == <NUM_LIT:0> : <EOL> break <EOL> dest . write ( data ) <EOL> if length < chunk_size : <EOL> break <EOL> dest . seek ( <NUM_LIT:0> ) <EOL> cache_ram ( cache_key + '<STR_LIT>' , None ) <EOL> cache_ram ( cache_key + '<STR_LIT>' , None ) <EOL> return dest <EOL> class Request ( Storage ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , env ) : <EOL> Storage . __init__ ( self ) <EOL> self . env = Storage ( env ) <EOL> self . env . web2py_path = global_settings . applications_parent <EOL> self . env . update ( global_settings ) <EOL> self . cookies = Cookie . SimpleCookie ( ) <EOL> self . _get_vars = None <EOL> self . _post_vars = None <EOL> self . _vars = None <EOL> self . _body = None <EOL> self . folder = None <EOL> self . application = None <EOL> self . function = None <EOL> self . args = List ( ) <EOL> self . extension = '<STR_LIT:html>' <EOL> self . now = datetime . datetime . now ( ) <EOL> self . utcnow = datetime . datetime . utcnow ( ) <EOL> self . is_restful = False <EOL> self . is_https = False <EOL> self . is_local = False <EOL> self . global_settings = settings . global_settings <EOL> def parse_get_vars ( self ) : <EOL> """<STR_LIT>""" <EOL> query_string = self . env . get ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> dget = cgi . parse_qs ( query_string , keep_blank_values = <NUM_LIT:1> ) <EOL> get_vars = self . _get_vars = Storage ( dget ) <EOL> for ( key , value ) in get_vars . iteritems ( ) : <EOL> if isinstance ( value , list ) and len ( value ) == <NUM_LIT:1> : <EOL> get_vars [ key ] = value [ <NUM_LIT:0> ] <EOL> def parse_post_vars ( self ) : <EOL> """<STR_LIT>""" <EOL> env = self . env <EOL> post_vars = self . _post_vars = Storage ( ) <EOL> body = self . body <EOL> is_json = env . get ( '<STR_LIT>' , '<STR_LIT>' ) [ : <NUM_LIT:16> ] == '<STR_LIT:application/json>' <EOL> if is_json : <EOL> try : <EOL> json_vars = sj . load ( body ) <EOL> except : <EOL> json_vars = { } <EOL> pass <EOL> if isinstance ( json_vars , dict ) : <EOL> post_vars . update ( json_vars ) <EOL> body . seek ( <NUM_LIT:0> ) <EOL> if ( body and not is_json <EOL> and env . request_method in ( '<STR_LIT:POST>' , '<STR_LIT>' , '<STR_LIT>' , '<STR_LIT>' ) ) : <EOL> query_string = env . pop ( '<STR_LIT>' , None ) <EOL> dpost = cgi . FieldStorage ( fp = body , environ = env , keep_blank_values = <NUM_LIT:1> ) <EOL> try : <EOL> post_vars . update ( dpost ) <EOL> except : pass <EOL> if query_string is not None : <EOL> env [ '<STR_LIT>' ] = query_string <EOL> body . seek ( <NUM_LIT:0> ) <EOL> def listify ( a ) : <EOL> return ( not isinstance ( a , list ) and [ a ] ) or a <EOL> try : <EOL> keys = sorted ( dpost ) <EOL> except TypeError : <EOL> keys = [ ] <EOL> for key in keys : <EOL> if key is None : <EOL> continue <EOL> dpk = dpost [ key ] <EOL> pvalue = listify ( [ ( _dpk if _dpk . filename else _dpk . value ) <EOL> for _dpk in dpk ] <EOL> if isinstance ( dpk , list ) else <EOL> ( dpk if dpk . filename else dpk . value ) ) <EOL> if len ( pvalue ) : <EOL> post_vars [ key ] = ( len ( pvalue ) > <NUM_LIT:1> and pvalue ) or pvalue [ <NUM_LIT:0> ] <EOL> @ property <EOL> def body ( self ) : <EOL> if self . _body is None : <EOL> try : <EOL> self . _body = copystream_progress ( self ) <EOL> except IOError : <EOL> raise HTTP ( <NUM_LIT> , "<STR_LIT>" ) <EOL> return self . _body <EOL> def parse_all_vars ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _vars = copy . copy ( self . get_vars ) <EOL> for key , value in self . post_vars . iteritems ( ) : <EOL> if not key in self . _vars : <EOL> self . _vars [ key ] = value <EOL> else : <EOL> if not isinstance ( self . _vars [ key ] , list ) : <EOL> self . _vars [ key ] = [ self . _vars [ key ] ] <EOL> self . _vars [ key ] += value if isinstance ( value , list ) else [ value ] <EOL> @ property <EOL> def get_vars ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _get_vars is None : <EOL> self . parse_get_vars ( ) <EOL> return self . _get_vars <EOL> @ property <EOL> def post_vars ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _post_vars is None : <EOL> self . parse_post_vars ( ) <EOL> return self . _post_vars <EOL> @ property <EOL> def vars ( self ) : <EOL> """<STR_LIT>""" <EOL> if self . _vars is None : <EOL> self . parse_all_vars ( ) <EOL> return self . _vars <EOL> def compute_uuid ( self ) : <EOL> self . uuid = '<STR_LIT>' % ( <EOL> self . application , <EOL> self . client . replace ( '<STR_LIT::>' , '<STR_LIT:_>' ) , <EOL> self . now . strftime ( '<STR_LIT>' ) , <EOL> web2py_uuid ( ) ) <EOL> return self . uuid <EOL> def user_agent ( self ) : <EOL> from gluon . contrib import user_agent_parser <EOL> session = current . session <EOL> user_agent = session . _user_agent <EOL> if user_agent : <EOL> return user_agent <EOL> user_agent = user_agent_parser . detect ( self . env . http_user_agent ) <EOL> for key , value in user_agent . items ( ) : <EOL> if isinstance ( value , dict ) : <EOL> user_agent [ key ] = Storage ( value ) <EOL> user_agent = session . _user_agent = Storage ( user_agent ) <EOL> return user_agent <EOL> def requires_https ( self ) : <EOL> """<STR_LIT>""" <EOL> cmd_opts = global_settings . cmd_options <EOL> if ( ( cmd_opts and ( cmd_opts . shell or cmd_opts . scheduler ) ) <EOL> or global_settings . cronjob or self . is_https ) : <EOL> current . session . secure ( ) <EOL> else : <EOL> current . session . forget ( ) <EOL> redirect ( URL ( scheme = '<STR_LIT>' , args = self . args , vars = self . vars ) ) <EOL> def restful ( self ) : <EOL> def wrapper ( action , self = self ) : <EOL> def f ( _action = action , _self = self , * a , ** b ) : <EOL> self . is_restful = True <EOL> method = _self . env . request_method <EOL> if len ( _self . args ) and '<STR_LIT:.>' in _self . args [ - <NUM_LIT:1> ] : <EOL> _self . args [ - <NUM_LIT:1> ] , _ , self . extension = self . args [ - <NUM_LIT:1> ] . rpartition ( '<STR_LIT:.>' ) <EOL> current . response . headers [ '<STR_LIT:Content-Type>' ] = contenttype ( '<STR_LIT:.>' + _self . extension . lower ( ) ) <EOL> rest_action = _action ( ) . get ( method , None ) <EOL> if not ( rest_action and method == method . upper ( ) <EOL> and callable ( rest_action ) ) : <EOL> raise HTTP ( <NUM_LIT> , "<STR_LIT>" ) <EOL> try : <EOL> return rest_action ( * _self . args , ** getattr ( _self , '<STR_LIT>' , { } ) ) <EOL> except TypeError , e : <EOL> exc_type , exc_value , exc_traceback = sys . exc_info ( ) <EOL> if len ( traceback . extract_tb ( exc_traceback ) ) == <NUM_LIT:1> : <EOL> raise HTTP ( <NUM_LIT> , "<STR_LIT>" ) <EOL> else : <EOL> raise e <EOL> f . __doc__ = action . __doc__ <EOL> f . __name__ = action . __name__ <EOL> return f <EOL> return wrapper <EOL> class Response ( Storage ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self ) : <EOL> Storage . __init__ ( self ) <EOL> self . status = <NUM_LIT:200> <EOL> self . headers = dict ( ) <EOL> self . headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> self . body = cStringIO . StringIO ( ) <EOL> self . session_id = None <EOL> self . cookies = Cookie . SimpleCookie ( ) <EOL> self . postprocessing = [ ] <EOL> self . flash = '<STR_LIT>' <EOL> self . meta = Storage ( ) <EOL> self . menu = [ ] <EOL> self . files = [ ] <EOL> self . generic_patterns = [ ] <EOL> self . delimiters = ( '<STR_LIT>' , '<STR_LIT>' ) <EOL> self . _vars = None <EOL> self . _caller = lambda f : f ( ) <EOL> self . _view_environment = None <EOL> self . _custom_commit = None <EOL> self . _custom_rollback = None <EOL> def write ( self , data , escape = True ) : <EOL> if not escape : <EOL> self . body . write ( str ( data ) ) <EOL> else : <EOL> self . body . write ( xmlescape ( data ) ) <EOL> def render ( self , * a , ** b ) : <EOL> from compileapp import run_view_in <EOL> if len ( a ) > <NUM_LIT:2> : <EOL> raise SyntaxError ( <EOL> '<STR_LIT>' ) <EOL> elif len ( a ) == <NUM_LIT:2> : <EOL> ( view , self . _vars ) = ( a [ <NUM_LIT:0> ] , a [ <NUM_LIT:1> ] ) <EOL> elif len ( a ) == <NUM_LIT:1> and isinstance ( a [ <NUM_LIT:0> ] , str ) : <EOL> ( view , self . _vars ) = ( a [ <NUM_LIT:0> ] , { } ) <EOL> elif len ( a ) == <NUM_LIT:1> and hasattr ( a [ <NUM_LIT:0> ] , '<STR_LIT>' ) and callable ( a [ <NUM_LIT:0> ] . read ) : <EOL> ( view , self . _vars ) = ( a [ <NUM_LIT:0> ] , { } ) <EOL> elif len ( a ) == <NUM_LIT:1> and isinstance ( a [ <NUM_LIT:0> ] , dict ) : <EOL> ( view , self . _vars ) = ( None , a [ <NUM_LIT:0> ] ) <EOL> else : <EOL> ( view , self . _vars ) = ( None , { } ) <EOL> self . _vars . update ( b ) <EOL> self . _view_environment . update ( self . _vars ) <EOL> if view : <EOL> import cStringIO <EOL> ( obody , oview ) = ( self . body , self . view ) <EOL> ( self . body , self . view ) = ( cStringIO . StringIO ( ) , view ) <EOL> run_view_in ( self . _view_environment ) <EOL> page = self . body . getvalue ( ) <EOL> self . body . close ( ) <EOL> ( self . body , self . view ) = ( obody , oview ) <EOL> else : <EOL> run_view_in ( self . _view_environment ) <EOL> page = self . body . getvalue ( ) <EOL> return page <EOL> def include_meta ( self ) : <EOL> s = '<STR_LIT:\n>' . join ( <EOL> '<STR_LIT>' % ( k , xmlescape ( v ) ) <EOL> for k , v in ( self . meta or { } ) . iteritems ( ) ) <EOL> self . write ( s , escape = False ) <EOL> def include_files ( self , extensions = None ) : <EOL> """<STR_LIT>""" <EOL> from gluon import URL <EOL> files = [ ] <EOL> has_js = has_css = False <EOL> for item in self . files : <EOL> if extensions and not item . split ( '<STR_LIT:.>' ) [ - <NUM_LIT:1> ] in extensions : <EOL> continue <EOL> if item in files : <EOL> continue <EOL> if item . endswith ( '<STR_LIT>' ) : <EOL> has_js = True <EOL> if item . endswith ( '<STR_LIT>' ) : <EOL> has_css = True <EOL> files . append ( item ) <EOL> if have_minify and ( ( self . optimize_css and has_css ) or ( self . optimize_js and has_js ) ) : <EOL> key = hashlib . md5 ( repr ( files ) ) . hexdigest ( ) <EOL> cache = self . cache_includes or ( current . cache . ram , <NUM_LIT> * <NUM_LIT:5> ) <EOL> def call_minify ( files = files ) : <EOL> return minify . minify ( files , <EOL> URL ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> current . request . folder , <EOL> self . optimize_css , <EOL> self . optimize_js ) <EOL> if cache : <EOL> cache_model , time_expire = cache <EOL> files = cache_model ( '<STR_LIT>' + key , <EOL> call_minify , <EOL> time_expire ) <EOL> else : <EOL> files = call_minify ( ) <EOL> s = '<STR_LIT>' <EOL> for item in files : <EOL> if isinstance ( item , str ) : <EOL> f = item . lower ( ) . split ( '<STR_LIT:?>' ) [ <NUM_LIT:0> ] <EOL> if self . static_version : <EOL> item = item . replace ( <EOL> '<STR_LIT>' , '<STR_LIT>' % self . static_version , <NUM_LIT:1> ) <EOL> if f . endswith ( '<STR_LIT>' ) : <EOL> s += css_template % item <EOL> elif f . endswith ( '<STR_LIT>' ) : <EOL> s += js_template % item <EOL> elif f . endswith ( '<STR_LIT>' ) : <EOL> s += coffee_template % item <EOL> elif f . endswith ( '<STR_LIT>' ) : <EOL> s += typescript_template % item <EOL> elif f . endswith ( '<STR_LIT>' ) : <EOL> s += less_template % item <EOL> elif isinstance ( item , ( list , tuple ) ) : <EOL> f = item [ <NUM_LIT:0> ] <EOL> if f == '<STR_LIT>' : <EOL> s += css_inline % item [ <NUM_LIT:1> ] <EOL> elif f == '<STR_LIT>' : <EOL> s += js_inline % item [ <NUM_LIT:1> ] <EOL> self . write ( s , escape = False ) <EOL> def stream ( <EOL> self , <EOL> stream , <EOL> chunk_size = DEFAULT_CHUNK_SIZE , <EOL> request = None , <EOL> attachment = False , <EOL> filename = None <EOL> ) : <EOL> """<STR_LIT>""" <EOL> headers = self . headers <EOL> keys = [ item . lower ( ) for item in headers ] <EOL> if attachment : <EOL> if filename is None : <EOL> attname = "<STR_LIT>" <EOL> else : <EOL> attname = filename <EOL> headers [ "<STR_LIT>" ] = "<STR_LIT>" % attname <EOL> if not request : <EOL> request = current . request <EOL> if isinstance ( stream , ( str , unicode ) ) : <EOL> stream_file_or_304_or_206 ( stream , <EOL> chunk_size = chunk_size , <EOL> request = request , <EOL> headers = headers , <EOL> status = self . status ) <EOL> if hasattr ( stream , '<STR_LIT:name>' ) : <EOL> filename = stream . name <EOL> if filename and not '<STR_LIT>' in keys : <EOL> headers [ '<STR_LIT:Content-Type>' ] = contenttype ( filename ) <EOL> if filename and not '<STR_LIT>' in keys : <EOL> try : <EOL> headers [ '<STR_LIT>' ] = os . path . getsize ( filename ) <EOL> except OSError : <EOL> pass <EOL> env = request . env <EOL> if request . is_https and isinstance ( env . http_user_agent , str ) and not re . search ( r'<STR_LIT>' , env . http_user_agent ) and re . search ( r'<STR_LIT>' , env . http_user_agent ) : <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' <EOL> if request and env . web2py_use_wsgi_file_wrapper : <EOL> wrapped = env . wsgi_file_wrapper ( stream , chunk_size ) <EOL> else : <EOL> wrapped = streamer ( stream , chunk_size = chunk_size ) <EOL> return wrapped <EOL> def download ( self , request , db , chunk_size = DEFAULT_CHUNK_SIZE , attachment = True , download_filename = None ) : <EOL> """<STR_LIT>""" <EOL> current . session . forget ( current . response ) <EOL> if not request . args : <EOL> raise HTTP ( <NUM_LIT> ) <EOL> name = request . args [ - <NUM_LIT:1> ] <EOL> items = re . compile ( '<STR_LIT>' ) . match ( name ) <EOL> if not items : <EOL> raise HTTP ( <NUM_LIT> ) <EOL> ( t , f ) = ( items . group ( '<STR_LIT>' ) , items . group ( '<STR_LIT>' ) ) <EOL> try : <EOL> field = db [ t ] [ f ] <EOL> except AttributeError : <EOL> raise HTTP ( <NUM_LIT> ) <EOL> try : <EOL> ( filename , stream ) = field . retrieve ( name , nameonly = True ) <EOL> except IOError : <EOL> raise HTTP ( <NUM_LIT> ) <EOL> headers = self . headers <EOL> headers [ '<STR_LIT:Content-Type>' ] = contenttype ( name ) <EOL> if download_filename == None : <EOL> download_filename = filename <EOL> if attachment : <EOL> headers [ '<STR_LIT>' ] = '<STR_LIT>' % download_filename . replace ( '<STR_LIT:">' , '<STR_LIT>' ) <EOL> return self . stream ( stream , chunk_size = chunk_size , request = request ) <EOL> def json ( self , data , default = None ) : <EOL> return json ( data , default = default or custom_json ) <EOL> def xmlrpc ( self , request , methods ) : <EOL> """<STR_LIT>""" <EOL> return handler ( request , self , methods ) <EOL> def toolbar ( self ) : <EOL> from html import DIV , SCRIPT , BEAUTIFY , TAG , URL , A <EOL> BUTTON = TAG . button <EOL> admin = URL ( "<STR_LIT>" , "<STR_LIT:default>" , "<STR_LIT>" , extension = '<STR_LIT:html>' , <EOL> args = current . request . application ) <EOL> from gluon . dal import DAL <EOL> dbstats = [ ] <EOL> dbtables = { } <EOL> infos = DAL . get_instances ( ) <EOL> for k , v in infos . iteritems ( ) : <EOL> dbstats . append ( TABLE ( * [ TR ( PRE ( row [ <NUM_LIT:0> ] ) , '<STR_LIT>' % <EOL> ( row [ <NUM_LIT:1> ] * <NUM_LIT:1000> ) ) <EOL> for row in v [ '<STR_LIT>' ] ] ) ) <EOL> dbtables [ k ] = dict ( defined = v [ '<STR_LIT>' ] [ '<STR_LIT>' ] or '<STR_LIT>' , <EOL> lazy = v [ '<STR_LIT>' ] [ '<STR_LIT>' ] or '<STR_LIT>' ) <EOL> u = web2py_uuid ( ) <EOL> backtotop = A ( '<STR_LIT>' , _href = "<STR_LIT>" % u ) <EOL> request = copy . copy ( current . request ) <EOL> request . update ( vars = current . request . vars , <EOL> get_vars = current . request . get_vars , <EOL> post_vars = current . request . post_vars ) <EOL> return DIV ( <EOL> BUTTON ( '<STR_LIT>' , _onclick = "<STR_LIT>" % admin ) , <EOL> BUTTON ( '<STR_LIT>' , <EOL> _onclick = "<STR_LIT>" % u ) , <EOL> BUTTON ( '<STR_LIT>' , <EOL> _onclick = "<STR_LIT>" % u ) , <EOL> BUTTON ( '<STR_LIT>' , <EOL> _onclick = "<STR_LIT>" % u ) , <EOL> BUTTON ( '<STR_LIT>' , <EOL> _onclick = "<STR_LIT>" % u ) , <EOL> BUTTON ( '<STR_LIT>' , <EOL> _onclick = "<STR_LIT>" % u ) , <EOL> DIV ( BEAUTIFY ( request ) , backtotop , <EOL> _class = "<STR_LIT>" , _id = "<STR_LIT>" % u ) , <EOL> DIV ( BEAUTIFY ( current . session ) , backtotop , <EOL> _class = "<STR_LIT>" , _id = "<STR_LIT>" % u ) , <EOL> DIV ( BEAUTIFY ( current . response ) , backtotop , <EOL> _class = "<STR_LIT>" , _id = "<STR_LIT>" % u ) , <EOL> DIV ( BEAUTIFY ( dbtables ) , backtotop , _class = "<STR_LIT>" , <EOL> _id = "<STR_LIT>" % u ) , <EOL> DIV ( BEAUTIFY ( <EOL> dbstats ) , backtotop , _class = "<STR_LIT>" , _id = "<STR_LIT>" % u ) , <EOL> SCRIPT ( "<STR_LIT>" ) , _id = "<STR_LIT>" % u <EOL> ) <EOL> class Session ( Storage ) : <EOL> """<STR_LIT>""" <EOL> def connect ( <EOL> self , <EOL> request = None , <EOL> response = None , <EOL> db = None , <EOL> tablename = '<STR_LIT>' , <EOL> masterapp = None , <EOL> migrate = True , <EOL> separate = None , <EOL> check_client = False , <EOL> cookie_key = None , <EOL> cookie_expires = None , <EOL> compression_level = None <EOL> ) : <EOL> """<STR_LIT>""" <EOL> request = request or current . request <EOL> response = response or current . response <EOL> masterapp = masterapp or request . application <EOL> cookies = request . cookies <EOL> self . _unlock ( response ) <EOL> response . session_masterapp = masterapp <EOL> response . session_id_name = '<STR_LIT>' % masterapp . lower ( ) <EOL> response . session_data_name = '<STR_LIT>' % masterapp . lower ( ) <EOL> response . session_cookie_expires = cookie_expires <EOL> response . session_client = str ( request . client ) . replace ( '<STR_LIT::>' , '<STR_LIT:.>' ) <EOL> response . session_cookie_key = cookie_key <EOL> response . session_cookie_compression_level = compression_level <EOL> try : <EOL> old_session_id = cookies [ response . session_id_name ] . value <EOL> except KeyError : <EOL> old_session_id = None <EOL> response . session_id = old_session_id <EOL> if cookie_key : <EOL> response . session_storage_type = '<STR_LIT>' <EOL> elif db : <EOL> response . session_storage_type = '<STR_LIT>' <EOL> else : <EOL> response . session_storage_type = '<STR_LIT:file>' <EOL> if ( global_settings . db_sessions is True or <EOL> masterapp in global_settings . db_sessions ) : <EOL> return <EOL> if response . session_storage_type == '<STR_LIT>' : <EOL> if response . session_data_name in cookies : <EOL> session_cookie_data = cookies [ response . session_data_name ] . value <EOL> else : <EOL> session_cookie_data = None <EOL> if session_cookie_data : <EOL> data = secure_loads ( session_cookie_data , cookie_key , <EOL> compression_level = compression_level ) <EOL> if data : <EOL> self . update ( data ) <EOL> response . session_id = True <EOL> elif response . session_storage_type == '<STR_LIT:file>' : <EOL> response . session_new = False <EOL> response . session_file = None <EOL> if response . session_id : <EOL> if not regex_session_id . match ( response . session_id ) : <EOL> response . session_id = None <EOL> else : <EOL> response . session_filename = os . path . join ( up ( request . folder ) , masterapp , <EOL> '<STR_LIT>' , response . session_id ) <EOL> try : <EOL> response . session_file = open ( response . session_filename , '<STR_LIT>' ) <EOL> portalocker . lock ( response . session_file , <EOL> portalocker . LOCK_EX ) <EOL> response . session_locked = True <EOL> self . update ( cPickle . load ( response . session_file ) ) <EOL> response . session_file . seek ( <NUM_LIT:0> ) <EOL> oc = response . session_filename . split ( '<STR_LIT:/>' ) [ - <NUM_LIT:1> ] . split ( '<STR_LIT:->' ) [ <NUM_LIT:0> ] <EOL> if check_client and response . session_client != oc : <EOL> raise Exception ( "<STR_LIT>" ) <EOL> except : <EOL> response . session_id = None <EOL> if not response . session_id : <EOL> uuid = web2py_uuid ( ) <EOL> response . session_id = '<STR_LIT>' % ( response . session_client , uuid ) <EOL> separate = separate and ( lambda session_name : session_name [ - <NUM_LIT:2> : ] ) <EOL> if separate : <EOL> prefix = separate ( response . session_id ) <EOL> response . session_id = '<STR_LIT>' % ( prefix , response . session_id ) <EOL> response . session_filename = os . path . join ( up ( request . folder ) , masterapp , <EOL> '<STR_LIT>' , response . session_id ) <EOL> response . session_new = True <EOL> elif response . session_storage_type == '<STR_LIT>' : <EOL> if global_settings . db_sessions is not True : <EOL> global_settings . db_sessions . add ( masterapp ) <EOL> if response . session_file : <EOL> self . _close ( response ) <EOL> if settings . global_settings . web2py_runtime_gae : <EOL> request . tickets_db = db <EOL> if masterapp == request . application : <EOL> table_migrate = migrate <EOL> else : <EOL> table_migrate = False <EOL> tname = tablename + '<STR_LIT:_>' + masterapp <EOL> table = db . get ( tname , None ) <EOL> Field = db . Field <EOL> if table is None : <EOL> db . define_table ( <EOL> tname , <EOL> Field ( '<STR_LIT>' , '<STR_LIT>' , default = False ) , <EOL> Field ( '<STR_LIT>' , length = <NUM_LIT:64> ) , <EOL> Field ( '<STR_LIT>' , '<STR_LIT>' , <EOL> default = request . now ) , <EOL> Field ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> Field ( '<STR_LIT>' , length = <NUM_LIT:64> ) , <EOL> Field ( '<STR_LIT>' , '<STR_LIT>' ) , <EOL> migrate = table_migrate , <EOL> ) <EOL> table = db [ tname ] <EOL> response . session_db_table = table <EOL> if response . session_id : <EOL> try : <EOL> ( record_id , unique_key ) = response . session_id . split ( '<STR_LIT::>' ) <EOL> record_id = long ( record_id ) <EOL> except ( TypeError , ValueError ) : <EOL> record_id = None <EOL> if record_id : <EOL> row = table ( record_id , unique_key = unique_key ) <EOL> if row : <EOL> session_data = cPickle . loads ( row . session_data ) <EOL> self . update ( session_data ) <EOL> response . session_new = False <EOL> else : <EOL> record_id = None <EOL> if record_id : <EOL> response . session_id = '<STR_LIT>' % ( record_id , unique_key ) <EOL> response . session_db_unique_key = unique_key <EOL> response . session_db_record_id = record_id <EOL> else : <EOL> response . session_id = None <EOL> response . session_new = True <EOL> else : <EOL> response . session_new = True <EOL> if isinstance ( response . session_id , str ) : <EOL> response . cookies [ response . session_id_name ] = response . session_id <EOL> response . cookies [ response . session_id_name ] [ '<STR_LIT:path>' ] = '<STR_LIT:/>' <EOL> if cookie_expires : <EOL> response . cookies [ response . session_id_name ] [ '<STR_LIT>' ] = cookie_expires . strftime ( FMT ) <EOL> session_pickled = cPickle . dumps ( self ) <EOL> response . session_hash = hashlib . md5 ( session_pickled ) . hexdigest ( ) <EOL> if self . flash : <EOL> ( response . flash , self . flash ) = ( self . flash , None ) <EOL> def renew ( self , clear_session = False ) : <EOL> if clear_session : <EOL> self . clear ( ) <EOL> request = current . request <EOL> response = current . response <EOL> session = response . session <EOL> masterapp = response . session_masterapp <EOL> cookies = request . cookies <EOL> if response . session_storage_type == '<STR_LIT>' : <EOL> return <EOL> if response . session_storage_type == '<STR_LIT:file>' : <EOL> self . _close ( response ) <EOL> uuid = web2py_uuid ( ) <EOL> response . session_id = '<STR_LIT>' % ( response . session_client , uuid ) <EOL> separate = ( lambda s : s [ - <NUM_LIT:2> : ] ) if session and response . session_id [ <NUM_LIT:2> : <NUM_LIT:3> ] == "<STR_LIT:/>" else None <EOL> if separate : <EOL> prefix = separate ( response . session_id ) <EOL> response . session_id = '<STR_LIT>' % ( prefix , response . session_id ) <EOL> response . session_filename = os . path . join ( up ( request . folder ) , masterapp , <EOL> '<STR_LIT>' , response . session_id ) <EOL> response . session_new = True <EOL> elif response . session_storage_type == '<STR_LIT>' : <EOL> table = response . session_db_table <EOL> if response . session_file : <EOL> self . _close ( response ) <EOL> if response . session_new : <EOL> return <EOL> if response . session_id is None : <EOL> return <EOL> ( record_id , sep , unique_key ) = response . session_id . partition ( '<STR_LIT::>' ) <EOL> if record_id . isdigit ( ) and long ( record_id ) > <NUM_LIT:0> : <EOL> new_unique_key = web2py_uuid ( ) <EOL> row = table ( record_id ) <EOL> if row and row . unique_key == unique_key : <EOL> table . _db ( table . id == record_id ) . update ( unique_key = new_unique_key ) <EOL> else : <EOL> record_id = None <EOL> if record_id : <EOL> response . session_id = '<STR_LIT>' % ( record_id , new_unique_key ) <EOL> response . session_db_record_id = record_id <EOL> response . session_db_unique_key = new_unique_key <EOL> else : <EOL> response . session_new = True <EOL> def _fixup_before_save ( self ) : <EOL> response = current . response <EOL> rcookies = response . cookies <EOL> if self . _forget and response . session_id_name in rcookies : <EOL> del rcookies [ response . session_id_name ] <EOL> elif self . _secure and response . session_id_name in rcookies : <EOL> rcookies [ response . session_id_name ] [ '<STR_LIT>' ] = True <EOL> def clear_session_cookies ( sefl ) : <EOL> request = current . request <EOL> response = current . response <EOL> session = response . session <EOL> masterapp = response . session_masterapp <EOL> cookies = request . cookies <EOL> rcookies = response . cookies <EOL> if response . session_data_name in cookies : <EOL> rcookies [ response . session_data_name ] = '<STR_LIT>' <EOL> rcookies [ response . session_data_name ] [ '<STR_LIT:path>' ] = '<STR_LIT:/>' <EOL> rcookies [ response . session_data_name ] [ '<STR_LIT>' ] = PAST <EOL> if response . session_id_name in rcookies : <EOL> del rcookies [ response . session_id_name ] <EOL> def save_session_id_cookie ( self ) : <EOL> request = current . request <EOL> response = current . response <EOL> session = response . session <EOL> masterapp = response . session_masterapp <EOL> cookies = request . cookies <EOL> rcookies = response . cookies <EOL> if not response . session_cookie_key : <EOL> if response . session_data_name in cookies : <EOL> rcookies [ response . session_data_name ] = '<STR_LIT>' <EOL> rcookies [ response . session_data_name ] [ '<STR_LIT:path>' ] = '<STR_LIT:/>' <EOL> rcookies [ response . session_data_name ] [ '<STR_LIT>' ] = PAST <EOL> if response . session_id : <EOL> rcookies [ response . session_id_name ] = response . session_id <EOL> rcookies [ response . session_id_name ] [ '<STR_LIT:path>' ] = '<STR_LIT:/>' <EOL> expires = response . session_cookie_expires <EOL> if isinstance ( expires , datetime . datetime ) : <EOL> expires = expires . strftime ( FMT ) <EOL> if expires : <EOL> rcookies [ response . session_id_name ] [ '<STR_LIT>' ] = expires <EOL> def clear ( self ) : <EOL> Storage . clear ( self ) <EOL> def is_new ( self ) : <EOL> if self . _start_timestamp : <EOL> return False <EOL> else : <EOL> self . _start_timestamp = datetime . datetime . today ( ) <EOL> return True <EOL> def is_expired ( self , seconds = <NUM_LIT> ) : <EOL> now = datetime . datetime . today ( ) <EOL> if not self . _last_timestamp or self . _last_timestamp + datetime . timedelta ( seconds = seconds ) > now : <EOL> self . _last_timestamp = now <EOL> return False <EOL> else : <EOL> return True <EOL> def secure ( self ) : <EOL> self . _secure = True <EOL> def forget ( self , response = None ) : <EOL> self . _close ( response ) <EOL> self . _forget = True <EOL> def _try_store_in_cookie ( self , request , response ) : <EOL> if self . _forget or self . _unchanged ( response ) : <EOL> self . save_session_id_cookie ( ) <EOL> return False <EOL> name = response . session_data_name <EOL> compression_level = response . session_cookie_compression_level <EOL> value = secure_dumps ( dict ( self ) , <EOL> response . session_cookie_key , <EOL> compression_level = compression_level ) <EOL> rcookies = response . cookies <EOL> rcookies . pop ( name , None ) <EOL> rcookies [ name ] = value <EOL> rcookies [ name ] [ '<STR_LIT:path>' ] = '<STR_LIT:/>' <EOL> expires = response . session_cookie_expires <EOL> if isinstance ( expires , datetime . datetime ) : <EOL> expires = expires . strftime ( FMT ) <EOL> if expires : <EOL> rcookies [ name ] [ '<STR_LIT>' ] = expires <EOL> return True <EOL> def _unchanged ( self , response ) : <EOL> session_pickled = cPickle . dumps ( self ) <EOL> response . session_pickled = session_pickled <EOL> session_hash = hashlib . md5 ( session_pickled ) . hexdigest ( ) <EOL> return response . session_hash == session_hash <EOL> def _try_store_in_db ( self , request , response ) : <EOL> if ( not response . session_db_table or <EOL> self . _forget or <EOL> ( self . _unchanged ( response ) and not response . session_new ) ) : <EOL> if ( not response . session_db_table and <EOL> global_settings . db_sessions is not True and <EOL> response . session_masterapp in global_settings . db_sessions ) : <EOL> global_settings . db_sessions . remove ( response . session_masterapp ) <EOL> self . save_session_id_cookie ( ) <EOL> return False <EOL> table = response . session_db_table <EOL> record_id = response . session_db_record_id <EOL> if response . session_new : <EOL> unique_key = web2py_uuid ( ) <EOL> else : <EOL> unique_key = response . session_db_unique_key <EOL> session_pickled = response . session_pickled or cPickle . dumps ( self ) <EOL> dd = dict ( locked = False , <EOL> client_ip = response . session_client , <EOL> modified_datetime = request . now , <EOL> session_data = session_pickled , <EOL> unique_key = unique_key ) <EOL> if record_id : <EOL> if not table . _db ( table . id == record_id ) . update ( ** dd ) : <EOL> record_id = None <EOL> if not record_id : <EOL> record_id = table . insert ( ** dd ) <EOL> response . session_id = '<STR_LIT>' % ( record_id , unique_key ) <EOL> response . session_db_unique_key = unique_key <EOL> response . session_db_record_id = record_id <EOL> self . save_session_id_cookie ( ) <EOL> return True <EOL> def _try_store_in_cookie_or_file ( self , request , response ) : <EOL> if response . session_storage_type == '<STR_LIT:file>' : <EOL> return self . _try_store_in_file ( request , response ) <EOL> if response . session_storage_type == '<STR_LIT>' : <EOL> return self . _try_store_in_cookie ( request , response ) <EOL> def _try_store_in_file ( self , request , response ) : <EOL> try : <EOL> if ( not response . session_id or self . _forget <EOL> or self . _unchanged ( response ) ) : <EOL> self . save_session_id_cookie ( ) <EOL> return False <EOL> if response . session_new or not response . session_file : <EOL> session_folder = os . path . dirname ( response . session_filename ) <EOL> if not os . path . exists ( session_folder ) : os . mkdir ( session_folder ) <EOL> response . session_file = open ( response . session_filename , '<STR_LIT:wb>' ) <EOL> portalocker . lock ( response . session_file , portalocker . LOCK_EX ) <EOL> response . session_locked = True <EOL> if response . session_file : <EOL> session_pickled = response . session_pickled or cPickle . dumps ( self ) <EOL> response . session_file . write ( session_pickled ) <EOL> response . session_file . truncate ( ) <EOL> finally : <EOL> self . _close ( response ) <EOL> self . save_session_id_cookie ( ) <EOL> return True <EOL> def _unlock ( self , response ) : <EOL> if response and response . session_file and response . session_locked : <EOL> try : <EOL> portalocker . unlock ( response . session_file ) <EOL> response . session_locked = False <EOL> except : <EOL> pass <EOL> def _close ( self , response ) : <EOL> if response and response . session_file : <EOL> self . _unlock ( response ) <EOL> try : <EOL> response . session_file . close ( ) <EOL> del response . session_file <EOL> except : <EOL> pass </s>
