Merge pull request #3854 from beeankha/add_debug_toolbar

Update Custom Middleware to New Style

Reviewed-by: https://github.com/softwarefactory-project-zuul[bot]
This commit is contained in:
softwarefactory-project-zuul[bot]
2019-05-15 18:38:24 +00:00
committed by GitHub
5 changed files with 52 additions and 80 deletions

View File

@@ -18,6 +18,7 @@ from django.db import IntegrityError, connection
from django.utils.functional import curry from django.utils.functional import curry
from django.shortcuts import get_object_or_404, redirect from django.shortcuts import get_object_or_404, redirect
from django.apps import apps from django.apps import apps
from django.utils.deprecation import MiddlewareMixin
from django.utils.translation import ugettext_lazy as _ from django.utils.translation import ugettext_lazy as _
from django.urls import reverse, resolve from django.urls import reverse, resolve
@@ -31,7 +32,7 @@ analytics_logger = logging.getLogger('awx.analytics.activity_stream')
perf_logger = logging.getLogger('awx.analytics.performance') perf_logger = logging.getLogger('awx.analytics.performance')
class TimingMiddleware(threading.local): class TimingMiddleware(threading.local, MiddlewareMixin):
dest = '/var/log/tower/profile' dest = '/var/log/tower/profile'
@@ -64,11 +65,12 @@ class TimingMiddleware(threading.local):
return filepath return filepath
class ActivityStreamMiddleware(threading.local): class ActivityStreamMiddleware(threading.local, MiddlewareMixin):
def __init__(self): def __init__(self, get_response=None):
self.disp_uid = None self.disp_uid = None
self.instance_ids = [] self.instance_ids = []
super().__init__(get_response)
def process_request(self, request): def process_request(self, request):
if hasattr(request, 'user') and hasattr(request.user, 'is_authenticated') and request.user.is_authenticated(): if hasattr(request, 'user') and hasattr(request.user, 'is_authenticated') and request.user.is_authenticated():
@@ -118,7 +120,7 @@ class ActivityStreamMiddleware(threading.local):
self.instance_ids.append(instance.id) self.instance_ids.append(instance.id)
class SessionTimeoutMiddleware(object): class SessionTimeoutMiddleware(MiddlewareMixin):
""" """
Resets the session timeout for both the UI and the actual session for the API Resets the session timeout for both the UI and the actual session for the API
to the value of SESSION_COOKIE_AGE on every request if there is a valid session. to the value of SESSION_COOKIE_AGE on every request if there is a valid session.
@@ -151,9 +153,9 @@ def _customize_graph():
settings.NAMED_URL_GRAPH[Instance].add_bindings() settings.NAMED_URL_GRAPH[Instance].add_bindings()
class URLModificationMiddleware(object): class URLModificationMiddleware(MiddlewareMixin):
def __init__(self): def __init__(self, get_response=None):
models = [m for m in apps.get_app_config('main').get_models() if hasattr(m, 'get_absolute_url')] models = [m for m in apps.get_app_config('main').get_models() if hasattr(m, 'get_absolute_url')]
generate_graph(models) generate_graph(models)
_customize_graph() _customize_graph()
@@ -177,6 +179,7 @@ class URLModificationMiddleware(object):
category=_('Named URL'), category=_('Named URL'),
category_slug='named-url', category_slug='named-url',
) )
super().__init__(get_response)
def _named_url_to_pk(self, node, named_url): def _named_url_to_pk(self, node, named_url):
kwargs = {} kwargs = {}
@@ -207,7 +210,7 @@ class URLModificationMiddleware(object):
request.path_info = new_path request.path_info = new_path
class MigrationRanCheckMiddleware(object): class MigrationRanCheckMiddleware(MiddlewareMixin):
def process_request(self, request): def process_request(self, request):
executor = MigrationExecutor(connection) executor = MigrationExecutor(connection)

View File

