I've built an app with a table called Project which is stored in sqlite, I want allow only super users can view the approve column when creating, editing data.
The Project data is retrieved in the "class Project", and I've added
if current_user.has_role('superuser')
in "class ProjectView":
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_security import SQLAlchemyUserDatastore, current_user,UserMixin,
RoleMixin
from flask_admin.contrib import sqla
# Create Flask application
app = Flask(__name__)
app.config.from_pyfile('config.py')
db = SQLAlchemy(app)
# Define models
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)
description = db.Column(db.String(255))
def __str__(self):
return self.name
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'))
def __str__(self):
return self.email
class Project(db.Model):
id = db.Column(db.Integer, primary_key=True)
team = db.Column(db.Unicode(64))
project_name = db.Column(db.Unicode(128))
approve = db.Column(db.Boolean())
# Setup Flask-Security
user_datastore = SQLAlchemyUserDatastore(db, User, Role)
security = Security(app, user_datastore)
# Create customized model view class
class MyModelView(sqla.ModelView):
def is_accessible(self):
if not current_user.is_active or not current_user.is_authenticated:
return False
if current_user.has_role('superuser'):
return True
return False
def _handle_view(self, name, **kwargs):
if not self.is_accessible():
if current_user.is_authenticated:
# permission denied
abort(403)
else:
# login
return redirect(url_for('security.login', next=request.url))
class ProjectView(sqla.ModelView):
def is_accessible(self):
if not current_user.is_active:
return False
else:
return True
def _handle_view(self, name, **kwargs):
if not self.is_accessible():
if current_user.is_authenticated:
# permission denied
abort(403)
else:
if current_user.has_role('superuser'):
form_create_rules = [
rules.FieldSet(('team'), 'Personal Info'),
rules.Header('Project Info'),
rules.Field('project_name'),
'approve',
rules.Container('rule_demo.wrap', rules.Field('notes'))
]
else:
form_create_rules = [
rules.FieldSet(('team'), 'Personal Info'),
rules.Header('Project Info'),
rules.Field('project_name'),
#'approve',
rules.Container('rule_demo.wrap', rules.Field('notes'))
]
form_edit_rules = form_create_rules
create_template = 'rule_create.html'
edit_template = 'rule_edit.html'
@app.route('/')
def index():
return render_template('index.html')
# Create admin
admin = flask_admin.Admin(
app,
'Release Control System',
# log in success page
base_template='my_master.html',
template_mode='bootstrap3',
)
# Add model views
admin.add_view(MyModelView(Role, db.session))
admin.add_view(MyModelView(User, db.session))
admin.add_view(ProjectView(Project, db.session))
But it still not works and all users still can view the approve column. Kindly advise. Thanks in advance.
You can use BaseModelView.column_list attribute to specify dynamically calculated list of accessible columns, just make it a property. However different "field" attributes of ModelView
are cached on application launch so you need to override their caches:
from flask import has_app_context
class ProjectView(sqla.ModelView):
@property
def _list_columns(self):
return self.get_list_columns()
@_list_columns.setter
def _list_columns(self, value):
pass
@property
def column_list(self):
if not has_app_context() or current_user.has_role('superuser'):
return ['team', 'project_name', 'approve']
else:
return ['team', 'project_name']
column_list
attribute is used during application initialisation when current_user
is not available. Use flask.has_app_context() method to check this state and pass application a full list of columns on launch.
If you need to specify different set of columns for editing you need form_rules
attributes (you already used them in your question):
from flask_admin.form import rules
class ProjectView(sqla.ModelView):
@property
def _form_edit_rules(self):
return rules.RuleSet(self, self.form_rules)
@_form_edit_rules.setter
def _form_edit_rules(self, value):
pass
@property
def _form_create_rules(self):
return rules.RuleSet(self, self.form_rules)
@_form_create_rules.setter
def _form_create_rules(self, value):
pass
@property
def form_rules(self):
form_rules = [
rules.FieldSet(('team',), 'Personal Info'),
rules.Header('Project Info'),
rules.Field('project_name')
]
if not has_app_context() or current_user.has_role('superuser'):
form_rules.append('approve')
form_rules.append(rules.Container('rule_demo.wrap', rules.Field('notes')))
return form_rules
Also you do not need to use _handle_view
to redirect user to login page. For this purpose BaseView.inaccessible_callback method is used:
def inaccessible_callback(self, name, **kwargs):
if current_user.is_authenticated:
abort(403)
else:
return redirect(url_for('security.login', next=request.url))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With