You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

41 lines
1.2 KiB

  1. from werkzeug.security import generate_password_hash, check_password_hash
  2. from flask_login import UserMixin
  3. from . import db, login_manager
  4. class Role(db.Model):
  5. __tablename__ = 'roles'
  6. id = db.Column(db.Integer, primary_key=True)
  7. name = db.Column(db.String(64), unique=True)
  8. users = db.relationship('User', backref='role', lazy='dynamic')
  9. def __repr__(self):
  10. return '<Role %r>' % self.name
  11. class User(UserMixin, db.Model):
  12. __tablename__ = 'users'
  13. id = db.Column(db.Integer, primary_key=True)
  14. email = db.Column(db.String(64), unique=True, index=True)
  15. username = db.Column(db.String(64), unique=True, index=True)
  16. role_id = db.Column(db.Integer, db.ForeignKey('roles.id'))
  17. password_hash = db.Column(db.String(128))
  18. @property
  19. def password(self):
  20. raise AttributeError('Password is not a readable attribute')
  21. @password.setter
  22. def password(self, password):
  23. self.password_hash = generate_password_hash(password)
  24. def verify_password(self, password):
  25. return check_password_hash(self.password_hash, password)
  26. def __repr__(self):
  27. return '<User %r>' % self.username
  28. @login_manager.user_loader
  29. def load_user(user_id):
  30. return User.query.get(int(user_id))