-
InfluxDB
Power Real-Time Data Analytics at Scale. Get real-time insights from all types of time series data with InfluxDB. Ingest, query, and analyze billions of data points in real-time with unbounded cardinality.
Flask I have a template that is using flask-login and I have a permission model built on bitsets. In my template, I want to expose content in a page based on permissions. I have a function can(permission) in my user model that controls what a user can do. class User(UserMixin, db.Model): __tablename__ = 'users' id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(64), unique=True) fullname = db.Column(db.String(75)) password_hash = db.Column(db.String(128)) role_id = db.Column(db.Integer, db.ForeignKey('roles.id')) @property def password(self): raise AttributeError('password is not a readable attribute') @password.setter def password(self, password): self.password_hash = generate_password_hash(password) def verify\_password(self, password): return check_password_hash(self.password_hash, password) def can(self, permissions): return self.role is not None and (self.role.permissions & permissions) == permissions def __repr__(self): return ''.format(self.email)class Permission: FOLLOW = 0x01 COMMENT = 0x02 WRITE_ARTICLES = 0x04 MODERATE_COMMENTS = 0x08 ADMINISTER = 0x80 When I try to call the can() function in my template and pass it a permission, I get an error that Permission is not defined, presumably because the Permission class is not in scope for the template. Here is part of my base.html that all other templates extend: {% if current_user.can(Permission.ADMINISTER) %}Administration Add User Change Password Lock User{% endif %} I'm loosely following Miguel Grinberg's book on Flask Web Development and he is doing the same thing in his base.html (line 32). How can I make the Permission class available to my template? Answer link : https://codehunter.cc/a/flask/how-can-i-make-a-class-variable-available-to-jinja2-templates-with-flask