<s> from contextlib import contextmanager <EOL> from django . contrib . sites . models import Site <EOL> from django . core import mail <EOL> from django . db import transaction <EOL> from django . utils . functional import LazyObject <EOL> def get_current_site ( ) : <EOL> if Site . _meta . installed : <EOL> return Site . objects . get_current ( ) <EOL> return None <EOL> def in_transaction ( test_ignore = True ) : <EOL> result = transaction . is_managed ( ) <EOL> if test_ignore : <EOL> result = result and not hasattr ( mail , '<STR_LIT>' ) <EOL> return result <EOL> @ contextmanager <EOL> def patch ( namespace , name , function ) : <EOL> """<STR_LIT>""" <EOL> if isinstance ( namespace , LazyObject ) : <EOL> if namespace . _wrapped is None : <EOL> namespace . _setup ( ) <EOL> namespace = namespace . _wrapped <EOL> try : <EOL> original = getattr ( namespace , name ) <EOL> except AttributeError : <EOL> original = NotImplemented <EOL> try : <EOL> setattr ( namespace , name , function ) <EOL> yield <EOL> finally : <EOL> if original is NotImplemented : <EOL> delattr ( namespace , name ) <EOL> else : <EOL> setattr ( namespace , name , original ) </s>
