Giter VIP home page Giter VIP logo

Comments (3)

fjsj avatar fjsj commented on August 30, 2024

I'm not sure if I understand the issue correctly. Could you please share the relevant parts of you roles.py and what behavior you expect?

Isn't this a case where your new CustomUser model doesn't have the right permissions, as the old one?

Check https://docs.djangoproject.com/en/3.2/topics/auth/default/#default-permissions

from django-role-permissions.

FeralRobot avatar FeralRobot commented on August 30, 2024

To answer your second question first, yes, my new CustomUser model doesn't have the right permissions. The issue is I cannot create the new permissions that I need using django-role-permissions.

Here is my original / current roles.py:

class Admin(AbstractUserRole):
    available_permissions = {
        'run_task': True,
        'run_scheduled_task': True,
        'view_cred': True,
        'create_cred': True,
        'delete_cred': True,
        'create_user': True,
        'delete_user': True,
        'view_user': True,
        'change_user': True,
        'add_user': True,
    }

This creates permissions named Add User, Change User, View User, Delete User, and this works fine with the default django user model. These permission names are created as Title Case by django-role-permissions, as the documentation describes.

Now I move the user model to CustomUser. The symptom appears, which is I can no longer see the users when I log into the django admin as "Admin" role. I use the template permission tags, with has_role in the template logic. We can come back to the template discussion, but I don't believe this is the heart of the issue.

With the CustomUser model implemented, I now log into the django admin as superuser. I now see eight permissions for the superuser associated with users: Add User, Change User, View User, Delete User (the four original ones), and four new ones Can add user, Can change user, Can view user, Can delete user. Notice the case of these last four permissions, it's like a sentence with just the first letter in the first word capitalized. In the django admin, I add the Can view user permission to my "Admin" user. Now I login again to the django admin as the "Admin" user. I can now view the users. Great! I think, I just need to create these new permissions with django-role-permissions.

I go to the roles file above and add:
'Can_view_user':True,

The permission django-role-permissions then creates is Can View User (note the Title Case) which does not match the case of Can view user. I login to the django admin after this change to the roles file, and I am still unable to view the users in the django admin.

Desired behavior from django-role-permission:

snake case in ===> Title Case out (no change, this is the current default behavior)
"sentence case" in ===> Sentence case out (not Title Case)

example:

'view_user':True, in roles.py ===> View User is the permission name created
'Can_view_user': True in roles.py ===> Can view user is the permission name created

from django-role-permissions.

FeralRobot avatar FeralRobot commented on August 30, 2024

Something like this in rolepermissions/utils.py

import re


def Sentence_case(s):
    return s[0].isupper() and sum([int(x[0].isupper()) for x in s.split('_')]) == 1


def camelToSnake(s):
    """
    https://gist.github.com/jaytaylor/3660565
    Is it ironic that this function is written in camel case, yet it
    converts to snake case? hmm..
    """
    if Sentence_case(s):
        return s
    else:
        _underscorer1 = re.compile(r'(.)([A-Z][a-z]+)')
        _underscorer2 = re.compile('([a-z0-9])([A-Z])')

        subbed = _underscorer1.sub(r'\1_\2', s)
        return _underscorer2.sub(r'\1_\2', subbed).lower()


def snake_to_title(s):
    if Sentence_case(s):
        return ' '.join(s.split('_'))
    else:
        return ' '.join(x.capitalize() for x in s.split('_'))


def camel_or_snake_to_title(s):
    return snake_to_title(camelToSnake(s))


tests = ['camelCase', 'snake_case', 'Can_add_user']
for t in tests:
    print(camel_or_snake_to_title(t))

from django-role-permissions.

Related Issues (20)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.