def patch(): global _has_patched if _has_patched: return # Import for side-effect: configures logging handlers. # pylint: disable-msg=W0611 import log_settings # Monkey-patch django forms to avoid having to use Jinja2's |safe # everywhere. import safe_django_forms safe_django_forms.monkeypatch() # Monkey-patch Django's csrf_protect decorator to use session-based CSRF # tokens: if 'session_csrf' in settings.INSTALLED_APPS: import session_csrf session_csrf.monkeypatch() from . import admin admin.monkeypatch() if 'compressor' in settings.INSTALLED_APPS: import jingo from compressor.contrib.jinja2ext import CompressorExtension jingo.env.add_extension(CompressorExtension) logging.debug("Note: funfactory monkey patches executed in %s" % __file__) # prevent it from being run again later _has_patched = True
def patch(): # Import for side-effect: configures logging handlers. # pylint: disable-msg=W0611 import log_settings # Monkey-patch django forms to avoid having to use Jinja2's |safe # everywhere. import safe_django_forms safe_django_forms.monkeypatch() # Monkey-patch Django's csrf_protect decorator to use session-based CSRF # tokens: if 'session_csrf' in settings.INSTALLED_APPS: import session_csrf session_csrf.monkeypatch() logging.debug("Note: funfactory monkey patches executed in %s" % __file__)
def patch(): global _has_patched if _has_patched: return # Import for side-effect: configures logging handlers. # pylint: disable-msg=W0611 from . import log_settings # noqa # Monkey-patch django forms to avoid having to use Jinja2's |safe # everywhere. try: import jingo.monkey jingo.monkey.patch() except ImportError: # If we can't import jingo.monkey, then it's an older jingo, # so we go back to the old ways. import safe_django_forms safe_django_forms.monkeypatch() logging.debug("Note: bedrock monkey patches executed in %s" % __file__) # prevent it from being run again later _has_patched = True
from django.core.management import execute_manager, setup_environ try: import settings_local as settings except ImportError: try: import settings except ImportError: import sys sys.stderr.write( "Error: Tried importing 'settings_local.py' and 'settings.py' " "but neither could be found (or they're throwing an ImportError)." " Please come back and try again later.") raise # If we want to use django settings anywhere, we need to set up the required # environment variables. setup_environ(settings) # Configure Celery import djcelery djcelery.setup_loader() # Monkey-patch django forms to avoid having to use Jinja2's |safe everywhere. import safe_django_forms safe_django_forms.monkeypatch() if __name__ == "__main__": execute_manager(settings)
# Finally load the settings file that was specified. res = imp.find_module(setting) settings = imp.load_module(setting, *res) os.environ['DJANGO_SETTINGS_MODULE'] = setting if not settings.DEBUG: warnings.simplefilter('ignore') # The first thing execute_manager does is call `setup_environ`. Logging config # needs to access settings, so we'll setup the environ early. setup_environ(settings) # Hardcore monkeypatching action. import safe_django_forms safe_django_forms.monkeypatch() import session_csrf session_csrf.monkeypatch() # Fix jinja's Markup class to not crash when localizers give us bad format # strings. from jinja2 import Markup mod = Markup.__mod__ trans_log = logging.getLogger('z.trans') def new(self, arg): try: return mod(self, arg) except Exception:
def setup_environ(manage_file, settings=None): """Sets up a Django app within a manage.py file. Keyword Arguments **settings** An imported settings module. Without this, playdoh tries to import these modules (in order): settings_local, settings """ # sys is global to avoid undefined local global sys, current_settings, execute_manager, ROOT ROOT = os.path.dirname(os.path.abspath(manage_file)) # Adjust the python path and put local packages in front. prev_sys_path = list(sys.path) # Make settings_local importable sys.path.append(ROOT) # Give precedence to your app's parent dir, which contains __init__.py sys.path.append(os.path.abspath(os.path.join(ROOT, os.pardir))) site.addsitedir(path('apps')) site.addsitedir(path('lib')) # Local (project) vendor library site.addsitedir(path('vendor-local')) site.addsitedir(path('vendor-local/lib/python')) # Global (upstream) vendor library site.addsitedir(path('vendor')) site.addsitedir(path('vendor/lib/python')) # Move the new items to the front of sys.path. (via virtualenv) new_sys_path = [] for item in list(sys.path): if item not in prev_sys_path: new_sys_path.append(item) sys.path.remove(item) sys.path[:0] = new_sys_path from django.core.management import execute_manager, setup_environ if not settings: if os.path.isfile(os.path.join(ROOT, 'settings_local.py')): import settings_local as settings warnings.warn("Using settings_local.py is deprecated. See " "http://playdoh.readthedocs.org/en/latest/upgrading.html", DeprecationWarning) else: import settings current_settings = settings validate_settings(settings) # If we want to use django settings anywhere, we need to set up the # required environment variables. setup_environ(settings) # Import for side-effect: configures logging handlers. # pylint: disable-msg=W0611 import log_settings # Monkey-patch django forms to avoid having to use Jinja2's |safe # everywhere. import safe_django_forms safe_django_forms.monkeypatch() # Monkey-patch Django's csrf_protect decorator to use session-based CSRF # tokens: if 'session_csrf' in settings.INSTALLED_APPS: import session_csrf session_csrf.monkeypatch() # Configure Celery (optional) if 'djcelery' in settings.INSTALLED_APPS: import djcelery djcelery.setup_loader()