class Project(db.Model): __tablename__ = "projects" id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(80), unique=True) name = db.Column(db.String(80)) owner = db.Column(db.Integer, db.ForeignKey('users.id')) description = db.Column(db.Text) start = db.Column(db.DateTime) finish = db.Column(db.DateTime) cycle_id = db.Column(db.Integer, db.ForeignKey('reportingcycles.id')) cycle = db.relationship('ReportingCycle', backref='project') org_id = db.Column(db.Integer, db.ForeignKey('organisations.id')) organisation = db.relationship('Organisation', backref='project') status = db.Column(db.Boolean, default=True) users = db.relationship("UserProject") __table_args__ = (db.UniqueConstraint('code', 'org_id', name='uix_prj1'), ) def __init__(self, code, name, description, owner, start, finish, cycle, organisation, status): self.code = code self.name = name self.owner = owner self.description = description self.start = start self.finish = finish self.status = status self.org_id = organisation.id self.cycle_id = cycle.id def __repr__(self): return self.name
class Section(db.Model): __tablename__ ="sections" id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(80), unique=True) name = db.Column(db.String(80)) project_id = db.Column(db.Integer, db.ForeignKey('projects.id')) project= db.relationship('Project', backref='section') parent_id = db.Column(db.Integer, db.ForeignKey('sections.id')) parent = db.relationship(lambda: Section, remote_side=id, backref='subs') active = db.Column(db.Boolean) __table_args__ = ( db.UniqueConstraint('code', 'project_id', name='uix_sec1'), ) def __init__(self, code, name, project, parent, active=True): self.code = code self.name = name self.active = active self.project_id = project.id self.parent = parent if parent: self.parent_id= parent.id def json(self): return ({"name": self.name , "id": str(self.id) , "parent_id": str(self.parent_id), "level": 0, "childs":[ ]}) def __repr__(self): return self.name
class Message(db.Model): __tablename__ = "messages" id = db.Column(db.Integer, primary_key=True) subject = db.Column(db.String(100)) sender_id = db.Column(db.Integer, db.ForeignKey('users.id')) sender = db.relationship('User', foreign_keys=[sender_id]) recipient_id = db.Column(db.Integer, db.ForeignKey('users.id')) recipient = db.relationship('User', foreign_keys=[recipient_id]) body = db.Column(db.String(255)) timestamp = db.Column(db.DateTime, index=True, default=datetime.utcnow) read = db.Column(db.Boolean) is_active = db.Column(db.Boolean) def __repr__(self): return '<Message {}>'.format(self.body)
class Company(db.Model): __tablename__ = "companies" id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(80), unique=True) name = db.Column(db.String(80)) description = db.Column(db.Text) address = db.Column(db.Text) contact_number = db.Column(db.Text) contact_person = db.Column(db.Text) org_id = db.Column(db.Integer, db.ForeignKey('organisations.id')) organisation = db.relationship('Organisation', backref='company') status = db.Column(db.Boolean, default=True) __table_args__ = (db.UniqueConstraint('code', 'org_id', name='uix_prj1'), ) def __init__(self, code, name, description, address, contact_number, contact_person, organisation, status=True): self.code = code self.name = name self.description = description self.address = address self.constact_person = contact_person self.contact_number = contact_number self.org_id = organisation.id def __repr__(self): return self.name
class UserProject(db.Model): __tablename__ = 'user_project' project_id = db.Column(db.Integer, db.ForeignKey('projects.id'), primary_key=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), primary_key=True) role_id = db.Column(db.Integer) user = db.relationship("User", backref="parent_assocs") project = db.relationship("Project", backref="assoc") is_active = db.Column(db.Boolean) __table_args__ = (db.UniqueConstraint('project_id', 'user_id', name='uix_1'), ) def __init__(self, project, user, role, is_active=True): self.project_id = project.id self.user_id = user.id self.role_id = role.id self.is_active = is_active
class Resource(db.Model): __tablename__ = "rsrc_types" id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(80), unique=True) name = db.Column(db.String(80)) type_id = db.Column(db.Integer, db.ForeignKey('rsrc_types.id')) type = db.relationship('RSRCType', backref='resources') organisation_id = db.Column(db.Integer, db.ForeignKey('organisations.id')) organisation = db.relationship('Organisation', backref='rsrctypes') is_active = db.Column(db.Boolean, default=True) def __init__(self, code, name, type, organisation, is_active=True): self.code = code self.name = name if type: self.type_id = type.id if organisation: self.organisation_id = organisation.id self.is_active = is_active def __repr__(self): return self.name
class SubcontractorCompanies(db.Model): __tablename__ = 'subcontractor_company' id = db.Column(db.Integer, primary_key=True) subcontractor_id = db.Column(db.Integer, db.ForeignKey('subcontractors.id')) company_id = db.Column(db.Integer, db.ForeignKey('companies.id')) company = db.relationship("Company", backref="assoc_subcontractor") subcontractor = db.relationship("Subcontractor", backref="assoc") org_id = db.Column(db.Integer, db.ForeignKey('organisations.id')) organisation = db.relationship('Organisation', backref='subcontractorcompanies') is_active = db.Column(db.Boolean) __table_args__ = (db.UniqueConstraint('subcontractor_id', 'company_id', name='sc_cmp_1'), ) def __init__(self, subcontractor, company, organisation, is_active=True): self.subcontractor_id = subcontractor.id self.company_id = company.id self.org_id = organisation.id self.is_active = is_active
class Role(db.Model): __tablename__='roles' __table_args__ = {'extend_existing': True} id = db.Column(db.Integer, primary_key =True) name = db.Column(db.String(80)) organisation_id = db.Column(db.Integer, db.ForeignKey('organisations.id')) organisation = db.relationship('Organisation', backref='roles') manager_id = db.Column(db.Integer, db.ForeignKey('roles.id')) manager = db.relationship(lambda: Role, remote_side=id,backref='subs') is_active = db.Column(db.Boolean, default=True) __table_args__ = ( db.UniqueConstraint('name', 'organisation_id', name='uix_rol1'), ) def __init__(self, name, organisation, manager, is_active=True): self.name = name if organisation is not None: self.organisation_id = organisation.id if manager is not None: self.manager_id = manager.id self.is_active = is_active def __repr__(self): return self.name
class User(db.Model): __tablename__='users' id = db.Column(db.Integer, primary_key =True) firstname = db.Column(db.String(80)) lastname = db.Column(db.String(80)) email = db.Column(db.String(35), unique =True) username = db.Column(db.String(80), unique= True) password = db.Column(db.String(80)) organisation_id = db.Column(db.Integer, db.ForeignKey('organisations.id')) organisation= db.relationship('Organisation', backref='users') confirmed = db.Column(db.Boolean, nullable=False, default=False) confirmed_on = db.Column(db.DateTime, nullable=True) is_admin = db.Column(db.Boolean, default=False) is_active = db.Column(db.Boolean, default=True) def __init__(self, firstname, lastname, email, username, password, organisation, is_admin=False, is_active=True, confirmed=False, confirmed_on= None): self.firstname = firstname self.email = email self.lastname = lastname self.password = password self.is_admin = is_admin self.username = username if organisation: self.organisation_id = organisation.id self.is_active= is_active self.confirmed = confirmed self.confirmed_on = confirmed_on def get_reset_password_token(self, expires_in=600): return jwt.encode( {'reset_password': self.id, 'exp': time() + expires_in}, app.config['SECRET_KEY'], algorithm='HS256').decode('utf-8') @staticmethod def verify_reset_password_token(token): try: id = jwt.decode(token, app.config['SECRET_KEY'], algorithms=['HS256'])['reset_password'] except: return return User.query.get(id) def __repr__(self): return self.username