123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268 |
- """
- All models for module
- """
- from hashlib import blake2b
- import hmac
- from urllib.parse import quote
- from datetime import datetime, timedelta
- # from sqlalchemy.ext.hybrid import hybrid_method, hybrid_property
- import markdown
- from flask import Markup
- from flask_login import UserMixin
- from app import db, argon2, login_manager
- ALLOWED_EXTENSIONS = set(['txt', 'pdf', 'png', 'jpg', 'jpeg', 'gif'])
- IMAGE_EXTENSIONS = set(['png', 'jpg', 'jpeg', 'gif'])
- class User(db.Model, UserMixin):
- """Model for User"""
- id = db.Column(db.Integer, primary_key=True)
- name = db.Column(db.String, unique=True, nullable=False)
- email = db.Column(db.String(255), unique=True)
- discord = db.Column(db.String(255), unique=True)
- game_id = db.Column(db.BigInteger, unique=True)
- alt = db.Column(db.Boolean, server_default='f', default=False)
- party_member = db.Column(db.Boolean, server_default='f', default=False)
- _password = db.Column("password", db.String(255))
- registration_at = db.Column(db.DateTime, default=datetime.utcnow)
- approved = db.Column(db.Boolean, server_default='f', default=False)
- def __init__(self, id=None):
- self.id = id
- @login_manager.user_loader
- def load_user(user_id):
- return User.query.get(user_id)
- @property
- def password(self):
- """Return the password"""
- return self._password
- @password.setter
- def password(self, password):
- """Hash password"""
- self._password = argon2.generate_password_hash(password)
- def check_password(self, password):
- """Check if password is correct"""
- return argon2.check_password_hash(self.password, password)
- question_id = db.Column(
- db.Integer,
- db.ForeignKey("question.id")
- )
- user = db.relationship(
- "Question",
- backref=db.backref("users", lazy="dynamic")
- )
- class Page(db.Model):
- """Model for Page"""
- id = db.Column(db.Integer, primary_key=True)
- title = db.Column(db.String, nullable=False)
- datetime = db.Column(db.DateTime, default=datetime.utcnow)
- source = db.Column(db.String)
- private = db.Column(db.Boolean, server_default='f', default=False)
- def content(self):
- """Render page source"""
- return Markup(markdown.markdown(self.source, extensions=['tables', 'toc', 'meta']))
- def url(self):
- """Generate URL for page"""
- return quote(self.title.strip().lower().replace(" ", "_"))
- def path(self):
- """Generate path with parents"""
- if self.parent_id:
- return '%s/%s' % (self.parent.path(), self.url())
- return self.url()
- user_id = db.Column(
- db.Integer,
- db.ForeignKey("user.id")
- )
- user = db.relationship(
- "User",
- backref=db.backref("Pages", lazy="dynamic")
- )
- parent_id = db.Column(
- db.Integer,
- db.ForeignKey("page.id")
- )
- parent = db.relationship(
- "Page",
- backref=db.backref("children", lazy="dynamic"),
- uselist=False,
- remote_side=id
- )
- class File(db.Model):
- """Model for Page"""
- id = db.Column(db.Integer, primary_key=True)
- title = db.Column(db.String, nullable=False)
- datetime = db.Column(db.DateTime, default=datetime.utcnow)
- path = db.Column(db.String, nullable=False)
- identifier = db.Column(db.String)
- private = db.Column(db.Boolean, server_default='f', default=False)
- def url(self):
- """Generate URL for page"""
- return quote(self.title.strip().lower().replace(" ", "_"))
- def extension(self):
- """Return file extension"""
- return '.' in self.path and self.path.rsplit('.', 1)[1].lower()
- def is_image(self):
- """True if file is image"""
- return self.extension() in IMAGE_EXTENSIONS
- user_id = db.Column(
- db.Integer,
- db.ForeignKey("user.id")
- )
- user = db.relationship(
- "User",
- backref=db.backref("files", lazy="dynamic")
- )
- class Ballot(db.Model):
- """Model for Ballot"""
- id = db.Column(db.Integer, primary_key=True)
- name = db.Column(db.String, nullable=False)
- description = db.Column(db.String)
- start_at = db.Column(db.DateTime, default=datetime.utcnow)
- end_at = db.Column(db.DateTime, nullable=False)
- user_id = db.Column(
- db.Integer,
- db.ForeignKey("user.id")
- )
- user = db.relationship(
- "User",
- backref=db.backref("options", lazy="dynamic")
- )
- priority_id = db.Column(
- db.Integer,
- db.ForeignKey("priority.id")
- )
- priority = db.relationship(
- "Priority",
- backref=db.backref("ballots", lazy="dynamic")
- )
- def active(self):
- """Check if vote is active"""
- today = datetime.today()
- if self.start_at <= today <= self.end_at:
- return True
- #return False
- return True
- class Priority(db.Model):
- """Model for Priority"""
- id = db.Column(db.Integer, primary_key=True)
- name = db.Column(db.String, nullable=False)
- description = db.Column(db.String, nullable=False)
- class Question(db.Model):
- """Model for Question"""
- id = db.Column(db.Integer, primary_key=True)
- name = db.Column(db.String, nullable=False)
- description = db.Column(db.String)
- combined_approval_voting = db.Column(db.Boolean, server_default='f', default=False)
- ballot_id = db.Column(
- db.Integer,
- db.ForeignKey("ballot.id")
- )
- ballot = db.relationship(
- "Ballot",
- backref=db.backref("questions", lazy="dynamic")
- )
- class Option(db.Model):
- """Model for Option"""
- id = db.Column(db.Integer, primary_key=True)
- name = db.Column(db.String)
- description = db.Column(db.String)
- user_id = db.Column(
- db.Integer,
- db.ForeignKey("user.id")
- )
- user = db.relationship(
- "User",
- backref=db.backref("eligible", lazy="dynamic")
- )
- question_id = db.Column(
- db.Integer,
- db.ForeignKey("question.id")
- )
- question = db.relationship(
- "Question",
- backref=db.backref("options", lazy="dynamic")
- )
- class Vote(db.Model):
- """Model for Vote"""
- id = db.Column(db.Integer, primary_key=True)
- datetime = db.Column(db.DateTime, default=datetime.utcnow)
- option_id = db.Column(
- db.Integer,
- db.ForeignKey("option.id")
- )
- option = db.relationship(
- "Option",
- backref=db.backref("votes", lazy="dynamic")
- )
- user_id = db.Column(
- db.Integer,
- db.ForeignKey("user.id")
- )
- user = db.relationship(
- "User",
- backref=db.backref("votes", lazy="dynamic")
- )
- class Code(db.Model):
- """Model for code"""
- id = db.Column(db.Integer, primary_key=True)
- expire_date = db.Column(db.DateTime, default=datetime.utcnow() + timedelta(days=20))
- secret = db.Column(db.String(255), unique=True)
- def get_digest(self, string):
- """Generate digest on string"""
- if isinstance(string, int):
- string = str(string)
- string = string.encode('utf-8')
- secret = self.secret.encode('utf-8')
- blake = blake2b(digest_size=3)
- blake.update(secret + string)
- return blake.hexdigest()
|