""" 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) def game_url(self): """Give profile url""" if self.game_id: return "https://rivalregions.com/#slide/profile/%d" % self.game_id return 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 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") ) def has_voten(self, user_id): """Check if user has voted""" for option in self.options: if option.votes.filter(Vote.user_id == user_id).first(): return True return False def get_option(self, option): """Get the pro option""" return self.options.filter(Option.name == option).first() def score(self): """Get winning option from question""" if self.combined_approval_voting: pro_option = self.get_option('Voor') pro_votes = pro_option.votes.count() against_option = self.get_option('Tegen') against_votes = against_option.votes.count() return pro_votes - against_votes higest = Option() for option in self.options: if higest.votes.count() < option.votes.count(): higest = option return higest.name 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()