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 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 Permission(db.Model): __tablename__ = "permissions" id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(80)) is_active = db.Column(db.Boolean) def __init__(self, name, is_active=True): self.name = name self.is_active = is_active def __repr__(self): return self.name
class Organisation(db.Model): __tablename__ ="organisations" id = db.Column(db.Integer, primary_key=True) code = db.Column(db.String(80)) name = db.Column(db.String(80)) description = db.Column(db.Text) is_active = db.Column(db.Boolean) __table_args__ = ( db.UniqueConstraint('code', 'name', name='uix_org1'), ) def __init__(self, code, name, description, is_active=True): self.code = code self.name = name self.description = description self.is_active = is_active def __repr__(self): return self.name
class UserLog(db.Model): __tablename__='users_log' id = db.Column(db.Integer, primary_key=True) user_id = db.Column(db.Integer) date = db.Column(db.DateTime) success = db.Column(db.Boolean) ipaddr = db.Column(db.String(40)) def __init__(self, user_id, success, ipaddr, date=None): self.user_id = user_id self.date = datetime.now() self.success = success self.ipaddr = ipaddr def new_messages(self): last_read_time = self.last_message_read_time or datetime(1900, 1, 1) return Message.query.filter_by(recipient=self, read=False).count() def __repr__(self): return 'user:'******' logged on: ' + str(self.date)
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 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 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 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 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 Task(db.Model): __tablename__ = "tasks" id = db.Column(db.Integer, primary_key=True) task_code = db.Column(db.String(80)) task_name = db.Column(db.String(255)) early_end_date = db.Column(db.DateTime) early_start_date = db.Column(db.DateTime) remain_drtn_hr_cnt = db.Column(db.Integer) target_drtn_hr_cnt = db.Column(db.Integer) def __init__(self, task_code, task_name, early_end_date, early_start_date, remain_drtn_hr_cnt, target_drtn_hr_cnt): self.task_code = task_code self.task_name = task_name self.early_end_date = early_end_date self.early_start_date = early_start_date self.remain_drtn_hr_cnt = remain_drtn_hr_cnt self.target_drtn_hr_cnt = target_drtn_hr_cnt
class RSRCType(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)) description = db.Column(db.Text) 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, description, organisation, is_active=True): self.code = code self.name = name self.description = description if organisation: self.organisation_id = organisation.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