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.
AttributeError: 'NoneType' object has no attribute 'has_role'
after revising the code. And if I comment@property
, then it showsTypeError: 'instancemethod' object is not iterable
. – Choric