Exemplo n.º 1
0
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(20), unique=True, nullable=False)
    password = db.Column(db.String(60), nullable=False)
    videos = db.relationship('Video',
                             secondary=users_videos,
                             lazy='subquery',
                             backref=db.backref('users', lazy=True))

    def __init__(self, username, password):
        self.username = username
        self.password = User.hashed_password(password)

    @staticmethod
    def hashed_password(password):
        return bcrypt.generate_password_hash(password).decode("utf-8")

    @staticmethod
    def get_user_with_username_and_password(username, password):
        user = User.query.filter_by(username=username).first()
        if user and bcrypt.check_password_hash(user.password, password):
            return user
        else:
            return None

    def __repr__(self):
        return f"<User '{self.id}','{self.username}','{self.password}'>"

    @property
    def serialize(self):
        return {
            'id': self.id,
            'username': self.username,
        }
Exemplo n.º 2
0
class User(db.Model, UserMixin):
    id = db.Column(db.Integer, primary_key=True)
    user_username = db.Column(db.String(100), unique=True, nullable=False)
    user_email = db.Column(db.String(100), unique=True, nullable=True)
    user_image = db.Column(db.String(100),
                           nullable=True,
                           default='default.jpg')
    user_password = db.Column(db.String(100), nullable=False)
    user_status = db.Column(db.Boolean, nullable=False, default=False)
    questions = db.relationship('Question', backref='author', lazy=True)
    comments = db.relationship('Comment', backref='article_author', lazy=True)

    def vote_question(self, question):
        if not self.has_voted_question(question):
            vote = Vote(v_u_id=self.id, v_q_id=question.id)
            db.session.add(vote)
            db.session.commit()

    def unvote_question(self, question):
        if self.has_voted_question(question):
            Vote.query.filter_by(v_u_id=self.id, v_q_id=question.id).delete()
            db.session.commit()

    def has_voted_question(self, question):
        return Vote.query.filter(Vote.v_u_id == self.id, Vote.v_q_id
                                 == question.id).count() > 0

    def __repr__(self):
        return f"User('{self.user_username}', '{self.user_email}', '{str(self.user_image)}')"
Exemplo n.º 3
0
class Departament(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.String(db.String(64))
    parent_id = db.Column(db.Integer)

    def __repr__(self):
        return f'<Departament {self.id} "{self.name}">'
Exemplo n.º 4
0
class score(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    player_id1 = db.Column(db.String(64), index=True)
    player_id2 = db.Column(db.String(64), index=True)
    deadline = db.Column(db.Date, index=True)
    score = db.Column(db.String(120), index=True, default="")
    division = db.Column(db.String(2), index=True)
    level = db.Column(db.Float, index=True)
Exemplo n.º 5
0
class Employee(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    first_name = db.Column(db.String(64))
    last_name = db.Column(db.String(64))
    departament_id = db.Column(db.Integer, db.ForeignKey('departament.id'))
    departament = db.relationship('Departament', backref='employee')

    def __repr__(self):
        return f'<Employee ({self.id}) {self.first_name} {self.last_name} from {self.departament_id}>'
Exemplo n.º 6
0
class Author(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(200), nullable=False)
    image_url = db.Column(db.String(200), nullable=False)
    videos = db.relationship('Video', backref='author', lazy=True)

    @property
    def serialize(self):
        return {'id': self.id, 'image': self.image_url, 'name': self.name}

    def __repr__(self):
        return '<Author %r>' % self.name
Exemplo n.º 7
0
class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.String(140))
    timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))

    def __repr__(self):
        return '<Post {}>'.format(self.body)
Exemplo n.º 8
0
class Context(db.Model):
    __tablename__ = "contexts"
    id = db.Column(db.Integer, primary_key=True)
    term = db.Column(db.String(80), index=True, unique=True, nullable=False)
    content = db.Column(db.LargeBinary)

    def __repr__(self):
        return "<Context(id={}, term='{}', content='{}')>".format(
            self.id, self.term, self.content)
