/usr/share/pyshared/classytags/helpers.py is in python-django-classy-tags 0.3.4.1-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 | from classytags.core import Tag
from django.core.exceptions import ImproperlyConfigured
from django.template.context import Context
from django.template.loader import render_to_string
class AsTag(Tag):
"""
Same as tag but allows for an optional 'as varname'. The 'as varname'
options must be added 'manually' to the options class.
"""
def __init__(self, parser, tokens):
super(AsTag, self).__init__(parser, tokens)
if len(self.options.breakpoints) < 1:
raise ImproperlyConfigured(
"AsTag subclasses require at least one breakpoint."
)
last_breakpoint = self.options.options[self.options.breakpoints[-1]]
optscount = len(last_breakpoint)
if optscount != 1:
raise ImproperlyConfigured(
"The last breakpoint of AsTag subclasses require exactly one "
"argument, got %s instead." % optscount
)
self.varname_name = last_breakpoint[-1].name
def render_tag(self, context, **kwargs):
"""
INTERNAL!
Get's the value for the current context and arguments and puts it into
the context if needed or returns it.
"""
varname = kwargs.pop(self.varname_name)
value = self.get_value(context, **kwargs)
if varname:
context[varname] = value
return ''
return value
def get_value(self, context, **kwargs):
"""
Returns the value for the current context and arguments.
"""
raise NotImplementedError
class InclusionTag(Tag):
"""
A helper Tag class which allows easy inclusion tags.
The template attribute must be set.
Instead of render_tag, override get_context in your subclasses.
Optionally override get_template in your subclasses.
"""
template = None
def __init__(self, parser, tokens):
super(InclusionTag, self).__init__(parser, tokens)
if self.template is None:
raise ImproperlyConfigured(
"InclusionTag subclasses require the template attribute to be "
"set."
)
def render_tag(self, context, **kwargs):
"""
INTERNAL!
Gets the context and data to render.
"""
template = self.get_template(context, **kwargs)
data = self.get_context(context, **kwargs)
output = render_to_string(template, data)
return output
def get_template(self, context, **kwargs):
"""
Returns the template to be used for the current context and arguments.
"""
return self.template
def get_context(self, context, **kwargs):
"""
Returns the context to render the template with.
"""
return {}
|