django use template tag in has_perm()












1















I my project I have to use a model less permission. I wrote my own template tags in order to show or not an action button or content in a template. Here my template tags:



from django.template import Library
from django.contrib.auth.models import Permission

register = Library()


@register.filter(name='is_allowed_for')
def is_allowed_for(permission, user):
if user.has_perm(permission.title()):
return True

return False


@register.filter(name='in_department')
def in_department(permission, department_name):
if permission != '':
new_permission = department_name.title() + '.' + permission.title()
else:
new_permission = department_name.title()
return new_permission


In my template this works like expected: (department_name is passed from the view)



{% if 'Dashboard.View'|in_department:department_name|is_allowed_for:user %}
has Department Dasboard view
{% else %}
has not Department Dasboard view
{% endif %}


This does not work, I receive a TemplateSytaxError:



{% if user.has_perm('Dashboard.View'|in_department:department_name) %}
has Department Dasboard view
{% else %}
has not Department Dasboard view
{% endif %}


Isn't the tag applied before the has_perm method is called? Or what is wrong...? Thx for any help.










share|improve this question

























  • The problem isn't the tag, it's the parentheses.

    – Daniel Roseman
    Jan 20 at 10:27











  • Thx for the response. So, the second approach is not possible?

    – cwhisperer
    Jan 20 at 10:47











  • No. But why don't you write a filter that does all this, including calling has_perm?

    – Daniel Roseman
    Jan 20 at 10:53











  • in_department and is_allowed are filters or not ? Do you have a link with examples?

    – cwhisperer
    Jan 20 at 11:04
















1















I my project I have to use a model less permission. I wrote my own template tags in order to show or not an action button or content in a template. Here my template tags:



from django.template import Library
from django.contrib.auth.models import Permission

register = Library()


@register.filter(name='is_allowed_for')
def is_allowed_for(permission, user):
if user.has_perm(permission.title()):
return True

return False


@register.filter(name='in_department')
def in_department(permission, department_name):
if permission != '':
new_permission = department_name.title() + '.' + permission.title()
else:
new_permission = department_name.title()
return new_permission


In my template this works like expected: (department_name is passed from the view)



{% if 'Dashboard.View'|in_department:department_name|is_allowed_for:user %}
has Department Dasboard view
{% else %}
has not Department Dasboard view
{% endif %}


This does not work, I receive a TemplateSytaxError:



{% if user.has_perm('Dashboard.View'|in_department:department_name) %}
has Department Dasboard view
{% else %}
has not Department Dasboard view
{% endif %}


Isn't the tag applied before the has_perm method is called? Or what is wrong...? Thx for any help.










share|improve this question

























  • The problem isn't the tag, it's the parentheses.

    – Daniel Roseman
    Jan 20 at 10:27











  • Thx for the response. So, the second approach is not possible?

    – cwhisperer
    Jan 20 at 10:47











  • No. But why don't you write a filter that does all this, including calling has_perm?

    – Daniel Roseman
    Jan 20 at 10:53











  • in_department and is_allowed are filters or not ? Do you have a link with examples?

    – cwhisperer
    Jan 20 at 11:04














1












1








1








I my project I have to use a model less permission. I wrote my own template tags in order to show or not an action button or content in a template. Here my template tags:



from django.template import Library
from django.contrib.auth.models import Permission

register = Library()


@register.filter(name='is_allowed_for')
def is_allowed_for(permission, user):
if user.has_perm(permission.title()):
return True

return False


@register.filter(name='in_department')
def in_department(permission, department_name):
if permission != '':
new_permission = department_name.title() + '.' + permission.title()
else:
new_permission = department_name.title()
return new_permission


In my template this works like expected: (department_name is passed from the view)



{% if 'Dashboard.View'|in_department:department_name|is_allowed_for:user %}
has Department Dasboard view
{% else %}
has not Department Dasboard view
{% endif %}


This does not work, I receive a TemplateSytaxError:



{% if user.has_perm('Dashboard.View'|in_department:department_name) %}
has Department Dasboard view
{% else %}
has not Department Dasboard view
{% endif %}


Isn't the tag applied before the has_perm method is called? Or what is wrong...? Thx for any help.










share|improve this question
















I my project I have to use a model less permission. I wrote my own template tags in order to show or not an action button or content in a template. Here my template tags:



from django.template import Library
from django.contrib.auth.models import Permission

register = Library()


@register.filter(name='is_allowed_for')
def is_allowed_for(permission, user):
if user.has_perm(permission.title()):
return True

return False


@register.filter(name='in_department')
def in_department(permission, department_name):
if permission != '':
new_permission = department_name.title() + '.' + permission.title()
else:
new_permission = department_name.title()
return new_permission


In my template this works like expected: (department_name is passed from the view)



{% if 'Dashboard.View'|in_department:department_name|is_allowed_for:user %}
has Department Dasboard view
{% else %}
has not Department Dasboard view
{% endif %}


This does not work, I receive a TemplateSytaxError:



{% if user.has_perm('Dashboard.View'|in_department:department_name) %}
has Department Dasboard view
{% else %}
has not Department Dasboard view
{% endif %}


Isn't the tag applied before the has_perm method is called? Or what is wrong...? Thx for any help.







django templatetags






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Jan 20 at 16:37







cwhisperer

















asked Jan 20 at 9:45









cwhisperercwhisperer

63311833




63311833













  • The problem isn't the tag, it's the parentheses.

    – Daniel Roseman
    Jan 20 at 10:27











  • Thx for the response. So, the second approach is not possible?

    – cwhisperer
    Jan 20 at 10:47











  • No. But why don't you write a filter that does all this, including calling has_perm?

    – Daniel Roseman
    Jan 20 at 10:53











  • in_department and is_allowed are filters or not ? Do you have a link with examples?

    – cwhisperer
    Jan 20 at 11:04



















  • The problem isn't the tag, it's the parentheses.

    – Daniel Roseman
    Jan 20 at 10:27











  • Thx for the response. So, the second approach is not possible?

    – cwhisperer
    Jan 20 at 10:47











  • No. But why don't you write a filter that does all this, including calling has_perm?

    – Daniel Roseman
    Jan 20 at 10:53











  • in_department and is_allowed are filters or not ? Do you have a link with examples?

    – cwhisperer
    Jan 20 at 11:04

















The problem isn't the tag, it's the parentheses.

– Daniel Roseman
Jan 20 at 10:27





The problem isn't the tag, it's the parentheses.

– Daniel Roseman
Jan 20 at 10:27













Thx for the response. So, the second approach is not possible?

– cwhisperer
Jan 20 at 10:47





Thx for the response. So, the second approach is not possible?

– cwhisperer
Jan 20 at 10:47













No. But why don't you write a filter that does all this, including calling has_perm?

– Daniel Roseman
Jan 20 at 10:53





No. But why don't you write a filter that does all this, including calling has_perm?

– Daniel Roseman
Jan 20 at 10:53













in_department and is_allowed are filters or not ? Do you have a link with examples?

– cwhisperer
Jan 20 at 11:04





in_department and is_allowed are filters or not ? Do you have a link with examples?

– cwhisperer
Jan 20 at 11:04












0






active

oldest

votes











Your Answer






StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");

StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});

function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});


}
});














draft saved

draft discarded


















StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275217%2fdjango-use-template-tag-in-has-perm%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown

























0






active

oldest

votes








0






active

oldest

votes









active

oldest

votes






active

oldest

votes
















draft saved

draft discarded




















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid



  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.


To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f54275217%2fdjango-use-template-tag-in-has-perm%23new-answer', 'question_page');
}
);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Liquibase includeAll doesn't find base path

How to use setInterval in EJS file?

Petrus Granier-Deferre