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

AttributeError: 'str' object has no attribute 'roles' #9

Open
leoGalani opened this issue Jul 15, 2016 · 3 comments
Open

AttributeError: 'str' object has no attribute 'roles' #9

leoGalani opened this issue Jul 15, 2016 · 3 comments

Comments

@leoGalani
Copy link

leoGalani commented Jul 15, 2016

Hi, i'm trying to follow the steps but i'm getting a error when i try to add roles to user (UserMIxin instance) and i'm not sure how to put all together..

my_user = UserMixin()
my_user.add_roles('admin', 'superadmin')
db.session.add(my_user)
db.session.commit()

But when i create a normal user, i usually do

my_user = User(name="xxx", email="xxxxdddd@dd.com", pass="1234")
db.session.add(my_user)
db.session.commit()

my model is like this:

class User(UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(50), nullable=False)
    email = db.Column(db.String(50), nullable=False, unique=True)
    password = db.Column(db.String(50), nullable=False)

def __init__(self, name, email, password, roles=None):
    """Setting params to the object."""
    self.name = name
    self.email = email.lower()
    self.password = bcrypt.hashpw(
        password.encode('utf-8'), bcrypt.gensalt())
    UserMixin.__init__(self, roles)

.... other methods ...

any tips on how to do i right? :)
(im using @bepetersn fork)

@bepetersn
Copy link
Contributor

bepetersn commented Jul 30, 2016

Hi Leo, I finally did get a chance to look at this, and I had trouble producing your exact error. I saw some others, which we can talk about & I might know what's causing them (mentioned below). However, the good news is I have a completely functional example using your above User model code at https://github.com/bepetersn/flask-permissions-ex.

It's been a little while since I looked at this stuff, but the biggest change with my fork (re: the above-mentioned errors) is you shouldn't instantiate UserMixin now -- it's an abstract model base. So you want to use a User model like yours above. And that can cause some errors -- I got a different one from yours about not being able to set roles.

Let me know what you think.

@leoGalani
Copy link
Author

Hi Brian,
My main problem is when i create roles and then try add it to a user.. it appears the problem :)

from app import db
from flask_permissions.models import UserMixin, Role

db.create_all()
superadmin.add_abilities(
    'create_admin', 'edit_admin_user', 'delete_admin_user')
db.session.add(superadmin)
db.session.commit()
#....
first_user = UserMixin

first_user.add_roles('superadmin')  ###triggers the error
db.session.add(first_user)
db.session.commit()

@bepetersn
Copy link
Contributor

I assume with this line...
first_user = UserMixin
you mean this...
first_user = UserMixin()
but you really need to do this...
first_user = User()
(where User is a UserMixin subclass of your definition).

You need to create a user instance with your User model, which should be a subclass of UserMixin. You can't do it with UserMixin directly anymore. I'm considering this a documentation failure, so I'm presently updating the README to show the right way to actually create a user with the new code. I'm going to fix the tests with this too, so the PR will finally be ready!

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