class User(db.Model, TimestampMixin): """ Contains information of users table """ __tablename__ = 'users' def __init__(self, **kwargs): """ Support direct initialization :param kwargs: """ for k, v in kwargs.items(): setattr(self, k, v) id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(191), nullable=False, unique=True) email = db.Column(db.String(191), nullable=False, unique=True) password_hash = db.Column(db.String(100)) is_admin = db.Column(db.Integer, default=0) is_active = db.Column(db.SmallInteger, default=0) last_login = db.Column(db.TIMESTAMP, default=datetime.datetime.now) # history_pass_change = relationship("HistoryPassChange") # fullname = db.Column(db.String(191), nullable=False) @property def password(self): raise AttributeError('password: write-only field') @password.setter def password(self, password): self.password_hash = bcrypt.generate_password_hash(password).decode( 'utf-8') def get_password(self): return self.password_hash def check_password(self, password): return bcrypt.check_password_hash(self.password_hash, password) def get_id(self): return self.id @property def is_authenticated(self): return True def to_dict(self): """ Transform user obj into dict :return: """ return { 'id': self.id, 'username': self.username, 'email': self.email, # 'fullname': self.fullname, }
class SignupRequest(db.Model, TimestampMixin): __tablename__ = 'signup_request' def __init__(self, **kwargs): """ Support direct initialization :param kwargs: """ for k, v in kwargs.items(): setattr(self, k, v) id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(191), nullable=False) email = db.Column(db.String(191), nullable=False) # fullname = db.Column(db.String(191), nullable=False) password_hash = db.Column(db.String(100)) is_admin = db.Column(db.Boolean, default=0) expired_time = db.Column(db.TIMESTAMP) user_token_confirm = db.Column(db.String(512), nullable=True) @property def password(self): raise AttributeError('password: write-only field') @password.setter def password(self, password): self.password_hash = bcrypt.generate_password_hash(password).decode( 'utf-8') def check_password(self, password): return bcrypt.check_password_hash(self.password_hash, password) def get_id(self): return self.id @property def is_authenticated(self): return True def to_dict(self): """ Transform user obj into dict :return: """ return { 'id': self.id, 'username': self.username, 'email': self.email, # 'fullname': self.fullname, }
class Room(db.Model, TimestampMixin): __tablename__ = 'rooms' def __init__(self, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255), nullable=False) has_password = db.Column(db.Boolean, default=False) password_hash = db.Column(db.String(255)) number_of_users = db.Column(db.Integer) descriptions = db.Column(db.JSON) current_song = db.Column(db.String(512)) current_song_start_time = db.Column(db.TIMESTAMP) creator_id = db.Column(db.Integer, ForeignKey('users.id'), nullable=False) creator = relationship('User') users = relationship('User', backref='room') songs = relationship('Song', backref='room') messages = relationship('Message', backref='room') @property def password(self): raise AttributeError('password: write-only field') @password.setter def password(self, password): self.password_hash = bcrypt.generate_password_hash(password).decode('utf-8') def check_password(self, password): return bcrypt.check_password_hash(self.password_hash, password) def to_dict(self): # TODO: add more visible attributes return { 'id': self.id, 'name': self.name, 'has_password': self.has_password, 'number_of_users': self.number_of_users, 'descriptions': self.descriptions }
class User(db.Model, TimestampMixin): __tablename__ = 'users' def __init__(self, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255), nullable=False) token = db.Column(db.String(512), nullable=False) room_id = db.Column(db.Integer, ForeignKey('rooms.id')) room = relationship('Room', back_populates='users') songs = relationship('Song', backref='user') votes = relationship('Vote', backref='user') messages = relationship('Message', backref='user') reactions = relationship('Reaction', backref='user') def to_dict(self): return {'id': self.id, 'name': self.name, 'token': self.token}
class Action(db.Model, TimestampMixin): __tablename__ = 'action' def __init__(self, **kwargs): """ Support direct initialization :param kwargs: """ for k, v in kwargs.items(): setattr(self, k, v) id = db.Column(db.Integer, primary_key=True, autoincrement=True) detail = db.Column(db.String(500))
class HistoryWrongPass(db.Model, TimestampMixin): __tablename__ = 'history_wrong_pass' def __init__(self, **kwargs): """ Support direct initialization :param kwargs: """ for k, v in kwargs.items(): setattr(self, k, v) id = db.Column(db.Integer, primary_key=True, autoincrement=True) username = db.Column(db.String(100), ForeignKey('users.username')) time = db.Column(db.TIMESTAMP)
class UserToken(db.Model, TimestampMixin): __tablename__ = 'user_token' def __init__(self, **kwargs): """ Support direct initialization :param kwargs: """ for k, v in kwargs.items(): setattr(self, k, v) id = db.Column(db.Integer, primary_key=True, autoincrement=True) user_id = db.Column(db.Integer, ForeignKey('users.id')) token = db.Column(db.String(512)) expired_time = db.Column(db.TIMESTAMP)
class HistoryPassChange(db.Model, TimestampMixin): __tablename__ = 'history_pass_change' def __init__(self, **kwargs): """ Support direct initialization :param kwargs: """ for k, v in kwargs.items(): setattr(self, k, v) id = db.Column(db.Integer, primary_key=True, autoincrement=True) user_id = db.Column(db.Integer, ForeignKey('users.id')) history_pass_change = db.Column(db.String(100), nullable=True)
class Song(db.Model, TimestampMixin): __tablename__ = 'songs' def __init__(self, **kwargs): for key, value in kwargs.items(): setattr(self, key, value) id = db.Column(db.Integer, primary_key=True, autoincrement=True) link = db.Column(db.String(512), nullable=False) number_of_upvotes = db.Column(db.Integer) number_of_downvotes = db.Column(db.Integer) room_id = db.Column(db.Integer, ForeignKey('rooms.id'), nullable=False) room = relationship('Room', back_populates='songs') user_id = db.Column(db.Integer, ForeignKey('users.id'), nullable=False) user = relationship('User', back_populates='songs') votes = relationship('Vote', backref='song') def to_dict(self): pass
class User(db.Model, TimestampMixin): """ Contains information of users table """ __tablename__ = 'users' def __init__(self, **kwargs): """ Support direct initialization :param kwargs: """ for k, v in kwargs.items(): setattr(self, k, v) class Role(enum.Enum): """ Role of a user in the system. """ admin = 'admin' moderator = 'moderator' viewer = 'viewer' id = db.Column(db.Integer, primary_key=True, autoincrement=True) email = db.Column(db.String(191), nullable=False, unique=True) username = db.Column(db.String(191), nullable=False, unique=True) fullname = db.Column(db.String(191), nullable=False) status = db.Column(db.Integer, default=1) password_hash = db.Column(db.String(100)) id_token = db.Column(db.String(512), nullable=True) image = db.Column(db.Text(), nullable=True) role = db.Column(db.Enum(Role), nullable=False, default=Role.viewer) last_login = db.Column(db.TIMESTAMP, default=datetime.datetime.now) @property def password(self): raise AttributeError('password: write-only field') @password.setter def password(self, password): self.password_hash = bcrypt.generate_password_hash(password).decode( 'utf-8') def check_password(self, password): return bcrypt.check_password_hash(self.password_hash, password) def get_id(self): return self.id @property def is_authenticated(self): return True def to_dict(self): """ Transform user obj into dict :return: """ return { 'id': self.id, 'username': self.username, 'email': self.email, 'role': self.role, 'fullname': self.fullname, 'status': self.status }