Handle the caching of responses between Python 2 and Python 3.

This commit is contained in:
Diana Huang
2019-12-12 11:14:40 -05:00
parent fe5a862153
commit e005c0461e
3 changed files with 7 additions and 25 deletions

View File

@@ -36,28 +36,6 @@ META_KEY_TO_CONTEXT_KEY = {
}
class ResponseLoggingMiddleware(object):
"""
A debugging middleware for the purpose of understanding what the current state of the response
is at this point in the stack.
"""
def process_response(self, _request, response):
"""
Logs the response at the current point in the middleware stack for debugging purposes.
"""
try:
log.info('Logging response for debugging purposes: %r', response)
log.info('Response container data: %r', response._container) # pylint: disable=protected-access
log.info('Container types: %r', [type(el) for el in response._container]) # pylint: disable=protected-access
except: # pylint: disable=bare-except
# If this causes an error, we don't want it to bubble up since this is just for logging.
log.exception('Error logging response object')
return response
class TrackMiddleware(object):
"""
Tracks all requests made, as well as setting up context for other server

View File

@@ -76,6 +76,13 @@ def cache_if_anonymous(*get_parameters):
})
response = cache.get(cache_key) # pylint: disable=maybe-no-member
# A hack to ensure that the response data is a valid text type for both Python 2 and 3.
response_content = response._container.copy() # pylint: disable=protected-member
response.content = b''
for item in response_content:
response.write(item)
if not response:
response = view_func(request, *args, **kwargs)
cache.set(cache_key, response, 60 * 3) # pylint: disable=maybe-no-member

View File

@@ -1444,9 +1444,6 @@ MIDDLEWARE_CLASSES = [
'lms.djangoapps.discussion.django_comment_client.middleware.AjaxExceptionMiddleware',
'django.middleware.common.CommonMiddleware',
# Debugging Middleware. Remove once Python 3 has been deployed.
'track.middleware.ResponseLoggingMiddleware',
'django.contrib.sites.middleware.CurrentSiteMiddleware',
'edx_rest_framework_extensions.auth.jwt.middleware.JwtAuthCookieMiddleware',