class SlackInstallation(BaseModel): """Keep track of each installation of Slack integration""" __tablename__ = "slack_installation" def __repr__(self): # pragma: no cover return f"<SlackInstallation: {self.workspace_name}>" # Attributes access_token = db.Column(EncryptedType(db.String, SECRET_KEY, AesEngine, "pkcs5"), nullable=False) authorizing_user_id = db.Column(db.String(300), nullable=False) bot_access_token = db.Column(EncryptedType(db.String, SECRET_KEY, AesEngine, "pkcs5"), nullable=False) bot_user_id = db.Column(EncryptedType(db.String, SECRET_KEY, AesEngine, "pkcs5"), nullable=False) scope = db.Column(db.String(800), nullable=False) workspace_id = db.Column(db.String(20), index=True, nullable=False) workspace_name = db.Column(db.String(255), nullable=False) state = db.Column(db.String(20), nullable=False, default="installed") auth_response = db.Column("auth_response", db.JSON) # Relationships github_summary_users = db.relationship("GitHubSummaryUser", back_populates="installation") github_summary_config = db.relationship( "GitHubSummaryConfiguration", back_populates="slack_installation", uselist=False)
class GitHubSummaryUser(BaseModel): """GitHub Summary User table TODO: GitHubSummaryUser should really be related to GitHubSummaryConfiguration versus SlackInstallation """ __tablename__ = "github_summary_user" def __repr__(self): # pragma: no cover return f"<User slack: {self.slack_id} github: {self.github_username}>" # Attributes installation_id = db.Column( db.Integer, db.ForeignKey("slack_installation.id", name="fk_installation_id"), nullable=False, ) slack_id = db.Column(db.String(300), nullable=False) github_id = db.Column(db.String(300), nullable=True) github_username = db.Column(db.String(300), nullable=True) github_state = db.Column(db.String(36), nullable=True) github_access_token = db.Column(db.String(100), nullable=True) # Relationships installation = db.relationship( "SlackInstallation", back_populates="github_summary_users" )
class GitHubSummaryUser(BaseModel): # TODO # add a field for workspace_id # add a relationship for the ORM """GitHub Summary User table""" __tablename__ = "github_summary_user" def __repr__(self): # pragma: no cover return f"<User slack: {self.slack_id} github: {self.github_username}>" # Attributes installation_id = db.Column( db.Integer, db.ForeignKey("slack_installation.id", name="fk_installation_id"), nullable=False, ) slack_id = db.Column(db.String(300), nullable=False) github_id = db.Column(db.String(300), nullable=True) github_username = db.Column(db.String(300), nullable=True) github_state = db.Column(db.String(36), nullable=True) github_access_token = db.Column(db.String(100), nullable=True) # Relationships installation = db.relationship("SlackInstallation", back_populates="github_summary_users")
class ApiUser(BaseModel): """API User table""" __tablename__ = "api_user" def __repr__(self): return f"<API: {self.username}>" # Attributes username = db.Column(db.String(255), nullable=False) token = db.Column(db.String(255), nullable=False) role = db.Column(db.String(255), nullable=False, default="user") # Relationships tasks = db.relationship("Task", back_populates="user")
class GitHubSummaryConfiguration(BaseModel): __tablename__ = "github_summary_configuration" def __repr__(self): # pragma: no cover return f"<GitHubSummaryConfiguration>" installation_id = db.Column( db.Integer, db.ForeignKey("slack_installation.id", name="fk_installation_id"), nullable=False, ) channel = db.Column(db.String(20), nullable=False) time_to_post = db.Column(db.String(20), nullable=True) timezone_info = db.Column(db.JSON) # Relationships slack_installation = db.relationship( "SlackInstallation", back_populates="github_summary_config" )
class SlackAppHomeOpened(BaseModel): """Track number of times App Home is opened For bot onboarding """ __tablename__ = "slack_app_home_opened" installation_id = db.Column( db.Integer, db.ForeignKey("slack_installation.id", name="fk_installation_id"), index=True, nullable=False, ) slack_id = db.Column(db.String(30), index=True, nullable=False) count = db.Column(db.Integer, nullable=False, default=1) # Relationships installation = db.relationship("SlackInstallation")
class Task(BaseModel): """Task Base Table""" __tablename__ = "task" # Attributes job_id = db.Column(db.String(36), index=True) name = db.Column(db.String(128), index=True) description = db.Column(db.String(128)) failed = db.Column(db.Boolean, default=False) complete = db.Column(db.Boolean, default=False) user_id = db.Column(db.Integer, db.ForeignKey("api_user.id", name="fk_task_user_id")) type = db.Column(db.String(55)) __mapper_args__ = { "polymorphic_identity": "task", "polymorphic_on": "type" } # Relationships user = db.relationship("ApiUser", back_populates="tasks") def get_rq_job(self): try: rq_job = Job.fetch(self.job_id, rq.connection) except (RedisError, NoSuchJobError): return None return rq_job def get_progress(self): job = self.get_rq_job() return job.meta.get("progress", 0) if job is not None else 100 def __repr__(self): # pragma: no cover return f"<Task: {self.job_id}-{self.name}>"