From 92bfddb834e109456896b1f10ef288cb53a38c1d Mon Sep 17 00:00:00 2001 From: Chris Church Date: Sun, 28 Jul 2013 16:30:19 -0400 Subject: [PATCH] Fix for django-jsonfield to work on Django 1.4.1 (default with Ubuntu 12.10, fix for AC-222), update settings to provide more information when unable to load local/global settings file. --- awx/__init__.py | 16 +++++++++++++++- awx/settings/development.py | 7 +++++-- awx/settings/production.py | 13 ++++++++----- awx/wsgi.py | 22 +++------------------- 4 files changed, 31 insertions(+), 27 deletions(-) diff --git a/awx/__init__.py b/awx/__init__.py index 339260e6c5..b8efd05c6a 100644 --- a/awx/__init__.py +++ b/awx/__init__.py @@ -33,7 +33,7 @@ def find_commands(management_dir): pass return commands -def manage(): +def prepare_env(): # Update the default settings environment variable based on current mode. os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'awx.settings.%s' % MODE) # Add local site-packages directory to path. @@ -48,6 +48,20 @@ def manage(): # Monkeypatch Django find_commands to also work with .pyc files. import django.core.management django.core.management.find_commands = find_commands + # Fixup sys.modules reference to django.utils.six to allow jsonfield to + # work when using Django 1.4. + import django.utils + try: + import django.utils.six + except ImportError: + import six + sys.modules['django.utils.six'] = sys.modules['six'] + django.utils.six = sys.modules['django.utils.six'] + from django.utils import six + +def manage(): + # Prepare the AWX environment. + prepare_env() # Now run the command (or display the version). from django.core.management import execute_from_command_line if len(sys.argv) >= 2 and sys.argv[1] in ('version', '--version'): diff --git a/awx/settings/development.py b/awx/settings/development.py index 8af7e064fc..820e8ce0e3 100644 --- a/awx/settings/development.py +++ b/awx/settings/development.py @@ -16,5 +16,8 @@ try: class dummymodule(str): __file__ = property(lambda self: self) sys.modules['local_settings'] = dummymodule(local_settings_file) -except IOError: - pass +except IOError, e: + from django.core.exceptions import ImproperlyConfigured + if os.path.exists(settings_file): + msg = 'Unable to load %s: %s' % (local_settings_file, str(e)) + raise ImproperlyConfigured(msg) diff --git a/awx/settings/production.py b/awx/settings/production.py index 722e3bf15c..87ab321c4f 100644 --- a/awx/settings/production.py +++ b/awx/settings/production.py @@ -33,10 +33,13 @@ settings_file = os.environ.get('AWX_SETTINGS_FILE', '/etc/awx/settings.py') try: execfile(settings_file) -except IOError: +except IOError, e: from django.core.exceptions import ImproperlyConfigured - msg = 'No AWX configuration found at %s.' % settings_file - if 'AWX_SETTINGS_FILE' not in os.environ: - msg += '\nDefine the AWX_SETTINGS_FILE environment variable to specify' - msg += ' an alternate path.' + if not os.path.exists(settings_file): + msg = 'No AWX configuration found at %s.' % settings_file + if 'AWX_SETTINGS_FILE' not in os.environ: + msg += '\nDefine the AWX_SETTINGS_FILE environment variable to ' + msg += 'specify an alternate path.' + else: + msg = 'Unable to load %s: %s' % (settings_file, str(e)) raise ImproperlyConfigured(msg) diff --git a/awx/wsgi.py b/awx/wsgi.py index 51e568373c..f12b1ac5ac 100644 --- a/awx/wsgi.py +++ b/awx/wsgi.py @@ -10,25 +10,9 @@ For more information on this file, see https://docs.djangoproject.com/en/dev/howto/deployment/wsgi/ """ -import os -import sys -import warnings -from awx import MODE -from distutils.sysconfig import get_python_lib - -# Update the default settings environment variable based on current mode. -os.environ.setdefault('DJANGO_SETTINGS_MODULE', 'awx.settings.%s' % MODE) - -# Add local site-packages directory to path. -local_site_packages = os.path.join(os.path.dirname(__file__), 'lib', - 'site-packages') -sys.path.insert(0, local_site_packages) - -# Hide DeprecationWarnings when running in production. Need to first load -# settings to apply our filter after Django's own warnings filter. -from django.conf import settings -if not settings.DEBUG: - warnings.simplefilter('ignore', DeprecationWarning) +# Prepare the AWX environment. +from awx import prepare_env +prepare_env() # Return the default Django WSGI application. from django.core.wsgi import get_wsgi_application