class SurveyAnswer(db.Model, TimestampMixin): __tablename__ = 'survey_answer' def __init__(self, **kwargs): if not kwargs.get('id'): setattr(self, 'id', uuid4().hex) for k, v in kwargs.items(): setattr(self, k, v) id = db.Column(db.String(255), primary_key=True) answer = db.Column(db.JSON()) survey_link_id = db.Column(db.String(255), db.ForeignKey('survey_link.id')) survey_link = relationship("SurveyLink") user_id = db.Column(db.String(255), db.ForeignKey('user.id'), nullable=True) def to_dict(self): return { 'id': self.id, 'answer': self.answer, 'created_at': self.created_at, 'survey': self.survey_form.to_dict() } def to_dict_simple(self): return { 'id': self.id, 'answer': self.answer, 'created_at': self.created_at, 'survey_form_id': self.survey_form_id }
class SurveyForm(db.Model, TimestampMixin): __tablename__ = 'survey_form' def __init__(self, **kwargs): if not kwargs.get('id'): setattr(self, 'id', uuid4().hex) self.update_attr(**kwargs) id = db.Column(db.String(255), primary_key=True) name = db.Column(db.Text, nullable=False) config = db.Column(db.JSON()) status = db.Column(db.Enum(Status), default=Status.OPEN) owner_id = db.Column(db.String(255), db.ForeignKey('user.id')) link_collection = relationship("LinkCollection", cascade="save-update, merge, delete") invite_collection = relationship("InviteCollection", cascade="save-update, merge, delete") email_collection = relationship("EmailCollection", cascade="save-update, merge, delete") def update_attr(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v) def to_dict(self): return { 'id': self.id, 'name': self.name, 'config': json.dumps(self.config), 'status': self.status.value, 'created_at': self.created_at, 'updated_at': self.updated_at, 'owner_id': self.owner_id, }
class Function(db.Model, TimestampMixin): __tablename__ = 'function' def __init__(self, **kwargs): for k, v in kwargs.items(): setattr(self, k, v) id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(50), nullable=False, unique=True) description = db.Column(db.Text(), nullable=True)
class UserGrant(db.Model, TimestampMixin): __tablename__ = 'user_grant' def __init__(self, **kwargs): if not kwargs.get('id'): setattr(self, 'id', uuid4().hex) for k, v in kwargs.items(): setattr(self, k, v) id = db.Column(db.String(255), primary_key=True) table_name = db.Column(db.Enum(Table), nullable=False) record_id = db.Column(db.String(255), nullable=False) can_view = db.Column(db.Boolean(), nullable=False) can_update = db.Column(db.Boolean(), nullable=False) can_delete = db.Column(db.Boolean(), nullable=False) user_id = db.Column(db.String(255), db.ForeignKey('user.id'))
class User(db.Model, TimestampMixin): __tablename__ = 'user' def __init__(self, **kwargs): if not kwargs.get('id'): setattr(self, 'id', uuid4().hex) for k, v in kwargs.items(): setattr(self, k, v) id = db.Column(db.String(255), primary_key=True) email = db.Column(db.String(255), nullable=False, unique=True) username = db.Column(db.String(255), nullable=False, unique=True) fullname = db.Column(db.String(255), nullable=True) is_active = db.Column(db.Boolean(), default=True) password_hash = db.Column(db.String(255)) link_survey = db.Column(db.ARRAY(db.String(255)), default=[]) other_function = db.Column(db.ARRAY(db.Integer), default=[]) contact = db.Column(db.ARRAY(db.String(255)), default=[]) survey_form = relationship('SurveyForm', cascade="save-update, merge, delete") role_id = db.Column(db.Integer, db.ForeignKey('role.id', ondelete='SET NULL'), nullable=True) role = relationship("Role") @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 def to_dict(self): """ Transform user obj into dict :return: """ return { 'id': self.id, 'username': self.username, 'email': self.email, 'fullname': self.fullname, 'is_active': self.is_active, 'created_at': self.created_at, 'roles': self.role.get_permission() if self.role else None, 'contact': self.contact } def to_dict_simple(self): return { 'id': self.id, 'username': self.username, 'email': self.email, 'fullname': self.fullname, 'role_id': self.role_id, 'role_name': self.role.name if self.role else None, 'functions': [], 'created_at': self.created_at.isoformat(), 'updated_at': self.updated_at.isoformat(), }