models.py 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. """
  2. All models for module
  3. """
  4. from urllib.parse import quote
  5. from datetime import datetime
  6. # from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property
  7. import markdown
  8. from flask import Markup
  9. from flask_login import UserMixin
  10. from app import db, argon2, login_manager
  11. ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
  12. IMAGE_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
  13. page_file = db.Table(
  14. 'page_file',
  15. db.Column(
  16. 'page_id',
  17. db.Integer,
  18. db.ForeignKey('page.id'),
  19. primary_key=True
  20. ),
  21. db.Column(
  22. 'file_id',
  23. db.Integer,
  24. db.ForeignKey('file.id'),
  25. primary_key=True
  26. ),
  27. )
  28. class User(db.Model, UserMixin):
  29. """Model for User"""
  30. id = db.Column(db.Integer, primary_key=True)
  31. name = db.Column(db.String, unique=True, nullable=False)
  32. email = db.Column(db.String(255), unique=True)
  33. _password = db.Column("password", db.String(255))
  34. registration_at = db.Column(db.DateTime, default=datetime.utcnow)
  35. approved = db.Column(db.Boolean, server_default='f', default=False)
  36. def __init__(self, id=None):
  37. self.id = id
  38. @login_manager.user_loader
  39. def load_user(user_id):
  40. return User.query.get(user_id)
  41. @property
  42. def password(self):
  43. """Return the password"""
  44. return self._password
  45. @password.setter
  46. def password(self, password):
  47. """Hash password"""
  48. self._password = argon2.generate_password_hash(password)
  49. def check_password(self, password):
  50. """Check if password is correct"""
  51. return argon2.check_password_hash(self.password, password)
  52. # function_id = db.Column(
  53. # db.Integer,
  54. # db.ForeignKey("function.id")
  55. # )
  56. # user = db.relationship(
  57. # "Function",
  58. # backref=db.backref("users", lazy="dynamic")
  59. # )
  60. class Page(db.Model):
  61. """Model for Page"""
  62. id = db.Column(db.Integer, primary_key=True)
  63. title = db.Column(db.String, nullable=False)
  64. datetime = db.Column(db.DateTime, default=datetime.utcnow)
  65. source = db.Column(db.String)
  66. private = db.Column(db.Boolean, server_default='f', default=False)
  67. def content(self):
  68. """Render page source"""
  69. return Markup(markdown.markdown(self.source, extensions=['tables', 'toc', 'meta']))
  70. def url(self):
  71. """Generate URL for page"""
  72. return quote(self.title.strip().lower().replace(" ", "_"))
  73. def path(self):
  74. """Generate path with parents"""
  75. if self.parent_id:
  76. return '%s/%s' % (self.parent.path(), self.url())
  77. return self.url()
  78. user_id = db.Column(
  79. db.Integer,
  80. db.ForeignKey("user.id")
  81. )
  82. user = db.relationship(
  83. "User",
  84. backref=db.backref("Pages", lazy="dynamic")
  85. )
  86. parent_id = db.Column(
  87. db.Integer,
  88. db.ForeignKey("page.id")
  89. )
  90. parent = db.relationship(
  91. "Page",
  92. backref=db.backref("children", lazy="dynamic"),
  93. uselist=False,
  94. remote_side=id
  95. )
  96. class File(db.Model):
  97. """Model for Page"""
  98. id = db.Column(db.Integer, primary_key=True)
  99. title = db.Column(db.String, nullable=False)
  100. datetime = db.Column(db.DateTime, default=datetime.utcnow)
  101. path = db.Column(db.String, nullable=False)
  102. identifier = db.Column(db.String)
  103. private = db.Column(db.Boolean, server_default='f', default=False)
  104. def url(self):
  105. """Generate URL for page"""
  106. return quote(self.title.strip().lower().replace(" ", "_"))
  107. def extension(self):
  108. """Return file extension"""
  109. return '.' in self.path and self.path.rsplit('.', 1)[1].lower()
  110. def is_image(self):
  111. """True if file is image"""
  112. return self.extension() in IMAGE_EXTENSIONS
  113. user_id = db.Column(
  114. db.Integer,
  115. db.ForeignKey("user.id")
  116. )
  117. user = db.relationship(
  118. "User",
  119. backref=db.backref("files", lazy="dynamic")
  120. )
  121. files = db.relationship(
  122. 'Page',
  123. secondary=page_file,
  124. lazy='subquery',
  125. backref=db.backref('pages', lazy=True)
  126. )