From d4fb2bf2b3ba4f70a93a1f69766746d5b310dca7 Mon Sep 17 00:00:00 2001 From: jkuczm Date: Sat, 14 Sep 2013 18:26:38 +0200 Subject: [PATCH 1/2] Unpickle context only from `phased` blocks Before this commit `utils.second_pass_render` tried to unpickle stashed context from all bits of content resulting from splitting of template by `PHASED_SECRET_DELIMITER`. Only odd bits are results of `phased` block rendering, so only those bits contain stashed context. Even bits can't contain stashed context and since they are results of first pass rendering they can be arbitrary long and even for content of sane length searching for regular expression match done in `unpickle_context` can take insanely long time. Fixed by unpickling context only from odd bits. Fixes: #9 --- phased/utils.py | 7 ++++--- 1 file changed, 4 insertions(+), 3 deletions(-) diff --git a/phased/utils.py b/phased/utils.py index 14b6508..98f5f8d 100644 --- a/phased/utils.py +++ b/phased/utils.py @@ -34,11 +34,12 @@ def second_pass_render(request, content): for index, bit in enumerate(content.split(settings.PHASED_SECRET_DELIMITER)): if index % 2: tokens = Lexer(bit, None).tokenize() + context = RequestContext(request, + restore_csrf_token(request, unpickle_context(bit))) else: - tokens.append(Token(TOKEN_TEXT, bit)) + tokens = [Token(TOKEN_TEXT, bit)] + context = None - context = RequestContext(request, - restore_csrf_token(request, unpickle_context(bit))) rendered = Parser(tokens).parse().render(context) if settings.PHASED_SECRET_DELIMITER in rendered: From af265c854d9e0e216e095dab8a724a5d798b5745 Mon Sep 17 00:00:00 2001 From: Philip Neustrom Date: Mon, 25 Aug 2014 21:11:06 +0800 Subject: [PATCH 2/2] Comments to improve second_pass_render() clarity. --- phased/utils.py | 3 +++ 1 file changed, 3 insertions(+) diff --git a/phased/utils.py b/phased/utils.py index 98f5f8d..214d5d8 100644 --- a/phased/utils.py +++ b/phased/utils.py @@ -33,15 +33,18 @@ def second_pass_render(request, content): result = tokens = [] for index, bit in enumerate(content.split(settings.PHASED_SECRET_DELIMITER)): if index % 2: + # We split(), so every other index is a phased content block: tokens = Lexer(bit, None).tokenize() context = RequestContext(request, restore_csrf_token(request, unpickle_context(bit))) else: + # We're not in a phased content block, so don't bother restoring context: tokens = [Token(TOKEN_TEXT, bit)] context = None rendered = Parser(tokens).parse().render(context) + # Phased blocks can contain phased blocks. if settings.PHASED_SECRET_DELIMITER in rendered: rendered = second_pass_render(request, rendered) result.append(rendered)