<s> from django . db import ( <EOL> DEFAULT_DB_ALIAS , DatabaseError , Error , ProgrammingError , connections , <EOL> ) <EOL> from django . utils . decorators import ContextDecorator <EOL> class TransactionManagementError ( ProgrammingError ) : <EOL> """<STR_LIT>""" <EOL> pass <EOL> def get_connection ( using = None ) : <EOL> """<STR_LIT>""" <EOL> if using is None : <EOL> using = DEFAULT_DB_ALIAS <EOL> return connections [ using ] <EOL> def get_autocommit ( using = None ) : <EOL> """<STR_LIT>""" <EOL> return get_connection ( using ) . get_autocommit ( ) <EOL> def set_autocommit ( autocommit , using = None ) : <EOL> """<STR_LIT>""" <EOL> return get_connection ( using ) . set_autocommit ( autocommit ) <EOL> def commit ( using = None ) : <EOL> """<STR_LIT>""" <EOL> get_connection ( using ) . commit ( ) <EOL> def rollback ( using = None ) : <EOL> """<STR_LIT>""" <EOL> get_connection ( using ) . rollback ( ) <EOL> def savepoint ( using = None ) : <EOL> """<STR_LIT>""" <EOL> return get_connection ( using ) . savepoint ( ) <EOL> def savepoint_rollback ( sid , using = None ) : <EOL> """<STR_LIT>""" <EOL> get_connection ( using ) . savepoint_rollback ( sid ) <EOL> def savepoint_commit ( sid , using = None ) : <EOL> """<STR_LIT>""" <EOL> get_connection ( using ) . savepoint_commit ( sid ) <EOL> def clean_savepoints ( using = None ) : <EOL> """<STR_LIT>""" <EOL> get_connection ( using ) . clean_savepoints ( ) <EOL> def get_rollback ( using = None ) : <EOL> """<STR_LIT>""" <EOL> return get_connection ( using ) . get_rollback ( ) <EOL> def set_rollback ( rollback , using = None ) : <EOL> """<STR_LIT>""" <EOL> return get_connection ( using ) . set_rollback ( rollback ) <EOL> def on_commit ( func , using = None ) : <EOL> """<STR_LIT>""" <EOL> get_connection ( using ) . on_commit ( func ) <EOL> class Atomic ( ContextDecorator ) : <EOL> """<STR_LIT>""" <EOL> def __init__ ( self , using , savepoint ) : <EOL> self . using = using <EOL> self . savepoint = savepoint <EOL> def __enter__ ( self ) : <EOL> connection = get_connection ( self . using ) <EOL> if not connection . in_atomic_block : <EOL> connection . commit_on_exit = True <EOL> connection . needs_rollback = False <EOL> if not connection . get_autocommit ( ) : <EOL> if connection . features . autocommits_when_autocommit_is_off : <EOL> raise TransactionManagementError ( <EOL> "<STR_LIT>" <EOL> "<STR_LIT>" ) <EOL> connection . in_atomic_block = True <EOL> connection . commit_on_exit = False <EOL> if connection . in_atomic_block : <EOL> if self . savepoint and not connection . needs_rollback : <EOL> sid = connection . savepoint ( ) <EOL> connection . savepoint_ids . append ( sid ) <EOL> else : <EOL> connection . savepoint_ids . append ( None ) <EOL> else : <EOL> connection . set_autocommit ( False , force_begin_transaction_with_broken_autocommit = True ) <EOL> connection . in_atomic_block = True <EOL> def __exit__ ( self , exc_type , exc_value , traceback ) : <EOL> connection = get_connection ( self . using ) <EOL> if connection . savepoint_ids : <EOL> sid = connection . savepoint_ids . pop ( ) <EOL> else : <EOL> connection . in_atomic_block = False <EOL> try : <EOL> if connection . closed_in_transaction : <EOL> pass <EOL> elif exc_type is None and not connection . needs_rollback : <EOL> if connection . in_atomic_block : <EOL> if sid is not None : <EOL> try : <EOL> connection . savepoint_commit ( sid ) <EOL> except DatabaseError : <EOL> try : <EOL> connection . savepoint_rollback ( sid ) <EOL> connection . savepoint_commit ( sid ) <EOL> except Error : <EOL> connection . needs_rollback = True <EOL> raise <EOL> else : <EOL> try : <EOL> connection . commit ( ) <EOL> except DatabaseError : <EOL> try : <EOL> connection . rollback ( ) <EOL> except Error : <EOL> connection . close ( ) <EOL> raise <EOL> else : <EOL> connection . needs_rollback = False <EOL> if connection . in_atomic_block : <EOL> if sid is None : <EOL> connection . needs_rollback = True <EOL> else : <EOL> try : <EOL> connection . savepoint_rollback ( sid ) <EOL> connection . savepoint_commit ( sid ) <EOL> except Error : <EOL> connection . needs_rollback = True <EOL> else : <EOL> try : <EOL> connection . rollback ( ) <EOL> except Error : <EOL> connection . close ( ) <EOL> finally : <EOL> if not connection . in_atomic_block : <EOL> if connection . closed_in_transaction : <EOL> connection . connection = None <EOL> else : <EOL> connection . set_autocommit ( True ) <EOL> elif not connection . savepoint_ids and not connection . commit_on_exit : <EOL> if connection . closed_in_transaction : <EOL> connection . connection = None <EOL> else : <EOL> connection . in_atomic_block = False <EOL> def atomic ( using = None , savepoint = True ) : <EOL> if callable ( using ) : <EOL> return Atomic ( DEFAULT_DB_ALIAS , savepoint ) ( using ) <EOL> else : <EOL> return Atomic ( using , savepoint ) <EOL> def _non_atomic_requests ( view , using ) : <EOL> try : <EOL> view . _non_atomic_requests . add ( using ) <EOL> except AttributeError : <EOL> view . _non_atomic_requests = { using } <EOL> return view <EOL> def non_atomic_requests ( using = None ) : <EOL> if callable ( using ) : <EOL> return _non_atomic_requests ( using , DEFAULT_DB_ALIAS ) <EOL> else : <EOL> if using is None : <EOL> using = DEFAULT_DB_ALIAS <EOL> return lambda view : _non_atomic_requests ( view , using ) </s>
