<s> import logging <EOL> from django . contrib . sessions . backends . base import SessionBase , CreateError <EOL> from django . core . exceptions import SuspiciousOperation <EOL> from django . db import IntegrityError , transaction , router <EOL> from django . utils import timezone <EOL> from django . utils . encoding import force_text <EOL> class SessionStore ( SessionBase ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , session_key = None ) : <EOL> super ( SessionStore , self ) . __init__ ( session_key ) <EOL> def load ( self ) : <EOL> try : <EOL> s = Session . objects . get ( <EOL> session_key = self . session_key , <EOL> expire_date__gt = timezone . now ( ) <EOL> ) <EOL> return self . decode ( s . session_data ) <EOL> except ( Session . DoesNotExist , SuspiciousOperation ) as e : <EOL> if isinstance ( e , SuspiciousOperation ) : <EOL> logger = logging . getLogger ( '<STR_LIT>' % <EOL> e . __class__ . __name__ ) <EOL> logger . warning ( force_text ( e ) ) <EOL> self . create ( ) <EOL> return { } <EOL> def exists ( self , session_key ) : <EOL> return Session . objects . filter ( session_key = session_key ) . exists ( ) <EOL> def create ( self ) : <EOL> while True : <EOL> self . _session_key = self . _get_new_session_key ( ) <EOL> try : <EOL> self . save ( must_create = True ) <EOL> except CreateError : <EOL> continue <EOL> self . modified = True <EOL> self . _session_cache = { } <EOL> return <EOL> def save ( self , must_create = False ) : <EOL> """<STR_LIT>""" <EOL> obj = Session ( <EOL> session_key = self . _get_or_create_session_key ( ) , <EOL> session_data = self . encode ( self . _get_session ( no_load = must_create ) ) , <EOL> expire_date = self . get_expiry_date ( ) <EOL> ) <EOL> using = router . db_for_write ( Session , instance = obj ) <EOL> try : <EOL> with transaction . atomic ( using = using ) : <EOL> obj . save ( force_insert = must_create , using = using ) <EOL> except IntegrityError : <EOL> if must_create : <EOL> raise CreateError <EOL> raise <EOL> def delete ( self , session_key = None ) : <EOL> if session_key is None : <EOL> if self . session_key is None : <EOL> return <EOL> session_key = self . session_key <EOL> try : <EOL> Session . objects . get ( session_key = session_key ) . delete ( ) <EOL> except Session . DoesNotExist : <EOL> pass <EOL> @ classmethod <EOL> def clear_expired ( cls ) : <EOL> Session . objects . filter ( expire_date__lt = timezone . now ( ) ) . delete ( ) <EOL> from django . contrib . sessions . models import Session </s>
