Tri
Tri

Reputation: 3039

Flask Admin edit able user change their data

I'm using Flask-Admin to manage my CRUD. There are three roles in my app, which is superuser, operator and client.

In this app, operators must ask superuser to register their account, to change their data and others.

But for the client which is uncounted numbers, I want they can register their account or editable their account information by own.

For now, the client has can register by own, but now I want the client can editable their information individually without through superuser.

So far, I just can edit the account information by superuser, like this screenshot: enter image description here

So for now, I want client can edit their name, email, password or other information by their own, but also separate their data with the other clients.

Here is the snippet of my model:

roles_users = db.Table(
    'roles_users',
    db.Column('user_id', db.Integer(), db.ForeignKey('user.id')),
    db.Column('role_id', db.Integer(), db.ForeignKey('role.id'))
)

class Role(db.Model, RoleMixin):
    id = db.Column(db.Integer(), primary_key=True)
    name = db.Column(db.String(80), unique=True)

class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    email = db.Column(db.String(255), unique=True)
    password = db.Column(db.String(255))
    roles = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))

class Operator(User):
    __tablename__ = 'operator'
    id = db.Column(db.Integer(), primary_key=True)
    user_id = db.Column(db.Integer(), db.ForeignKey('user.id'))


class Client(User):
    __tablename__ = 'client'
    id = db.Column(db.Integer(), primary_key=True)
    user_id = db.Column(db.Integer(), db.ForeignKey('user.id'))

So, how to do that with Flask-Admin..?

Upvotes: 1

Views: 1907

Answers (1)

gittert
gittert

Reputation: 1308

Flask-Security comes with a built-in form and view for password change. I would recommend using that. https://pythonhosted.org/Flask-Security/customizing.html

to edit user info via Flask-Admin view, you can override these methods by doing the following. Don't forget to add 'client' as accepted role in your flask-admin User class. The custom filter has to filter on current_user_id, so no other user profile can be editted.

def get_query(self) 
    if "superuser" in current_user.roles:
        return self.session.query(self.model)  # as original source code

    else:  # for all other roles
        return self.session.query(self.model).filter(
            < insert custom filter here> )



def get_count_query(self): 
    if "superuser" in current_user.roles:
        return self.session.query(func.count('*')).select_from(self.model) # as original source code

    else: # for all other roles
        return self.session.query(func.count('*')).filter(
            <insert custom filter here>  )

An alternative solution would be so build a custom view (without using flask-admin) and call it /myprofile.

Upvotes: 1

Related Questions