Exemplo n.º 9
0
class Highlight(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    video_id = db.Column(db.Integer, db.ForeignKey('video.id'), nullable=False)
    offset = db.Column(db.Integer, nullable=False)
    score = db.Column(db.Integer, nullable=False)
    emotion = db.Column(db.String(200))

    def __repr__(self):
        return f'<Highlight of {self.video_id}>'
Exemplo n.º 10
0
class user(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    firstName = db.Column(db.String(64), unique=True)
    lastName = db.Column(db.String(64))
    email = db.Column(db.String(120), index=True)  #, unique=True)
    phone = db.Column(db.String(120), index=True)  #, unique=True)
    password_hash = db.Column(db.String(128))

    def __repr__(self):
        return '<User {}>'.format(self.username)

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def is_active(self):
        """True, as all users are active."""
        return True

    def get_id(self):
        """Return the email address to satisfy Flask-Login's requirements."""
        return self.id

    def is_authenticated(self):
        """Return True if the user is authenticated."""
        return self.authenticated
Exemplo n.º 11
0
class Video(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(200), nullable=False)
    image_url = db.Column(db.String(200), nullable=False)
    author_id = db.Column(db.Integer,
                          db.ForeignKey('author.id'),
                          nullable=False)
    highlights = db.relationship('Highlight', backref='video', lazy=True)
    processed = db.Column(db.Boolean, nullable=False, default=False)

    @property
    def serialize(self):
        return {
            'id': self.id,
            'image': self.image_url,
            'profile': self.author.image_url,
            'name': self.name,
            'author': self.author.name,
        }

    def __repr__(self):
        return '<Video %r>' % self.name
Exemplo n.º 12
0
class User(UserMixin, db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(64), index=True, unique=True)
    email = db.Column(db.String(64), index=True, unique=True)
    password_hash = db.Column(db.String(128))
    posts = db.relationship('Post', backref='author', lazy='dynamic')

    about_me = db.Column(db.String(140))
    last_seen = db.Column(db.DateTime, default=datetime.utcnow)

    def __repr__(self):
        return '<User {}>'.format(self.username)

    def set_password(self, password):
        self.password_hash = generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password_hash, password)

    def avatar(self, size):
        digest = md5(self.email.lower().encode('utf-8')).hexdigest()
        return 'https://www.gravatar.com/avatar/' \
               '{}?d=identicon&s={}'.format(digest, size)
Exemplo n.º 13
0
class pointTable(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    player_id = db.Column(db.String(64))
    played = db.Column(db.Integer, default=0)
    win = db.Column(db.Integer, default=0)
    loss = db.Column(db.Integer, default=0)
    tie = db.Column(db.Integer, default=0)
    bonus = db.Column(db.Integer, default=0)
    points = db.Column(db.Integer, default=0)
    xrating = db.Column(db.Integer, default=1000)
    gamesplayed = db.Column(db.Integer, default=0)
    gameswon = db.Column(db.Integer, default=0)
    set1played = db.Column(db.Integer, default=0)
    set1won = db.Column(db.Integer, default=0)
    set2played = db.Column(db.Integer, default=0)
    set2won = db.Column(db.Integer, default=0)
    set3played = db.Column(db.Integer, default=0)
    set3won = db.Column(db.Integer, default=0)
Exemplo n.º 14
0
class UserLogin(db.Model):
    """
    Common user class with additional student's group attribute
    """
    __tablename__ = "users"
    id = db.Column(db.Integer, primary_key=True)
    login = db.Column(db.String(80), unique=True, index=True, nullable=False)
    group = db.Column(db.String(80))
    hash = db.Column(db.Binary(60), nullable=False)
    tries = db.relationship('Try', backref='users', lazy=True)

    is_authenticated = True
    is_active = True
    is_anonymous = False

    @classmethod
    def init(cls, login: str, group: str, password: str) -> "UserLogin":
        """
        Safe User constructor, performs hashing of the password
        Doesn't commit new user into the database
        """
        user = cls(login=login, group=group, hash="")
        user.update_password(password)
        db.session.add(user)
        db.session.commit()
        return user

    def update_password(self, password):
        log("User {} changes the password".format(self.login))
        self.hash = hashpw(bytes(password, "utf8"), gensalt())
        db.session.commit()

    def check_password(self, password: str) -> bool:
        """
        Compare stored hash with hash of input password
        """
        return hashpw(bytes(password, "utf8"), self.hash) == self.hash

    @classmethod
    def get(cls: Type["UserLogin"], uid: int) -> Optional["UserLogin"]:
        """
        Get User by user_id
        """
        return cls.query.get(uid)

    def get_try(self, eid: int) -> Optional["Try"]:
        """
        Check if user is doing exercise now
        """
        for i in self.tries:
            if i.eid == eid and i.finished_at is None:
                return i
        return None

    def get_tries(self) -> List["Try"]:
        return self.tries

    def add_try(self, tr: "Try") -> None:
        pass

    def get_id(self) -> str:
        """
        Necessary method for flask_login
        """
        return str(self.id)

    def __repr__(self):
        return "<User(id='{}', group='{}', active={})>".format(
            self.id, self.group, self.in_action())
Exemplo n.º 15
0
class players(db.Model):
    player_id = db.Column(db.String(64), index=True, primary_key=True)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'))