You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
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):
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 hasattr(obj.instance, 'DoesNotExist'):
obj = obj.instance
else:
self.codename = 'no permission method found'
if not user_obj.is_authenticated:
user_obj = AnonymousUser()
if hasattr(obj,self.codename):
context[self.varname] = getattr(obj,self.codename)(user_obj)
else:
context[self.varname]=False
return ''
The text was updated successfully, but these errors were encountered:
I could very well put your code in, but I think it's a good thing to learn how to make pull requests: it's simple and useful for the future, I suppose.
You're very welcome for django-rulez :) Glad it helps someone else.
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):
The text was updated successfully, but these errors were encountered: