I've built a system that allows users to apply for code review and wait for manager to approve.
And now what I want to achieve is as below:
- If it's approved, then all the fields become read-only(I manually set Project name as read-only here):
If it's rejected,
then all the fields become editable. Of course, when creating a new project, all the fields should be editable.
The code of class
Project
andProjectView
are as below:from flask_sqlalchemy import SQLAlchemy from flask_admin.contrib import sqla from flask_security import current_user # Create Flask application app = Flask(__name__) app.config.from_pyfile('config.py') db = SQLAlchemy(app) class Project(db.Model): id = db.Column(db.Integer, primary_key=True) project_name = db.Column(db.Unicode(128)) version = db.Column(db.Unicode(128)) SVN = db.Column(db.UnicodeText) approve = db.Column(db.Boolean()) def __unicode__(self): return self.name class ProjectView(sqla.ModelView): def is_accessible(self): if not current_user.is_active or not current_user.is_authenticated: return False return False @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.Field('project_name'), rules.Field('version'), rules.Field('SVN'), ] if not has_app_context() or current_user.has_role('superuser'): form_rules.append('approve')
In my opinion, since approve is a boolean variable, there should be a condition judgement to tell if it is 0 or 1 and then the field become read-only or editable accordingly.
Thanks for any advise in advance.