models.py 8.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  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. discord_id = db.Column(db.BigInteger, unique=True)
  22. game_id = db.Column(db.BigInteger, unique=True)
  23. alt = db.Column(db.Boolean, server_default='f', default=False)
  24. party_member = db.Column(db.Boolean, server_default='f', default=False)
  25. _password = db.Column("password", db.String(255))
  26. registration_at = db.Column(db.DateTime, default=datetime.utcnow)
  27. approved = db.Column(db.Boolean, server_default='f', default=False)
  28. def __init__(self, id=None):
  29. self.id = id
  30. @login_manager.user_loader
  31. def load_user(user_id):
  32. return User.query.get(user_id)
  33. @property
  34. def password(self):
  35. """Return the password"""
  36. return self._password
  37. @password.setter
  38. def password(self, password):
  39. """Hash password"""
  40. self._password = argon2.generate_password_hash(password)
  41. def check_password(self, password):
  42. """Check if password is correct"""
  43. return argon2.check_password_hash(self.password, password)
  44. def game_url(self):
  45. """Give profile url"""
  46. if self.game_id:
  47. return "https://rivalregions.com/#slide/profile/%d" % self.game_id
  48. return
  49. question_id = db.Column(
  50. db.Integer,
  51. db.ForeignKey("question.id")
  52. )
  53. user = db.relationship(
  54. "Question",
  55. backref=db.backref("users", lazy="dynamic")
  56. )
  57. class Page(db.Model):
  58. """Model for Page"""
  59. id = db.Column(db.Integer, primary_key=True)
  60. title = db.Column(db.String, nullable=False)
  61. datetime = db.Column(db.DateTime, default=datetime.utcnow)
  62. source = db.Column(db.String)
  63. private = db.Column(db.Boolean, server_default='f', default=False)
  64. def content(self):
  65. """Render page source"""
  66. return Markup(markdown.markdown(self.source, extensions=['tables', 'toc', 'meta']))
  67. def url(self):
  68. """Generate URL for page"""
  69. return quote(self.title.strip().lower().replace(" ", "_"))
  70. def path(self):
  71. """Generate path with parents"""
  72. if self.parent_id:
  73. return '%s/%s' % (self.parent.path(), self.url())
  74. return self.url()
  75. user_id = db.Column(
  76. db.Integer,
  77. db.ForeignKey("user.id")
  78. )
  79. user = db.relationship(
  80. "User",
  81. backref=db.backref("Pages", lazy="dynamic")
  82. )
  83. parent_id = db.Column(
  84. db.Integer,
  85. db.ForeignKey("page.id")
  86. )
  87. parent = db.relationship(
  88. "Page",
  89. backref=db.backref("children", lazy="dynamic"),
  90. uselist=False,
  91. remote_side=id
  92. )
  93. class File(db.Model):
  94. """Model for Page"""
  95. id = db.Column(db.Integer, primary_key=True)
  96. title = db.Column(db.String, nullable=False)
  97. datetime = db.Column(db.DateTime, default=datetime.utcnow)
  98. path = db.Column(db.String, nullable=False)
  99. identifier = db.Column(db.String)
  100. private = db.Column(db.Boolean, server_default='f', default=False)
  101. def url(self):
  102. """Generate URL for page"""
  103. return quote(self.title.strip().lower().replace(" ", "_"))
  104. def extension(self):
  105. """Return file extension"""
  106. return '.' in self.path and self.path.rsplit('.', 1)[1].lower()
  107. def is_image(self):
  108. """True if file is image"""
  109. return self.extension() in IMAGE_EXTENSIONS
  110. user_id = db.Column(
  111. db.Integer,
  112. db.ForeignKey("user.id")
  113. )
  114. user = db.relationship(
  115. "User",
  116. backref=db.backref("files", lazy="dynamic")
  117. )
  118. class Ballot(db.Model):
  119. """Model for Ballot"""
  120. id = db.Column(db.Integer, primary_key=True)
  121. name = db.Column(db.String, nullable=False)
  122. description = db.Column(db.String)
  123. start_at = db.Column(db.DateTime, default=datetime.utcnow)
  124. end_at = db.Column(db.DateTime, nullable=False)
  125. user_id = db.Column(
  126. db.Integer,
  127. db.ForeignKey("user.id")
  128. )
  129. user = db.relationship(
  130. "User",
  131. backref=db.backref("options", lazy="dynamic")
  132. )
  133. priority_id = db.Column(
  134. db.Integer,
  135. db.ForeignKey("priority.id")
  136. )
  137. priority = db.relationship(
  138. "Priority",
  139. backref=db.backref("ballots", lazy="dynamic")
  140. )
  141. def active(self):
  142. """Check if vote is active"""
  143. today = datetime.today()
  144. if self.start_at <= today <= self.end_at:
  145. return True
  146. return False
  147. class Priority(db.Model):
  148. """Model for Priority"""
  149. id = db.Column(db.Integer, primary_key=True)
  150. name = db.Column(db.String, nullable=False)
  151. description = db.Column(db.String, nullable=False)
  152. class Question(db.Model):
  153. """Model for Question"""
  154. id = db.Column(db.Integer, primary_key=True)
  155. name = db.Column(db.String, nullable=False)
  156. description = db.Column(db.String)
  157. combined_approval_voting = db.Column(db.Boolean, server_default='f', default=False)
  158. ballot_id = db.Column(
  159. db.Integer,
  160. db.ForeignKey("ballot.id")
  161. )
  162. ballot = db.relationship(
  163. "Ballot",
  164. backref=db.backref("questions", lazy="dynamic")
  165. )
  166. def has_voten(self, user_id):
  167. """Check if user has voted"""
  168. for option in self.options:
  169. if option.votes.filter(Vote.user_id == user_id).first():
  170. return True
  171. return False
  172. def get_option(self, option):
  173. """Get the pro option"""
  174. return self.options.filter(Option.name == option).first()
  175. def score(self):
  176. """Get winning option from question"""
  177. if self.combined_approval_voting:
  178. pro_option = self.get_option('Voor')
  179. pro_votes = pro_option.votes.count()
  180. against_option = self.get_option('Tegen')
  181. against_votes = against_option.votes.count()
  182. return pro_votes - against_votes
  183. higest = Option()
  184. for option in self.options:
  185. if higest.votes.count() < option.votes.count():
  186. higest = option
  187. return higest.name
  188. class Option(db.Model):
  189. """Model for Option"""
  190. id = db.Column(db.Integer, primary_key=True)
  191. name = db.Column(db.String)
  192. description = db.Column(db.String)
  193. user_id = db.Column(
  194. db.Integer,
  195. db.ForeignKey("user.id")
  196. )
  197. user = db.relationship(
  198. "User",
  199. backref=db.backref("eligible", lazy="dynamic")
  200. )
  201. question_id = db.Column(
  202. db.Integer,
  203. db.ForeignKey("question.id")
  204. )
  205. question = db.relationship(
  206. "Question",
  207. backref=db.backref("options", lazy="dynamic")
  208. )
  209. class Vote(db.Model):
  210. """Model for Vote"""
  211. id = db.Column(db.Integer, primary_key=True)
  212. datetime = db.Column(db.DateTime, default=datetime.utcnow)
  213. option_id = db.Column(
  214. db.Integer,
  215. db.ForeignKey("option.id")
  216. )
  217. option = db.relationship(
  218. "Option",
  219. backref=db.backref("votes", lazy="dynamic")
  220. )
  221. user_id = db.Column(
  222. db.Integer,
  223. db.ForeignKey("user.id")
  224. )
  225. user = db.relationship(
  226. "User",
  227. backref=db.backref("votes", lazy="dynamic")
  228. )
  229. class Code(db.Model):
  230. """Model for code"""
  231. id = db.Column(db.Integer, primary_key=True)
  232. expire_date = db.Column(db.DateTime, default=datetime.utcnow() + timedelta(days=20))
  233. secret = db.Column(db.String(255), unique=True)
  234. def get_digest(self, string):
  235. """Generate digest on string"""
  236. if isinstance(string, int):
  237. string = str(string)
  238. string = string.encode('utf-8')
  239. secret = self.secret.encode('utf-8')
  240. blake = blake2b(digest_size=3)
  241. blake.update(secret + string)
  242. return blake.hexdigest()