models.py 7.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268
  1. """
  2. All models for module
  3. """
  4. from hashlib import blake2b
  5. import hmac
  6. from urllib.parse import quote
  7. from datetime import datetime, timedelta
  8. # from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property
  9. import markdown
  10. from flask import Markup
  11. from flask_login import UserMixin
  12. from app import db, argon2, login_manager
  13. ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
  14. IMAGE_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
  15. class User(db.Model, UserMixin):
  16. """Model for User"""
  17. id = db.Column(db.Integer, primary_key=True)
  18. name = db.Column(db.String, unique=True, nullable=False)
  19. email = db.Column(db.String(255), unique=True)
  20. discord = db.Column(db.String(255), unique=True)
  21. game_id = db.Column(db.BigInteger, unique=True)
  22. alt = db.Column(db.Boolean, server_default='f', default=False)
  23. party_member = db.Column(db.Boolean, server_default='f', default=False)
  24. _password = db.Column("password", db.String(255))
  25. registration_at = db.Column(db.DateTime, default=datetime.utcnow)
  26. approved = db.Column(db.Boolean, server_default='f', default=False)
  27. def __init__(self, id=None):
  28. self.id = id
  29. @login_manager.user_loader
  30. def load_user(user_id):
  31. return User.query.get(user_id)
  32. @property
  33. def password(self):
  34. """Return the password"""
  35. return self._password
  36. @password.setter
  37. def password(self, password):
  38. """Hash password"""
  39. self._password = argon2.generate_password_hash(password)
  40. def check_password(self, password):
  41. """Check if password is correct"""
  42. return argon2.check_password_hash(self.password, password)
  43. question_id = db.Column(
  44. db.Integer,
  45. db.ForeignKey("question.id")
  46. )
  47. user = db.relationship(
  48. "Question",
  49. backref=db.backref("users", lazy="dynamic")
  50. )
  51. class Page(db.Model):
  52. """Model for Page"""
  53. id = db.Column(db.Integer, primary_key=True)
  54. title = db.Column(db.String, nullable=False)
  55. datetime = db.Column(db.DateTime, default=datetime.utcnow)
  56. source = db.Column(db.String)
  57. private = db.Column(db.Boolean, server_default='f', default=False)
  58. def content(self):
  59. """Render page source"""
  60. return Markup(markdown.markdown(self.source, extensions=['tables', 'toc', 'meta']))
  61. def url(self):
  62. """Generate URL for page"""
  63. return quote(self.title.strip().lower().replace(" ", "_"))
  64. def path(self):
  65. """Generate path with parents"""
  66. if self.parent_id:
  67. return '%s/%s' % (self.parent.path(), self.url())
  68. return self.url()
  69. user_id = db.Column(
  70. db.Integer,
  71. db.ForeignKey("user.id")
  72. )
  73. user = db.relationship(
  74. "User",
  75. backref=db.backref("Pages", lazy="dynamic")
  76. )
  77. parent_id = db.Column(
  78. db.Integer,
  79. db.ForeignKey("page.id")
  80. )
  81. parent = db.relationship(
  82. "Page",
  83. backref=db.backref("children", lazy="dynamic"),
  84. uselist=False,
  85. remote_side=id
  86. )
  87. class File(db.Model):
  88. """Model for Page"""
  89. id = db.Column(db.Integer, primary_key=True)
  90. title = db.Column(db.String, nullable=False)
  91. datetime = db.Column(db.DateTime, default=datetime.utcnow)
  92. path = db.Column(db.String, nullable=False)
  93. identifier = db.Column(db.String)
  94. private = db.Column(db.Boolean, server_default='f', default=False)
  95. def url(self):
  96. """Generate URL for page"""
  97. return quote(self.title.strip().lower().replace(" ", "_"))
  98. def extension(self):
  99. """Return file extension"""
  100. return '.' in self.path and self.path.rsplit('.', 1)[1].lower()
  101. def is_image(self):
  102. """True if file is image"""
  103. return self.extension() in IMAGE_EXTENSIONS
  104. user_id = db.Column(
  105. db.Integer,
  106. db.ForeignKey("user.id")
  107. )
  108. user = db.relationship(
  109. "User",
  110. backref=db.backref("files", lazy="dynamic")
  111. )
  112. class Ballot(db.Model):
  113. """Model for Ballot"""
  114. id = db.Column(db.Integer, primary_key=True)
  115. name = db.Column(db.String, nullable=False)
  116. description = db.Column(db.String)
  117. start_at = db.Column(db.DateTime, default=datetime.utcnow)
  118. end_at = db.Column(db.DateTime, nullable=False)
  119. user_id = db.Column(
  120. db.Integer,
  121. db.ForeignKey("user.id")
  122. )
  123. user = db.relationship(
  124. "User",
  125. backref=db.backref("options", lazy="dynamic")
  126. )
  127. priority_id = db.Column(
  128. db.Integer,
  129. db.ForeignKey("priority.id")
  130. )
  131. priority = db.relationship(
  132. "Priority",
  133. backref=db.backref("ballots", lazy="dynamic")
  134. )
  135. def active(self):
  136. """Check if vote is active"""
  137. today = datetime.today()
  138. if self.start_at <= today <= self.end_at:
  139. return True
  140. #return False
  141. return True
  142. class Priority(db.Model):
  143. """Model for Priority"""
  144. id = db.Column(db.Integer, primary_key=True)
  145. name = db.Column(db.String, nullable=False)
  146. description = db.Column(db.String, nullable=False)
  147. class Question(db.Model):
  148. """Model for Question"""
  149. id = db.Column(db.Integer, primary_key=True)
  150. name = db.Column(db.String, nullable=False)
  151. description = db.Column(db.String)
  152. combined_approval_voting = db.Column(db.Boolean, server_default='f', default=False)
  153. ballot_id = db.Column(
  154. db.Integer,
  155. db.ForeignKey("ballot.id")
  156. )
  157. ballot = db.relationship(
  158. "Ballot",
  159. backref=db.backref("questions", lazy="dynamic")
  160. )
  161. class Option(db.Model):
  162. """Model for Option"""
  163. id = db.Column(db.Integer, primary_key=True)
  164. name = db.Column(db.String)
  165. description = db.Column(db.String)
  166. user_id = db.Column(
  167. db.Integer,
  168. db.ForeignKey("user.id")
  169. )
  170. user = db.relationship(
  171. "User",
  172. backref=db.backref("eligible", lazy="dynamic")
  173. )
  174. question_id = db.Column(
  175. db.Integer,
  176. db.ForeignKey("question.id")
  177. )
  178. question = db.relationship(
  179. "Question",
  180. backref=db.backref("options", lazy="dynamic")
  181. )
  182. class Vote(db.Model):
  183. """Model for Vote"""
  184. id = db.Column(db.Integer, primary_key=True)
  185. datetime = db.Column(db.DateTime, default=datetime.utcnow)
  186. option_id = db.Column(
  187. db.Integer,
  188. db.ForeignKey("option.id")
  189. )
  190. option = db.relationship(
  191. "Option",
  192. backref=db.backref("votes", lazy="dynamic")
  193. )
  194. user_id = db.Column(
  195. db.Integer,
  196. db.ForeignKey("user.id")
  197. )
  198. user = db.relationship(
  199. "User",
  200. backref=db.backref("votes", lazy="dynamic")
  201. )
  202. class Code(db.Model):
  203. """Model for code"""
  204. id = db.Column(db.Integer, primary_key=True)
  205. expire_date = db.Column(db.DateTime, default=datetime.utcnow() + timedelta(days=20))
  206. secret = db.Column(db.String(255), unique=True)
  207. def get_digest(self, string):
  208. """Generate digest on string"""
  209. if isinstance(string, int):
  210. string = str(string)
  211. string = string.encode('utf-8')
  212. secret = self.secret.encode('utf-8')
  213. blake = blake2b(digest_size=3)
  214. blake.update(secret + string)
  215. return blake.hexdigest()