Merge pull request #6189 from edx/andya/fix-masquerade

Fix Mako templates to always use updated request context
This commit is contained in:
Andy Armstrong
2014-12-10 11:39:29 -05:00
14 changed files with 100 additions and 44 deletions

View File

@@ -7,7 +7,7 @@
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distribuetd under the License is distributed on an "AS IS" BASIS,
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
@@ -22,10 +22,24 @@ REQUEST_CONTEXT = threading.local()
class MakoMiddleware(object):
def process_request(self, request):
REQUEST_CONTEXT.context = RequestContext(request)
REQUEST_CONTEXT.context['is_secure'] = request.is_secure()
REQUEST_CONTEXT.context['site'] = safe_get_host(request)
""" Process the middleware request. """
REQUEST_CONTEXT.request = request
def process_response(self, request, response):
REQUEST_CONTEXT.context = None
def process_response(self, __, response):
""" Process the middleware response. """
REQUEST_CONTEXT.request = None
return response
def get_template_request_context():
"""
Returns the template processing context to use for the current request,
or returns None if there is not a current request.
"""
request = getattr(REQUEST_CONTEXT, "request", None)
if not request:
return None
context = RequestContext(request)
context['is_secure'] = request.is_secure()
context['site'] = safe_get_host(request)
return context

View File

@@ -19,7 +19,7 @@ import logging
from microsite_configuration import microsite
from edxmako import lookup_template
import edxmako.middleware
from edxmako.middleware import get_template_request_context
from django.conf import settings
from django.core.urlresolvers import reverse
log = logging.getLogger(__name__)
@@ -114,11 +114,12 @@ def render_to_string(template_name, dictionary, context=None, namespace='main'):
context_instance['marketing_link'] = marketing_link
# In various testing contexts, there might not be a current request context.
if getattr(edxmako.middleware.REQUEST_CONTEXT, "context", None):
for d in edxmako.middleware.REQUEST_CONTEXT.context:
context_dictionary.update(d)
for d in context_instance:
context_dictionary.update(d)
request_context = get_template_request_context()
if request_context:
for item in request_context:
context_dictionary.update(item)
for item in context_instance:
context_dictionary.update(item)
if context:
context_dictionary.update(context)
# fetch and render template

View File

@@ -12,12 +12,12 @@
# See the License for the specific language governing permissions and
# limitations under the License.
from django.conf import settings
from mako.template import Template as MakoTemplate
from edxmako.shortcuts import marketing_link
import edxmako
import edxmako.middleware
from django.conf import settings
from edxmako.middleware import get_template_request_context
from edxmako.shortcuts import marketing_link
from mako.template import Template as MakoTemplate
DJANGO_VARIABLES = ['output_encoding', 'encoding_errors']
@@ -48,11 +48,12 @@ class Template(MakoTemplate):
context_dictionary = {}
# In various testing contexts, there might not be a current request context.
if getattr(edxmako.middleware.REQUEST_CONTEXT, "context", None):
for d in edxmako.middleware.REQUEST_CONTEXT.context:
context_dictionary.update(d)
for d in context_instance:
context_dictionary.update(d)
request_context = get_template_request_context()
if request_context:
for item in request_context:
context_dictionary.update(item)
for item in context_instance:
context_dictionary.update(item)
context_dictionary['settings'] = settings
context_dictionary['EDX_ROOT_URL'] = settings.EDX_ROOT_URL
context_dictionary['django_context'] = context_instance

View File

@@ -10,6 +10,7 @@ from django.test.utils import override_settings
from django.test.client import RequestFactory
from django.core.urlresolvers import reverse
import edxmako.middleware
from edxmako.middleware import get_template_request_context
from edxmako import add_lookup, LOOKUP
from edxmako.shortcuts import (
marketing_link,
@@ -83,11 +84,11 @@ class MakoMiddlewareTest(TestCase):
self.middleware.process_request(self.request)
# requestcontext should not be None.
self.assertIsNotNone(edxmako.middleware.REQUEST_CONTEXT.context)
self.assertIsNotNone(get_template_request_context())
self.middleware.process_response(self.request, self.response)
# requestcontext should be None.
self.assertIsNone(edxmako.middleware.REQUEST_CONTEXT.context)
self.assertIsNone(get_template_request_context())
@unittest.skipUnless(settings.ROOT_URLCONF == 'lms.urls', 'Test only valid in lms')
@patch("edxmako.middleware.REQUEST_CONTEXT")