Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Extension of existing template tag #14

Open
AgDude opened this issue Jan 2, 2012 · 3 comments
Open

Extension of existing template tag #14

AgDude opened this issue Jan 2, 2012 · 3 comments

Comments

@AgDude
Copy link

AgDude commented Jan 2, 2012

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 ''
@chrisglass
Copy link
Owner

Maybe you should consider sending a pull request instead?
A pretty extensive help page is available here: http://help.github.com/send-pull-requests/

It boils down to:

  • Fork the project
  • clone your fork
  • make a feature branch
  • hack hack hack, commit in your feature branch
  • push your branch to your fork
  • use the github interface to "send a pull request" from your branch.

Voila! Easy and nice!

@AgDude
Copy link
Author

AgDude commented Jan 4, 2012

Thanks for the tip. I'll look dig into it sometime in the next few days.

btw: I just started using rulez. Thanks for the great work, it seems to be just what I am looking for,

@chrisglass
Copy link
Owner

Awesome, thanks!

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.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

2 participants