from os.path import exists

from django import template
from django.template.loader import render_to_string

register = template.Library()

from pyalp_page.module_node import ModuleNode


class EndModuleNode(ModuleNode):
    template_name = 'end_module.html'

    def __init__(self, module_type="main", bgcolor=""):
        self.module_type = module_type
        self.bgcolor = bgcolor

    def render(self, context):
        # global colors, container, master
        skin = context['skin']

        # globalize this for multiple formats??
        if self.bgcolor:
            current_color = self.bgcolor
        else:
            current_color = skin.colors["cell_background"]

        sides = ["tl", "tr", "bl", "br", "t", "b", "lside", "rside"]
        imgs = {
            "mod" + side: exists(skin.asset_path + "mod" + side + ".gif")
            for side in sides
        }

        dims = {}
        for key, value in imgs.items():
            if value:
                width, height = skin.getimagesize(
                    skin.asset_path + key + ".gif")
            else:
                width, height = (
                    skin.container["border_width"],
                    skin.container["border_height"]
                )
            dims[key] = {
                'width': width,
                'height': height
            }

        render_context = {
            'dims': dims,
            'current_color': current_color
        }

        return render_to_string(
            'end_module.html',
            render_context,
            context_instance=context
        )


@register.tag
def end_module(parser, token):
    return EndModuleNode()

@@ -2,110 +2,30 @@
 rom 
-os.path import exists%0A%0Afrom django import template%0Afrom django.template.loader import render_to_string
+django import template
 %0A%0Are
@@ -242,1272 +242,64 @@
    s
-elf.module_type = module_type%0A        self.bgcolor = bgcolor%0A%0A    def render(self, context):%0A        # global colors, container, master%0A        skin = context%5B'skin'%5D%0A%0A        # globalize this for multiple formats??%0A        if self.bgcolor:%0A            current_color = self.bgcolor%0A        else:%0A            current_color = skin.colors%5B%22cell_background%22%5D%0A%0A        sides = %5B%22tl%22, %22tr%22, %22bl%22, %22br%22, %22t%22, %22b%22, %22lside%22, %22rside%22%5D%0A        imgs = %7B%0A            %22mod%22 + side: exists(skin.asset_path + %22mod%22 + side + %22.gif%22)%0A            for side in sides%0A        %7D%0A%0A        dims = %7B%7D%0A        for key, value in imgs.items():%0A            if value:%0A                width, height = skin.getimagesize(%0A                    skin.asset_path + key + %22.gif%22)%0A            else:%0A                width, height = (%0A                    skin.container%5B%22border_width%22%5D,%0A                    skin.container%5B%22border_height%22%5D%0A                )%0A            dims%5Bkey%5D = %7B%0A                'width': width,%0A                'height': height%0A            %7D%0A%0A        render_context = %7B%0A            'dims': dims,%0A            'current_color': current_color%0A        %7D%0A%0A        return render_to_string(%0A            'end_module.html',%0A            render_context,%0A            context_instance=context%0A        
+uper().__init__(module_type=module_type, bgcolor=bgcolor
 )%0A%0A%0A

Remove duplicate code