models.py 1.9 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. """Models"""
  2. from datetime import datetime
  3. from sqlalchemy.ext.hybrid import hybrid_property, hybrid_method
  4. from app import db, login_manager, argon2
  5. from flask_login import UserMixin
  6. class User(db.Model, UserMixin):
  7. """Model for User"""
  8. id = db.Column(db.Integer, primary_key=True)
  9. email = db.Column(db.String)
  10. _password = db.Column("password", db.String)
  11. name = db.Column(db.String)
  12. registration_at = db.Column(db.DateTime, default=datetime.utcnow)
  13. @login_manager.user_loader
  14. def load_user(user_id):
  15. return User.query.get(user_id)
  16. @property
  17. def password(self):
  18. """Return the password"""
  19. return self._password
  20. @password.setter
  21. def password(self, password):
  22. """Hash password"""
  23. self._password = argon2.generate_password_hash(password)
  24. def check_password(self, password):
  25. """Check if password is correct"""
  26. return argon2.check_password_hash(self.password, password)
  27. class Key(db.Model):
  28. """Model for Key"""
  29. id = db.Column(db.Integer, primary_key=True)
  30. key = db.Column(db.String, nullable=True)
  31. created_at = db.Column(db.DateTime, default=datetime.utcnow)
  32. expire_at = db.Column(db.DateTime)
  33. active = db.Column(db.Boolean, default=False)
  34. used_at = db.Column(db.DateTime)
  35. uses = db.Column(db.Integer, default=0)
  36. user_id = db.Column(
  37. db.Integer,
  38. db.ForeignKey('user.id')
  39. )
  40. user = db.relationship(
  41. "User",
  42. backref=db.backref("keys", lazy="dynamic")
  43. )
  44. class Log(db.Model):
  45. """Model for Log"""
  46. id = db.Column(db.Integer, primary_key=True)
  47. succes = db.Column(db.Boolean, default=False)
  48. request_type = db.Column(db.String)
  49. request_url = db.Column(db.String)
  50. date_time = db.Column(db.DateTime)
  51. key_id = db.Column(
  52. db.Integer,
  53. db.ForeignKey('key.id')
  54. )
  55. key = db.relationship(
  56. "Key",
  57. backref=db.backref("logs", lazy="dynamic")
  58. )