def test_second_pass(self): request = HttpRequest() request.method = 'GET' first_render = compile_string(self.test_template, None).render(Context({'test_var': 'TEST'})) second_render = second_pass_render(request, first_render) self.assertEqual(second_render, 'firstsecondTEST')
def test_second_pass(self): request = self.factory.get('/') context = Context({ 'test_var': 'TEST', 'test_condition': True, 'test_condition2': True, }) first_render = compile_string(self.test_template, None).render(context) second_render = second_pass_render(request, first_render) self.assertEqual(second_render, 'teststashedTESTtest2stashed')
def process_response(self, request, response): """ If the content-type starts with ``text/html`` performs a second-phase render on response.content and updates the ``Content-Length`` header of the response to reflect the change in size after rendering. """ if not response['content-type'].startswith("text/html"): return response response.content = second_pass_render(request, response.content) response['Content-Length'] = str(len(response.content)) return response
def test_second_pass(self): request = HttpRequest() request.method = 'GET' context = Context({ 'test_var': 'TEST', 'test_condition': True, 'test_condition2': True, }) first_render = compile_string(self.test_template, None).render(context) second_render = second_pass_render(request, first_render) self.assertEqual(second_render, 'teststashedTESTtest2stashed')
def render(self, context): """ Template tag that acts like Django's cached tag except that it does a second pass rendering. Requires `RequestContext` and `django.core.context_processors.request` to be in TEMPLATE_CONTEXT_PROCESSORS """ content = super(PhasedCacheNode, self).render(context) return second_pass_render(context['request'], content)
def test_second_pass(self): request = self.factory.get('/') context = Context({ 'test_var': 'TEST', 'test_var2': 'TEST2', 'test_condition': True, }) first_render = compile_string(self.test_template, None).render(context) original_context = unpickle_context(first_render) self.assertEqual(original_context.get('test_var'), 'TEST') second_render = second_pass_render(request, first_render) self.assertEqual(second_render, 'teststashedTEST')
def process_response(self, request, response): """ If the content-type is "text/html", perform second-phase render on response.content and update response['Content-Length'] to reflect change in size after rendering. """ if not response['content-type'].startswith("text/html"): return response response.content = second_pass_render(request, response.content) response['Content-Length'] = str(len(response.content)) if request.META.get('CSRF_COOKIE', False): response.set_cookie(settings.CSRF_COOKIE_NAME, request.META["CSRF_COOKIE"], max_age = 60 * 60 * 24 * 7 * 52, domain=settings.CSRF_COOKIE_DOMAIN) return response
def second_pass_render(request, content): """ Split on the secret delimiter and generate the token list by passing through text outside of phased blocks as single text tokens and tokenizing text inside the phased blocks. This ensures that nothing outside of the phased blocks is tokenized, thus eliminating the possibility of a template code injection vulnerability. """ result = tokens = [] for index, bit in enumerate(content.split(settings.PHASED_SECRET_DELIMITER)): if index % 2: tokens = Lexer(bit, None).tokenize() else: tokens.append(Token(TOKEN_TEXT, bit)) context = RequestContext(request, restore_csrf_token(request, unpickle_context(bit))) rendered = Parser(tokens).parse().render(context) if settings.PHASED_SECRET_DELIMITER in rendered: rendered = second_pass_render(request, rendered) result.append(rendered) return "".join(result)
def test_second_pass(self): request = self.factory.get('/') first_render = compile_string(self.test_template, None).render(Context({'test_var': 'TEST'})) second_render = second_pass_render(request, first_render) self.assertEqual(second_render, 'firstsecondTEST')