models.py 8.2 KB

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