chrisglass/django-rulez — Issue #14: Extension of existing template tag
Author: AgDude
Created: 2012-01-02 00:39:51+00:00
State: open

This is a feature request and patch for a template tag which checks user permission based on a method defined on the model. This template tag will accept either a model instance, or a model form with an instance. This is not backwards compatible with the earlier template tag rulez_perm.

Example:
{% has_perm can_edit pubform as editor %}
{% if editor %}
You can edit this!
{% endif %}

The code:
class RulezPermsNode(template.Node):
def __init__(self, codename, objname, varname):
self.codename = codename
self.objname = objname
self.varname = varname

def render(self, context):
# pdb.set_trace()
user_obj = template.resolve_variable('user', context)
obj = template.resolve_variable(self.objname, context)
#check if the obj is a model instance
if not hasattr(obj, 'DoesNotExist'):
#If obj is a form, then try to get the form instance
if not hasattr(obj.instance, 'DoesNotExist'):
obj = obj.instance
else:
self.codename = None
if not user_obj.is_authenticated:
user_obj = AnonymousUser()
context[self.varname] = getattr(obj,self.codename)(user_obj)
return ''