<s> """<STR_LIT>""" <EOL> import os <EOL> import time <EOL> import datetime <EOL> import random <EOL> import hashlib <EOL> import Cookie <EOL> import pickle <EOL> import sys <EOL> import logging <EOL> from time import strftime <EOL> from google . appengine . ext import db <EOL> from google . appengine . api import memcache <EOL> from django . utils import simplejson <EOL> try : <EOL> import settings_default <EOL> import settings <EOL> if settings . __name__ . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] != settings_default . __name__ . rsplit ( '<STR_LIT:.>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] : <EOL> settings = settings_default <EOL> except : <EOL> settings = settings_default <EOL> class _AppEngineUtilities_Session ( db . Model ) : <EOL> """<STR_LIT>""" <EOL> sid = db . StringListProperty ( ) <EOL> ip = db . StringProperty ( ) <EOL> ua = db . StringProperty ( ) <EOL> last_activity = db . DateTimeProperty ( ) <EOL> dirty = db . BooleanProperty ( default = False ) <EOL> working = db . BooleanProperty ( default = False ) <EOL> deleted = db . BooleanProperty ( default = False ) <EOL> def put ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> memcache . set ( u"<STR_LIT>" % ( str ( self . key ( ) ) ) , self ) <EOL> except : <EOL> db . put ( self ) <EOL> self . last_activity = datetime . datetime . now ( ) <EOL> try : <EOL> self . dirty = False <EOL> db . put ( self ) <EOL> memcache . set ( u"<STR_LIT>" % ( str ( self . key ( ) ) ) , self ) <EOL> except : <EOL> self . dirty = True <EOL> memcache . set ( u"<STR_LIT>" % ( str ( self . key ( ) ) ) , self ) <EOL> return self <EOL> @ classmethod <EOL> def get_session ( cls , session_obj = None ) : <EOL> """<STR_LIT>""" <EOL> if session_obj . sid == None : <EOL> return None <EOL> session_key = session_obj . sid . rsplit ( u'<STR_LIT:_>' , <NUM_LIT:1> ) [ <NUM_LIT:0> ] <EOL> session = memcache . get ( u"<STR_LIT>" % ( str ( session_key ) ) ) <EOL> if session : <EOL> if session . deleted == True : <EOL> session . delete ( ) <EOL> return None <EOL> if session . dirty == True and session . working != False : <EOL> session . working = True <EOL> memcache . set ( u"<STR_LIT>" % ( str ( session_key ) ) , session ) <EOL> session . put ( ) <EOL> if session_obj . sid in session . sid : <EOL> sessionAge = datetime . datetime . now ( ) - session . last_activity <EOL> if sessionAge . seconds > session_obj . session_expire_time : <EOL> session . delete ( ) <EOL> return None <EOL> return session <EOL> else : <EOL> return None <EOL> try : <EOL> ds_session = db . get ( str ( session_key ) ) <EOL> except : <EOL> ds_session = None <EOL> if ds_session : <EOL> sessionAge = datetime . datetime . now ( ) - ds_session . last_activity <EOL> if sessionAge . seconds > session_obj . session_expire_time : <EOL> ds_session . delete ( ) <EOL> return None <EOL> memcache . set ( u"<STR_LIT>" % ( str ( session_key ) ) , ds_session ) <EOL> memcache . set ( u"<STR_LIT>" % ( str ( session_key ) ) , ds_session . get_items_ds ( ) ) <EOL> return ds_session <EOL> def get_items ( self ) : <EOL> """<STR_LIT>""" <EOL> items = memcache . get ( u"<STR_LIT>" % ( str ( self . key ( ) ) ) ) <EOL> if items : <EOL> for item in items : <EOL> if item . deleted == True : <EOL> item . delete ( ) <EOL> items . remove ( item ) <EOL> return items <EOL> query = _AppEngineUtilities_SessionData . all ( ) <EOL> query . filter ( u"<STR_LIT>" , self ) <EOL> results = query . fetch ( <NUM_LIT:1000> ) <EOL> return results <EOL> def get_item ( self , keyname = None ) : <EOL> """<STR_LIT>""" <EOL> mc = memcache . get ( u"<STR_LIT>" % ( str ( self . key ( ) ) ) ) <EOL> if mc : <EOL> for item in mc : <EOL> if item . keyname == keyname : <EOL> if item . deleted == True : <EOL> item . delete ( ) <EOL> return None <EOL> return item <EOL> query = _AppEngineUtilities_SessionData . all ( ) <EOL> query . filter ( u"<STR_LIT>" , self ) <EOL> query . filter ( u"<STR_LIT>" , keyname ) <EOL> results = query . fetch ( <NUM_LIT:1> ) <EOL> if len ( results ) > <NUM_LIT:0> : <EOL> memcache . set ( u"<STR_LIT>" % ( str ( self . key ( ) ) ) , self . get_items_ds ( ) ) <EOL> return results [ <NUM_LIT:0> ] <EOL> return None <EOL> def get_items_ds ( self ) : <EOL> """<STR_LIT>""" <EOL> query = _AppEngineUtilities_SessionData . all ( ) <EOL> query . filter ( u"<STR_LIT>" , self ) <EOL> results = query . fetch ( <NUM_LIT:1000> ) <EOL> return results <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> query = _AppEngineUtilities_SessionData . all ( ) <EOL> query . filter ( u"<STR_LIT>" , self ) <EOL> results = query . fetch ( <NUM_LIT:1000> ) <EOL> db . delete ( results ) <EOL> db . delete ( self ) <EOL> memcache . delete_multi ( [ u"<STR_LIT>" % ( str ( self . key ( ) ) ) , u"<STR_LIT>" % ( str ( self . key ( ) ) ) ] ) <EOL> except : <EOL> mc = memcache . get ( u"<STR_LIT>" % ( str ( self . key ( ) ) ) ) <EOL> if mc : <EOL> mc . deleted = True <EOL> else : <EOL> query = _AppEngineUtilities_Session . all ( ) <EOL> query . filter ( u"<STR_LIT>" , self . sid ) <EOL> results = query . fetch ( <NUM_LIT:1> ) <EOL> if len ( results ) > <NUM_LIT:0> : <EOL> results [ <NUM_LIT:0> ] . deleted = True <EOL> memcache . set ( u"<STR_LIT>" % ( unicode ( self . key ( ) ) ) , results [ <NUM_LIT:0> ] ) <EOL> return True <EOL> class _AppEngineUtilities_SessionData ( db . Model ) : <EOL> """<STR_LIT>""" <EOL> keyname = db . StringProperty ( ) <EOL> content = db . BlobProperty ( ) <EOL> model = db . ReferenceProperty ( ) <EOL> session = db . ReferenceProperty ( _AppEngineUtilities_Session ) <EOL> dirty = db . BooleanProperty ( default = False ) <EOL> deleted = db . BooleanProperty ( default = False ) <EOL> def put ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return_val = db . put ( self ) <EOL> self . dirty = False <EOL> except : <EOL> return_val = u"<STR_LIT>" <EOL> self . dirty = True <EOL> mc_items = memcache . get ( u"<STR_LIT>" % ( str ( self . session . key ( ) ) ) ) <EOL> if mc_items : <EOL> value_updated = False <EOL> for item in mc_items : <EOL> if value_updated == True : <EOL> break <EOL> if item . keyname == self . keyname : <EOL> item . content = self . content <EOL> item . model = self . model <EOL> memcache . set ( u"<STR_LIT>" % ( str ( self . session . key ( ) ) ) , mc_items ) <EOL> value_updated = True <EOL> break <EOL> if value_updated == False : <EOL> mc_items . append ( self ) <EOL> memcache . set ( u"<STR_LIT>" % ( str ( self . session . key ( ) ) ) , mc_items ) <EOL> return return_val <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> db . delete ( self ) <EOL> except : <EOL> self . deleted = True <EOL> mc_items = memcache . get ( u"<STR_LIT>" % ( str ( self . session . key ( ) ) ) ) <EOL> value_handled = False <EOL> for item in mc_items : <EOL> if value_handled == True : <EOL> break <EOL> if item . keyname == self . keyname : <EOL> if self . deleted == True : <EOL> item . deleted = True <EOL> else : <EOL> mc_items . remove ( item ) <EOL> memcache . set ( u"<STR_LIT>" % ( str ( self . session . key ( ) ) ) , mc_items ) <EOL> return True <EOL> class _DatastoreWriter ( object ) : <EOL> def put ( self , keyname , value , session ) : <EOL> """<STR_LIT>""" <EOL> keyname = session . _validate_key ( keyname ) <EOL> if value is None : <EOL> raise ValueError ( u"<STR_LIT>" ) <EOL> if session . cookie_vals . has_key ( keyname ) : <EOL> del ( session . cookie_vals [ keyname ] ) <EOL> session . output_cookie [ "<STR_LIT>" % ( session . cookie_name ) ] = simplejson . dumps ( session . cookie_vals ) <EOL> session . output_cookie [ "<STR_LIT>" % ( session . cookie_name ) ] [ "<STR_LIT:path>" ] = session . cookie_path <EOL> if session . cookie_domain : <EOL> session . output_cookie [ "<STR_LIT>" % ( session . cookie_name ) ] [ "<STR_LIT>" ] = session . cookie_domain <EOL> print session . output_cookie . output ( ) <EOL> sessdata = session . _get ( keyname = keyname ) <EOL> if sessdata is None : <EOL> sessdata = _AppEngineUtilities_SessionData ( ) <EOL> sessdata . keyname = keyname <EOL> try : <EOL> db . model_to_protobuf ( value ) <EOL> if not value . is_saved ( ) : <EOL> value . put ( ) <EOL> sessdata . model = value <EOL> except : <EOL> sessdata . content = pickle . dumps ( value ) <EOL> sessdata . model = None <EOL> sessdata . session = session . session <EOL> session . cache [ keyname ] = value <EOL> return sessdata . put ( ) <EOL> class _CookieWriter ( object ) : <EOL> def put ( self , keyname , value , session ) : <EOL> """<STR_LIT>""" <EOL> keyname = session . _validate_key ( keyname ) <EOL> if value is None : <EOL> raise ValueError ( u"<STR_LIT>" ) <EOL> session . cookie_vals [ keyname ] = value <EOL> session . cache [ keyname ] = value <EOL> session . output_cookie [ "<STR_LIT>" % ( session . cookie_name ) ] = simplejson . dumps ( session . cookie_vals ) <EOL> session . output_cookie [ "<STR_LIT>" % ( session . cookie_name ) ] [ "<STR_LIT:path>" ] = session . cookie_path <EOL> if session . cookie_domain : <EOL> session . output_cookie [ "<STR_LIT>" % ( session . cookie_name ) ] [ "<STR_LIT>" ] = session . cookie_domain <EOL> print session . output_cookie . output ( ) <EOL> return True <EOL> class Session ( object ) : <EOL> """<STR_LIT>""" <EOL> COOKIE_NAME = settings . session [ "<STR_LIT>" ] <EOL> def __init__ ( self , cookie_path = settings . session [ "<STR_LIT>" ] , <EOL> cookie_domain = settings . session [ "<STR_LIT>" ] , <EOL> cookie_name = settings . session [ "<STR_LIT>" ] , <EOL> session_expire_time = settings . session [ "<STR_LIT>" ] , <EOL> clean_check_percent = settings . session [ "<STR_LIT>" ] , <EOL> integrate_flash = settings . session [ "<STR_LIT>" ] , <EOL> check_ip = settings . session [ "<STR_LIT>" ] , <EOL> check_user_agent = settings . session [ "<STR_LIT>" ] , <EOL> set_cookie_expires = settings . session [ "<STR_LIT>" ] , <EOL> session_token_ttl = settings . session [ "<STR_LIT>" ] , <EOL> last_activity_update = settings . session [ "<STR_LIT>" ] , <EOL> writer = settings . session [ "<STR_LIT>" ] ) : <EOL> """<STR_LIT>""" <EOL> self . cookie_path = cookie_path <EOL> self . cookie_domain = cookie_domain <EOL> self . cookie_name = cookie_name <EOL> self . session_expire_time = session_expire_time <EOL> self . integrate_flash = integrate_flash <EOL> self . check_user_agent = check_user_agent <EOL> self . check_ip = check_ip <EOL> self . set_cookie_expires = set_cookie_expires <EOL> self . session_token_ttl = session_token_ttl <EOL> self . last_activity_update = last_activity_update <EOL> self . writer = writer <EOL> print self . no_cache_headers ( ) <EOL> self . cache = { } <EOL> string_cookie = os . environ . get ( u"<STR_LIT>" , u"<STR_LIT>" ) <EOL> self . cookie = Cookie . SimpleCookie ( ) <EOL> self . output_cookie = Cookie . SimpleCookie ( ) <EOL> if string_cookie == "<STR_LIT>" : <EOL> self . cookie_vals = { } <EOL> else : <EOL> self . cookie . load ( string_cookie ) <EOL> try : <EOL> self . cookie_vals = simplejson . loads ( self . cookie [ "<STR_LIT>" % ( self . cookie_name ) ] . value ) <EOL> for k in self . cookie_vals : <EOL> self . cache [ k ] = self . cookie_vals [ k ] <EOL> self . output_cookie [ "<STR_LIT>" % ( self . cookie_name ) ] = simplejson . dumps ( self . cookie_vals ) <EOL> except Exception , e : <EOL> self . cookie_vals = { } <EOL> if writer == "<STR_LIT>" : <EOL> pass <EOL> else : <EOL> self . sid = None <EOL> new_session = True <EOL> do_put = False <EOL> if self . cookie . get ( cookie_name ) : <EOL> self . sid = self . cookie [ cookie_name ] . value <EOL> self . session = _AppEngineUtilities_Session . get_session ( self ) <EOL> if self . session : <EOL> new_session = False <EOL> if new_session : <EOL> self . session = _AppEngineUtilities_Session ( ) <EOL> self . session . put ( ) <EOL> self . sid = self . new_sid ( ) <EOL> if u"<STR_LIT>" in os . environ : <EOL> self . session . ua = os . environ [ u"<STR_LIT>" ] <EOL> else : <EOL> self . session . ua = None <EOL> if u"<STR_LIT>" in os . environ : <EOL> self . session . ip = os . environ [ "<STR_LIT>" ] <EOL> else : <EOL> self . session . ip = None <EOL> self . session . sid = [ self . sid ] <EOL> self . session . put ( ) <EOL> else : <EOL> duration = datetime . timedelta ( seconds = self . session_token_ttl ) <EOL> session_age_limit = datetime . datetime . now ( ) - duration <EOL> if self . session . last_activity < session_age_limit : <EOL> self . sid = self . new_sid ( ) <EOL> if len ( self . session . sid ) > <NUM_LIT:2> : <EOL> self . session . sid . remove ( self . session . sid [ <NUM_LIT:0> ] ) <EOL> self . session . sid . append ( self . sid ) <EOL> do_put = True <EOL> else : <EOL> self . sid = self . session . sid [ - <NUM_LIT:1> ] <EOL> ula = datetime . timedelta ( seconds = self . last_activity_update ) <EOL> if datetime . datetime . now ( ) > self . session . last_activity + ula : <EOL> do_put = True <EOL> self . output_cookie [ cookie_name ] = self . sid <EOL> self . output_cookie [ cookie_name ] [ "<STR_LIT:path>" ] = self . cookie_path <EOL> if self . cookie_domain : <EOL> self . output_cookie [ cookie_name ] [ "<STR_LIT>" ] = self . cookie_domain <EOL> if self . set_cookie_expires : <EOL> self . output_cookie [ cookie_name ] [ "<STR_LIT>" ] = self . session_expire_time <EOL> self . cache [ u"<STR_LIT>" ] = self . sid <EOL> if do_put : <EOL> if self . sid != None or self . sid != u"<STR_LIT>" : <EOL> self . session . put ( ) <EOL> if self . output_cookie . has_key ( "<STR_LIT>" % ( cookie_name ) ) : <EOL> self . output_cookie [ "<STR_LIT>" % ( cookie_name ) ] [ "<STR_LIT:path>" ] = cookie_path <EOL> if self . set_cookie_expires : <EOL> self . output_cookie [ "<STR_LIT>" % ( cookie_name ) ] [ "<STR_LIT>" ] = self . session_expire_time <EOL> print self . output_cookie . output ( ) <EOL> if self . integrate_flash : <EOL> import flash <EOL> self . flash = flash . Flash ( cookie = self . cookie ) <EOL> if random . randint ( <NUM_LIT:1> , <NUM_LIT:100> ) < clean_check_percent : <EOL> self . _clean_old_sessions ( ) <EOL> def new_sid ( self ) : <EOL> """<STR_LIT>""" <EOL> sid = u"<STR_LIT>" % ( str ( self . session . key ( ) ) , <EOL> hashlib . md5 ( repr ( time . time ( ) ) + unicode ( random . random ( ) ) ) . hexdigest ( ) <EOL> ) <EOL> return sid <EOL> def _get ( self , keyname = None ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , '<STR_LIT>' ) : <EOL> if keyname != None : <EOL> return self . session . get_item ( keyname ) <EOL> return self . session . get_items ( ) <EOL> return None <EOL> def _validate_key ( self , keyname ) : <EOL> """<STR_LIT>""" <EOL> if keyname is None : <EOL> raise ValueError ( <EOL> u"<STR_LIT>" <EOL> ) <EOL> elif keyname in ( u"<STR_LIT>" , u"<STR_LIT>" ) : <EOL> raise ValueError ( u"<STR_LIT>" % keyname ) <EOL> if type ( keyname ) != type ( [ str , unicode ] ) : <EOL> return unicode ( keyname ) <EOL> return keyname <EOL> def _put ( self , keyname , value ) : <EOL> """<STR_LIT>""" <EOL> if self . writer == "<STR_LIT>" : <EOL> writer = _DatastoreWriter ( ) <EOL> else : <EOL> writer = _CookieWriter ( ) <EOL> return writer . put ( keyname , value , self ) <EOL> def _delete_session ( self ) : <EOL> """<STR_LIT>""" <EOL> if u"<STR_LIT>" in sys . modules [ '<STR_LIT:__main__>' ] . __dict__ : <EOL> sys . modules [ '<STR_LIT:__main__>' ] . AEU_Events . fire_event ( u"<STR_LIT>" ) <EOL> if hasattr ( self , u"<STR_LIT>" ) : <EOL> self . session . delete ( ) <EOL> self . cookie_vals = { } <EOL> self . cache = { } <EOL> self . output_cookie [ "<STR_LIT>" % ( self . cookie_name ) ] = simplejson . dumps ( self . cookie_vals ) <EOL> self . output_cookie [ "<STR_LIT>" % ( self . cookie_name ) ] [ "<STR_LIT:path>" ] = self . cookie_path <EOL> if self . cookie_domain : <EOL> self . output_cookie [ "<STR_LIT>" % ( self . cookie_name ) ] [ "<STR_LIT>" ] = self . cookie_domain <EOL> self . output_cookie [ self . cookie_name ] [ "<STR_LIT>" ] = <NUM_LIT:0> <EOL> self . output_cookie [ "<STR_LIT>" % ( self . cookie_name ) ] [ "<STR_LIT>" ] = <NUM_LIT:0> <EOL> print self . output_cookie . output ( ) <EOL> if u"<STR_LIT>" in sys . modules [ '<STR_LIT:__main__>' ] . __dict__ : <EOL> sys . modules [ '<STR_LIT:__main__>' ] . AEU_Events . fire_event ( u"<STR_LIT>" ) <EOL> return True <EOL> def delete ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _delete_session ( ) <EOL> @ classmethod <EOL> def delete_all_sessions ( cls ) : <EOL> """<STR_LIT>""" <EOL> all_sessions_deleted = False <EOL> while not all_sessions_deleted : <EOL> query = _AppEngineUtilities_Session . all ( ) <EOL> results = query . fetch ( <NUM_LIT> ) <EOL> if len ( results ) is <NUM_LIT:0> : <EOL> all_sessions_deleted = True <EOL> else : <EOL> for result in results : <EOL> result . delete ( ) <EOL> return True <EOL> def _clean_old_sessions ( self ) : <EOL> """<STR_LIT>""" <EOL> self . clean_old_sessions ( self . session_expire_time , <NUM_LIT:50> ) <EOL> @ classmethod <EOL> def clean_old_sessions ( cls , session_expire_time , count = <NUM_LIT:50> ) : <EOL> """<STR_LIT>""" <EOL> duration = datetime . timedelta ( seconds = session_expire_time ) <EOL> session_age = datetime . datetime . now ( ) - duration <EOL> query = _AppEngineUtilities_Session . all ( ) <EOL> query . filter ( u"<STR_LIT>" , session_age ) <EOL> results = query . fetch ( <NUM_LIT:50> ) <EOL> for result in results : <EOL> result . delete ( ) <EOL> return True <EOL> def cycle_key ( self ) : <EOL> """<STR_LIT>""" <EOL> self . sid = self . new_sid ( ) <EOL> if len ( self . session . sid ) > <NUM_LIT:2> : <EOL> self . session . sid . remove ( self . session . sid [ <NUM_LIT:0> ] ) <EOL> self . session . sid . append ( self . sid ) <EOL> return self . sid <EOL> def flush ( self ) : <EOL> """<STR_LIT>""" <EOL> self . _delete_session ( ) <EOL> self . __init__ ( ) <EOL> return True <EOL> def no_cache_headers ( self ) : <EOL> """<STR_LIT>""" <EOL> return u"<STR_LIT>" . join ( [ u"<STR_LIT>" , <EOL> strftime ( "<STR_LIT>" ) . decode ( "<STR_LIT:utf-8>" ) , <EOL> u"<STR_LIT>" , <EOL> u"<STR_LIT>" , <EOL> u"<STR_LIT>" , <EOL> ] ) <EOL> def clear ( self ) : <EOL> """<STR_LIT>""" <EOL> sessiondata = self . _get ( ) <EOL> if sessiondata is not None : <EOL> for sd in sessiondata : <EOL> sd . delete ( ) <EOL> self . cache = { } <EOL> self . cookie_vals = { } <EOL> self . output_cookie [ "<STR_LIT>" % ( self . cookie_name ) ] = simplejson . dumps ( self . cookie_vals ) <EOL> self . output_cookie [ "<STR_LIT>" % ( self . cookie_name ) ] [ "<STR_LIT:path>" ] = self . cookie_path <EOL> if self . cookie_domain : <EOL> self . output_cookie [ "<STR_LIT>" % ( self . cookie_name ) ] [ "<STR_LIT>" ] = self . cookie_domain <EOL> self . output_cookie [ "<STR_LIT>" % ( self . cookie_name ) ] [ "<STR_LIT>" ] = <NUM_LIT:0> <EOL> print self . output_cookie . output ( ) <EOL> return True <EOL> def has_key ( self , keyname ) : <EOL> """<STR_LIT>""" <EOL> return self . __contains__ ( keyname ) <EOL> def items ( self ) : <EOL> """<STR_LIT>""" <EOL> op = { } <EOL> for k in self : <EOL> op [ k ] = self [ k ] <EOL> return op <EOL> def keys ( self ) : <EOL> """<STR_LIT>""" <EOL> l = [ ] <EOL> for k in self : <EOL> l . append ( k ) <EOL> return l <EOL> def update ( self , * dicts ) : <EOL> """<STR_LIT>""" <EOL> for dict in dicts : <EOL> for k in dict : <EOL> self . _put ( k , dict [ k ] ) <EOL> return None <EOL> def values ( self ) : <EOL> """<STR_LIT>""" <EOL> v = [ ] <EOL> for k in self : <EOL> v . append ( self [ k ] ) <EOL> return v <EOL> def get ( self , keyname , default = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . __getitem__ ( keyname ) <EOL> except KeyError : <EOL> if default is not None : <EOL> return default <EOL> return None <EOL> def setdefault ( self , keyname , default = None ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> return self . __getitem__ ( keyname ) <EOL> except KeyError : <EOL> if default is not None : <EOL> self . __setitem__ ( keyname , default ) <EOL> return default <EOL> return None <EOL> @ classmethod <EOL> def check_token ( cls , cookie_name = COOKIE_NAME , delete_invalid = True ) : <EOL> """<STR_LIT>""" <EOL> string_cookie = os . environ . get ( u"<STR_LIT>" , u"<STR_LIT>" ) <EOL> cookie = Cookie . SimpleCookie ( ) <EOL> cookie . load ( string_cookie ) <EOL> if cookie . has_key ( cookie_name ) : <EOL> query = _AppEngineUtilities_Session . all ( ) <EOL> query . filter ( u"<STR_LIT>" , cookie [ cookie_name ] . value ) <EOL> results = query . fetch ( <NUM_LIT:1> ) <EOL> if len ( results ) > <NUM_LIT:0> : <EOL> return True <EOL> else : <EOL> if delete_invalid : <EOL> output_cookie = Cookie . SimpleCookie ( ) <EOL> output_cookie [ cookie_name ] = cookie [ cookie_name ] <EOL> output_cookie [ cookie_name ] [ u"<STR_LIT>" ] = <NUM_LIT:0> <EOL> print output_cookie . output ( ) <EOL> return False <EOL> def get_ds_entity ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , u"<STR_LIT>" ) : <EOL> return self . session <EOL> return None <EOL> def __getitem__ ( self , keyname ) : <EOL> """<STR_LIT>""" <EOL> if self . integrate_flash and ( keyname == u"<STR_LIT>" ) : <EOL> return self . flash . msg <EOL> if keyname in self . cache : <EOL> return self . cache [ keyname ] <EOL> if keyname in self . cookie_vals : <EOL> return self . cookie_vals [ keyname ] <EOL> if hasattr ( self , u"<STR_LIT>" ) : <EOL> data = self . _get ( keyname ) <EOL> if data : <EOL> try : <EOL> if data . model != None : <EOL> self . cache [ keyname ] = data . model <EOL> return self . cache [ keyname ] <EOL> else : <EOL> self . cache [ keyname ] = pickle . loads ( data . content ) <EOL> return self . cache [ keyname ] <EOL> except : <EOL> self . delete_item ( keyname ) <EOL> else : <EOL> raise KeyError ( unicode ( keyname ) ) <EOL> raise KeyError ( unicode ( keyname ) ) <EOL> def __setitem__ ( self , keyname , value ) : <EOL> """<STR_LIT>""" <EOL> if self . integrate_flash and ( keyname == u"<STR_LIT>" ) : <EOL> self . flash . msg = value <EOL> else : <EOL> keyname = self . _validate_key ( keyname ) <EOL> self . cache [ keyname ] = value <EOL> return self . _put ( keyname , value ) <EOL> def delete_item ( self , keyname , throw_exception = False ) : <EOL> """<STR_LIT>""" <EOL> if throw_exception : <EOL> self . __delitem__ ( keyname ) <EOL> return None <EOL> else : <EOL> try : <EOL> self . __delitem__ ( keyname ) <EOL> except KeyError : <EOL> return None <EOL> def __delitem__ ( self , keyname ) : <EOL> """<STR_LIT>""" <EOL> bad_key = False <EOL> sessdata = self . _get ( keyname = keyname ) <EOL> if sessdata is None : <EOL> bad_key = True <EOL> else : <EOL> sessdata . delete ( ) <EOL> if keyname in self . cookie_vals : <EOL> del self . cookie_vals [ keyname ] <EOL> bad_key = False <EOL> self . output_cookie [ "<STR_LIT>" % ( self . cookie_name ) ] = simplejson . dumps ( self . cookie_vals ) <EOL> self . output_cookie [ "<STR_LIT>" % ( self . cookie_name ) ] [ "<STR_LIT:path>" ] = self . cookie_path <EOL> if self . cookie_domain : <EOL> self . output_cookie [ "<STR_LIT>" % ( self . cookie_name ) ] [ "<STR_LIT>" ] = self . cookie_domain <EOL> print self . output_cookie . output ( ) <EOL> if bad_key : <EOL> raise KeyError ( unicode ( keyname ) ) <EOL> if keyname in self . cache : <EOL> del self . cache [ keyname ] <EOL> def __len__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , u"<STR_LIT>" ) : <EOL> results = self . _get ( ) <EOL> if results is not None : <EOL> return len ( results ) + len ( self . cookie_vals ) <EOL> else : <EOL> return <NUM_LIT:0> <EOL> return len ( self . cookie_vals ) <EOL> def __contains__ ( self , keyname ) : <EOL> """<STR_LIT>""" <EOL> try : <EOL> self . __getitem__ ( keyname ) <EOL> except KeyError : <EOL> return False <EOL> return True <EOL> def __iter__ ( self ) : <EOL> """<STR_LIT>""" <EOL> if hasattr ( self , u"<STR_LIT>" ) : <EOL> vals = self . _get ( ) <EOL> if vals is not None : <EOL> for k in vals : <EOL> yield k . keyname <EOL> for k in self . cookie_vals : <EOL> yield k <EOL> def __str__ ( self ) : <EOL> """<STR_LIT>""" <EOL> return u"<STR_LIT>" % '<STR_LIT:U+002CU+0020>' . join ( [ '<STR_LIT>' % ( k , self [ k ] ) for k in self ] ) </s>
