1 from django import template
2 from django.template.loader_tags import do_include
3 from django.core.files.storage import default_storage
4 from myslice.settings import theme
6 register = template.Library()
8 class IncludeNode(template.Node):
10 def __init__(self, template_name):
12 self.theme_template_name = "%s%s" % (theme, template_name)
13 self.template_name = template_name
15 def render(self, context):
17 # Loading the template and rendering it
18 included_template = template.loader.get_template(self.theme_template_name).render(context)
19 except template.TemplateDoesNotExist:
20 # template theme does not exists, try the generic one
22 # Loading the template and rendering it
23 included_template = template.loader.get_template(self.template_name).render(context)
24 except template.TemplateDoesNotExist:
25 included_template = ''
27 return included_template
30 def widget(parser, token):
31 """Usage: {% widget "widget.html" %}
33 This will fail silently if the template doesn't exist. If it does, it will
34 be rendered with the current context."""
36 tag_name, template_name = token.split_contents()
38 raise template.TemplateSyntaxError, \
39 "%r tag requires a single argument" % token.contents.split()[0]
41 return IncludeNode(template_name[1:-1])
43 @register.filter(name='file_exists')
44 def file_exists(filepath):
45 if default_storage.exists('portal' + filepath):
48 index = filepath.rfind('/')
49 new_filepath = filepath[:index] + '/image.png'