Skip to content

Commit

Permalink
pulled code from django-compressor and removed it as dependency
Browse files Browse the repository at this point in the history
  • Loading branch information
michaelpoi committed Dec 27, 2024
1 parent ecd4df2 commit 6cf6d4c
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 6 deletions.
3 changes: 3 additions & 0 deletions demoapp/.coveragerc
Original file line number Diff line number Diff line change
Expand Up @@ -5,3 +5,6 @@ omit =
*/sendmail/tests/*
*/sendmail/admin/*
*/demoapp/*
*/sendmail/django_compressor.py
*/sendmail/config.py
*/sendmail/checks.py
1 change: 0 additions & 1 deletion demoapp/tests/requirements/test_requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -12,4 +12,3 @@ pytest-cov
redis
requests
django-jsoneditor
django-compressor
1 change: 0 additions & 1 deletion requirements.txt
Original file line number Diff line number Diff line change
Expand Up @@ -94,4 +94,3 @@ webencodings==0.5.1
wheel==0.44.0
zipp==3.20.0
django-jsoneditor==0.2.4
django-compressor==4.5.1
93 changes: 93 additions & 0 deletions sendmail/django_compressor.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,93 @@
from copy import copy

from django.template.base import VariableNode, TextNode, NodeList
from django.template.defaulttags import IfNode
from django.template.loader_tags import (
BLOCK_CONTEXT_KEY,
ExtendsNode,
BlockNode,
BlockContext,
)



def handle_extendsnode(extendsnode, context):
"""Create a copy of Node tree of a derived template replacing
all blocks tags with the nodes of appropriate blocks.
Also handles {{ block.super }} tags.
"""
if BLOCK_CONTEXT_KEY not in context.render_context:
context.render_context[BLOCK_CONTEXT_KEY] = BlockContext()
block_context = context.render_context[BLOCK_CONTEXT_KEY]
blocks = dict(
(n.name, n) for n in extendsnode.nodelist.get_nodes_by_type(BlockNode)
)
block_context.add_blocks(blocks)

compiled_parent = extendsnode.get_parent(context)
parent_nodelist = compiled_parent.nodelist
# If the parent template has an ExtendsNode it is not the root.
for node in parent_nodelist:
# The ExtendsNode has to be the first non-text node.
if not isinstance(node, TextNode):
if isinstance(node, ExtendsNode):
return handle_extendsnode(node, context)
break
# Add blocks of the root template to block context.
blocks = dict((n.name, n) for n in parent_nodelist.get_nodes_by_type(BlockNode))
block_context.add_blocks(blocks)

block_stack = []
new_nodelist = remove_block_nodes(parent_nodelist, block_stack, block_context)
return new_nodelist


def remove_block_nodes(nodelist, block_stack, block_context):
new_nodelist = NodeList()
for node in nodelist:
if isinstance(node, VariableNode):
var_name = node.filter_expression.token.strip()
if var_name == "block.super":
if not block_stack:
continue
node = block_context.get_block(block_stack[-1].name)
if not node:
continue
if isinstance(node, BlockNode):
expanded_block = expand_blocknode(node, block_stack, block_context)
new_nodelist.extend(expanded_block)
else:
# IfNode has nodelist as a @property so we can not modify it
if isinstance(node, IfNode):
node = copy(node)
for i, (condition, sub_nodelist) in enumerate(
node.conditions_nodelists
):
sub_nodelist = remove_block_nodes(
sub_nodelist, block_stack, block_context
)
node.conditions_nodelists[i] = (condition, sub_nodelist)
else:
for attr in node.child_nodelists:
sub_nodelist = getattr(node, attr, None)
if sub_nodelist:
sub_nodelist = remove_block_nodes(
sub_nodelist, block_stack, block_context
)
node = copy(node)
setattr(node, attr, sub_nodelist)
new_nodelist.append(node)
return new_nodelist


def expand_blocknode(node, block_stack, block_context):
popped_block = block = block_context.pop(node.name)
if block is None:
block = node
block_stack.append(block)
expanded_nodelist = remove_block_nodes(block.nodelist, block_stack, block_context)
block_stack.pop()
if popped_block is not None:
block_context.push(node.name, popped_block)
return expanded_nodelist

21 changes: 17 additions & 4 deletions sendmail/parser.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,13 @@
import re

from django.template import loader
from django import template
from django.template import loader, TemplateSyntaxError, TemplateDoesNotExist
from django.template.base import Node, NodeList, VariableNode
from django.template.context import Context
from django.template.defaulttags import ForNode
from django.template.loader import get_template
from django.template.loader_tags import ExtendsNode, IncludeNode

from compressor.offline.django import DjangoParser, handle_extendsnode
from sendmail.django_compressor import handle_extendsnode


def handle_includenode(includenode, context):
Expand All @@ -26,7 +27,19 @@ def handle_includenode(includenode, context):
return included_template.template.nodelist


class SendmailParser(DjangoParser):
class SendmailParser:

def __init__(self, charset):
self.charset = charset

def parse(self, template_name):
try:
return get_template(template_name).template
except template.TemplateSyntaxError as e:
raise TemplateSyntaxError(str(e))
except template.TemplateDoesNotExist as e:
raise TemplateDoesNotExist(str(e))

def get_nodelist(self, node, original, context=None):
if isinstance(node, ExtendsNode):
if context is None:
Expand Down

0 comments on commit 6cf6d4c

Please sign in to comment.