class Task(SurrogatePK, Model): __tablename__ = 'tasks' task_name = Column(db.Text(), unique=False, nullable=True, index=True) task_desc = Column(db.Text(), unique=False, nullable=True) start_date = Column(db.Date(), unique=False, nullable=False, index=True) end_date = Column(db.Date(), unique=False, nullable=True, index=True) # one-to-many user to tasks creator_id = db.Column('creator_id', db.Integer, db.ForeignKey('users.id')) # ref to optional associated case case_id = db.Column('case_id', db.Integer, db.ForeignKey('cases.id'), nullable=True) # many-to-many users to tasks assignees = relationship('User', secondary=task_user_map, backref=db.backref('user_tasks', lazy='dynamic')) # ref to optional associated project project_id = db.Column('project_id', db.Integer, db.ForeignKey('projects.id'), nullable=True) def __init__(self, *args, **kwargs): db.Model.__init__(self, *args, **kwargs) def __repr__(self): return '<Task(id={id}, task_name={task_name}, )>'.format( id=self.id, task_name=self.task_name)
class Tag(SurrogatePK, Model): __tablename__ = 'tags' kind = Column(db.Text(), nullable=False, index=True) tag = Column(db.Text(), nullable=False, index=True) def __init__(self, **kwargs): db.Model.__init__(self, **kwargs) def __repr__(self): return '<Tag(id={id}, kind={kind}, tag={tag})>'.format( tag=self.tag, id=self.id, kind=self.kind)
class CaseType(SurrogatePK, Model): __tablename__ = 'case_types' code = Column(db.String(15), unique=True, nullable=False, index=True) description = Column(db.String(80), nullable=False, index=True) def __init__(self, name, **kwargs): db.Model.__init__(self, name=name, **kwargs) def __repr__(self): return '<CaseType (code={code}, description={desc})>'.format( code=self.code, desc=self.description)
class Project(SurrogatePK, Model): __tablename__ = 'projects' project_name = Column(db.Text(), unique=False, nullable=True, index=True) project_desc = Column(db.Text(), unique=False, nullable=True) start_date = Column(db.Date(), unique=False, nullable=False, index=True) end_date = Column(db.Date(), unique=False, nullable=True, index=True) members = relationship('User', secondary=project_user_map, backref=db.backref('user_projects', lazy='dynamic')) tasks = relationship('Task', backref='project_tasks') def __init__(self, *args, **kwargs): db.Model.__init__(self, *args, **kwargs) def __repr__(self): return '<Project(id={id}, project_name={project_name}, )>'.format( id=self.id, project_name=self.project_name)
class CaseStatus(SurrogatePK, Model): __tablename__ = 'case_statuses' type = Column(db.String(32), unique=True, nullable=True, index=True) def __init__(self, name, **kwargs): db.Model.__init__(self, name=name, **kwargs) def __repr__(self): return '<CaseStatus (type={type}>'.format( type=self.type)
class Role(SurrogatePK, Model): __tablename__ = 'roles' name = Column(db.String(80), unique=True, nullable=False) user_id = ReferenceCol('users', nullable=True) user = relationship('User', backref='roles') def __init__(self, name, **kwargs): db.Model.__init__(self, name=name, **kwargs) def __repr__(self): return '<Role({name})>'.format(name=self.name)
class Case(SurrogatePK, Model): __tablename__ = 'cases' crd_number = Column(db.Text(), unique=False, nullable=False, index=True) case_name = Column(db.Text(), unique=False, nullable=True, index=True) case_desc = Column(db.Text(), unique=False, nullable=True) start_date = Column(db.Date(), unique=False, nullable=False, index=True) end_date = Column(db.Date(), unique=False, nullable=True, index=True) case_type_id = ReferenceCol('case_types', nullable=False) case_type = relationship('CaseType', backref='case_types') case_status_id = ReferenceCol('case_statuses', nullable=True, colname='case_status_id', parent_table='cases') case_status = relationship('CaseStatus', backref='case_statuses') region_id = ReferenceCol('regions', nullable=False) region = relationship('Region', backref='regions') users = association_proxy('user_cases', 'user') mars_risk_score = Column(db.Integer, unique=False, nullable=True) qau_risk_score = Column(db.Integer, unique=False, nullable=True) examiner_risk_score = Column(db.Integer, unique=False, nullable=True) tags = relationship('Tag', secondary=case_tag_map, backref=db.backref('cases', lazy='dynamic')) files = relationship('CaseFile', backref='case_files') tasks = relationship('Task', backref='case_tasks') def __init__(self, *args, **kwargs): db.Model.__init__(self, *args, **kwargs) def get_tags(self, kind=None): """ Get a case's tags, class must be initialized first :param kind: risk | non_qau_staff | None :return: list of unicode tags according to the specified kind """ if kind: return [i.tag for i in self.tags if i.kind == kind] else: return [i.tag for i in self.tags] def __repr__(self): return '<Case(id={id}, case_name={case_name}, )>'.format( id=self.id, case_name=self.case_name)
class Region(SurrogatePK, Model): __tablename__ = 'regions' code = Column(db.String(4), unique=True, nullable=False) name = Column(db.String(80), unique=True, nullable=False) address = Column(db.String(120), unique=False, nullable=False) city = Column(db.String(80), unique=False, nullable=False) state = Column(db.String(2), unique=False, nullable=False) zip = Column(db.String(15), unique=True, nullable=False) phone = Column(db.String(15), unique=True, nullable=False) def __init__(self, **kwargs): db.Model.__init__(self, **kwargs) def __repr__(self): return '<Region(code={code})>'.format(code=self.code)
class User(UserMixin, SurrogatePK, Model): __tablename__ = 'users' username = Column(db.String(80), unique=True, nullable=False, index=True) email = Column(db.String(80), unique=True, nullable=False) password = Column(db.String(128), nullable=True) created_at = Column(db.DateTime, nullable=False, default=dt.datetime.utcnow) first_name = Column(db.String(30), nullable=True) last_name = Column(db.String(30), nullable=True) active = Column(db.Boolean(), default=False) is_admin = Column(db.Boolean(), default=False) contract = Column(db.String(20), default='None', nullable=True) # ref to case staff table user_cases = db.relationship('CaseStaffMap', cascade="all, delete-orphan", backref='users') cases = association_proxy('user_cases', 'cases') # two types of task refs created_tasks = relationship('Task', backref='creator', lazy='dynamic') tasks = relationship('Task', secondary="task_user_map", backref=db.backref('users', lazy='dynamic')) # project refs projects = relationship('Project', secondary="project_user_map", backref=db.backref('users', lazy='dynamic')) def __init__(self, username, email, password=None, **kwargs): db.Model.__init__(self, username=username, email=email, **kwargs) if password: self.set_password(password) else: self.password = None def set_password(self, password): self.password = bcrypt.generate_password_hash(password) def check_password(self, value): return bcrypt.check_password_hash(self.password, value) @property def is_permanent(self): return True if self.contract == 'permanent' else False @property def is_contractor(self): return True if self.contract == 'contractor' else False @property def is_manager(self): return True if self.contract == 'manager' else False @property def full_name(self): if self.first_name and self.last_name: return "{0} {1}".format(self.first_name, self.last_name) else: return self.username def __repr__(self): return '<User({username!r})>'.format(username=self.username)