Skip to content

Instantly share code, notes, and snippets.

@avelino
Created April 2, 2014 05:21
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save avelino/9928377 to your computer and use it in GitHub Desktop.
Save avelino/9928377 to your computer and use it in GitHub Desktop.
class AllImagesCheckPermissionForObjectsNode(Node):
def __init__(self, obj, name):
self.obj = Variable(obj)
self.name = name
def render(self, context):
check_published = True
if context['request'].user.is_staff or \
context['request'].user.is_superuser:
check_published = False
context[self.name] = self.obj.resolve(context).all_images(
check_published)
return ''
@register.tag(name='all_images_check_permission')
def all_images_check_permission(parser, token):
"""
{% all_images_check_permission object as images %}
"""
try:
parans = token.split_contents()
except ValueError:
raise TemplateSyntaxError(
_('tag requires exactly two arguments'))
if len(parans) != 4:
raise TemplateSyntaxError(
_('tag requires exactly three arguments'))
if parans[2] != 'as':
raise TemplateSyntaxError(
_("second argument to tag must be 'as'"))
return AllImagesCheckPermissionForObjectsNode(parans[1], parans[3])
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment