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

In views.py, I want users who belong to a specific group to be True. #135

Open
tsuruchoff opened this issue Jan 30, 2021 · 0 comments
Open

Comments

@tsuruchoff
Copy link

tsuruchoff commented Jan 30, 2021

Hello! This is my first question.
I don't usually use English very much, so if something doesn't make sense to you, please ask for more details.

I'm currently trying to use django-rules to determine if a user belongs to a certain group or not.
but the superuser is successfully excluded in html, but in views.py, it is always True.

What is wrong with this?

#rules.py

import rules
from rules import has_perm


is_admin = rules.is_group_member( 'is_admin' )
is_aaa = rules.is_group_member( 'aaa' )

@rules.predicate
def is_aaa_group(user):
    return user.has_perm( 'is_aaa' )

# Permissions
rules.add_perm( 'is_admin', is_admin )
rules.add_perm( 'is_aaa', is_aaa )
# views.py

class BookListView( ListView ):

    model = Book
    template_name = 'index.html'

    def get( self, request, *args, **kwargs ):
        user = self.request.user

        print( is_aaa_group(user) )

        return super().get( request, *args, **kwargs )

# superuser
Reality -> Trueorz
Ideal -> False
# aaa group
Reality -> True
Ideal -> True

In html, it works as expected. ↓

{% extends "base.html" %}
{% load static %}
{% load rules %}
{% has_perm 'is_admin' request.user as is_admin %}
{% has_perm 'is_aaa' request.user as is_aaa %}

{% block content %}


  {% if is_admin %}j

    <li>only superuser.</li>

  {% elif is_aaa %}

    <li>only aaa group.</li>

  {% endif %}


{% endblock %}

thank you.

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

1 participant