@@ -251,29 +251,11 @@ TEMPLATES = [
}, },
] ]
MIDDLEWARE_CLASSES = ( # NOQA
'awx.main.middleware.TimingMiddleware',
'awx.main.middleware.MigrationRanCheckMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'awx.main.middleware.ActivityStreamMiddleware',
'awx.sso.middleware.SocialAuthMiddleware',
'crum.CurrentRequestUserMiddleware',
'awx.main.middleware.URLModificationMiddleware',
'awx.main.middleware.SessionTimeoutMiddleware',
)
ROOT_URLCONF = 'awx.urls' ROOT_URLCONF = 'awx.urls'
WSGI_APPLICATION = 'awx.wsgi.application' WSGI_APPLICATION = 'awx.wsgi.application'
INSTALLED_APPS = ( INSTALLED_APPS = [
'django.contrib.auth', 'django.contrib.auth',
'django.contrib.contenttypes', 'django.contrib.contenttypes',
'django.contrib.messages', 'django.contrib.messages',
@@ -294,7 +276,7 @@ INSTALLED_APPS = (
'awx.ui', 'awx.ui',
'awx.sso', 'awx.sso',
'solo' 'solo'
) ]
INTERNAL_IPS = ('127.0.0.1',) INTERNAL_IPS = ('127.0.0.1',)
@@ -447,16 +429,6 @@ AWX_ISOLATED_VERBOSITY = 0
# } # }
# } # }
# Use Django-Debug-Toolbar if installed.
try:
import debug_toolbar
INSTALLED_APPS += (debug_toolbar.__name__,)
except ImportError:
pass
DEBUG_TOOLBAR_CONFIG = {
'ENABLE_STACKTRACES' : True,
}
DEVSERVER_DEFAULT_ADDR = '0.0.0.0' DEVSERVER_DEFAULT_ADDR = '0.0.0.0'
DEVSERVER_DEFAULT_PORT = '8013' DEVSERVER_DEFAULT_PORT = '8013'
@@ -1211,3 +1183,20 @@ AWX_REQUEST_PROFILE = False
# Delete temporary directories created to store playbook run-time # Delete temporary directories created to store playbook run-time
AWX_CLEANUP_PATHS = True AWX_CLEANUP_PATHS = True
MIDDLEWARE = [
'awx.main.middleware.TimingMiddleware',
'awx.main.middleware.MigrationRanCheckMiddleware',
'corsheaders.middleware.CorsMiddleware',
'django.contrib.sessions.middleware.SessionMiddleware',
'django.middleware.locale.LocaleMiddleware',
'django.middleware.common.CommonMiddleware',
'django.middleware.csrf.CsrfViewMiddleware',
'django.contrib.auth.middleware.AuthenticationMiddleware',
'django.contrib.messages.middleware.MessageMiddleware',
'awx.main.middleware.ActivityStreamMiddleware',
'awx.sso.middleware.SocialAuthMiddleware',
'crum.CurrentRequestUserMiddleware',
'awx.main.middleware.URLModificationMiddleware',
'awx.main.middleware.SessionTimeoutMiddleware',
]

View File

@@ -93,7 +93,7 @@ INSIGHTS_TRACKING_STATE = False
# Use Django-Jenkins if installed. Only run tests for awx.main app. # Use Django-Jenkins if installed. Only run tests for awx.main app.
try: try:
import django_jenkins import django_jenkins
INSTALLED_APPS += (django_jenkins.__name__,) # noqa INSTALLED_APPS += [django_jenkins.__name__,] # noqa
PROJECT_APPS = ('awx.main.tests', 'awx.api.tests',) PROJECT_APPS = ('awx.main.tests', 'awx.api.tests',)
except ImportError: except ImportError:
pass pass
@@ -112,7 +112,18 @@ if 'django_jenkins' in INSTALLED_APPS:
PEP8_RCFILE = "setup.cfg" PEP8_RCFILE = "setup.cfg"
PYLINT_RCFILE = ".pylintrc" PYLINT_RCFILE = ".pylintrc"
INSTALLED_APPS += ('rest_framework_swagger',) INSTALLED_APPS += [ # NOQA
'rest_framework_swagger',
'debug_toolbar',
]
MIDDLEWARE += [ # NOQA
'debug_toolbar.middleware.DebugToolbarMiddleware',
]
DEBUG_TOOLBAR_CONFIG = {
'ENABLE_STACKTRACES' : True,
}
# Configure a default UUID for development only. # Configure a default UUID for development only.
SYSTEM_UUID = '00000000-0000-0000-0000-000000000000' SYSTEM_UUID = '00000000-0000-0000-0000-000000000000'

View File

@@ -4,8 +4,6 @@
# Python # Python
import urllib.parse import urllib.parse
# Six
# Django # Django
from django.conf import settings from django.conf import settings
from django.utils.functional import LazyObject from django.utils.functional import LazyObject
@@ -20,10 +18,6 @@ from social_django.middleware import SocialAuthExceptionMiddleware
class SocialAuthMiddleware(SocialAuthExceptionMiddleware): class SocialAuthMiddleware(SocialAuthExceptionMiddleware):
def process_view(self, request, callback, callback_args, callback_kwargs):
if request.path.startswith('/sso/login/'):
request.session['social_auth_last_backend'] = callback_kwargs['backend']
def process_request(self, request): def process_request(self, request):
if request.path.startswith('/sso'): if request.path.startswith('/sso'):
# django-social keeps a list of backends in memory that it gathers # django-social keeps a list of backends in memory that it gathers
@@ -53,6 +47,11 @@ class SocialAuthMiddleware(SocialAuthExceptionMiddleware):
request.user = request.user._wrapped request.user = request.user._wrapped
request.session.pop('social_auth_error', None) request.session.pop('social_auth_error', None)
request.session.pop('social_auth_last_backend', None) request.session.pop('social_auth_last_backend', None)
return self.get_response(request)
def process_view(self, request, callback, callback_args, callback_kwargs):
if request.path.startswith('/sso/login/'):
request.session['social_auth_last_backend'] = callback_kwargs['backend']
def process_exception(self, request, exception): def process_exception(self, request, exception):
strategy = getattr(request, 'social_strategy', None) strategy = getattr(request, 'social_strategy', None)

View File

@@ -8,11 +8,10 @@ from awx import __version__ as tower_version
from awx import prepare_env, MODE from awx import prepare_env, MODE
prepare_env() prepare_env()
from django.core.wsgi import WSGIHandler # NOQA
import django # NOQA import django # NOQA
from django.conf import settings # NOQA from django.conf import settings # NOQA
from django.urls import resolve # NOQA from django.urls import resolve # NOQA
from django.core.wsgi import get_wsgi_application # NOQA
import social_django # NOQA import social_django # NOQA
@@ -41,41 +40,12 @@ if social_django.__version__ != '2.1.0':
still works".format(social_django.__version__)) still works".format(social_django.__version__))
if django.__version__ != '1.11.20': if not django.__version__.startswith('1.'):
raise RuntimeError("Django version other than 1.11.20 detected {}. \ raise RuntimeError("Django version other than 1.XX detected {}. \
Inherit from WSGIHandler to support short-circuit Django Middleware. \ Inherit from WSGIHandler to support short-circuit Django Middleware. \
This is known to work for Django 1.11.20 and may not work with other, \ This is known to work for Django 1.XX and may not work with other, \
even minor, versions.".format(django.__version__)) even minor, versions.".format(django.__version__))
if settings.MIDDLEWARE:
raise RuntimeError("MIDDLEWARE setting detected. \
The 'migration in progress' view feature short-circuits OLD Django \
MIDDLEWARE_CLASSES behavior. With the new Django MIDDLEWARE beahvior \
it's possible to short-ciruit the middleware onion through supported \
middleware mechanisms. Further, from django.core.wsgi.get_wsgi_application() \
should be called to get an instance of WSGIHandler().")
class AWXWSGIHandler(WSGIHandler):
def _legacy_get_response(self, request):
try:
# resolve can raise a 404, in that case, pass through to the
# "normal" middleware
if getattr(resolve(request.path), 'url_name', '') == 'migrations_notran':
# short-circuit middleware
request._cors_enabled = False
return self._get_response(request)
except django.urls.Resolver404:
pass
# fall through to middle-ware
return super(AWXWSGIHandler, self)._legacy_get_response(request)
# Return the default Django WSGI application. # Return the default Django WSGI application.
def get_wsgi_application():
django.setup(set_prefix=False)
return AWXWSGIHandler()
application = get_wsgi_application() application = get_wsgi_application()