class BlacklistedToken(BaseModel): """ Create a blacklisted token """ __tablename__ = "blacklisted_tokens" token = db.Column(db.String(500), unique=True, nullable=False) blacklisted_on = db.Column(db.DateTime, nullable=False) @staticmethod def check_if_blacklisted(token): # check whether token has been blacklisted result = BlacklistedToken.query.filter_by(token=str(token)).first() if result: return True else: return False def __init__(self, token): self.token = token self.blacklisted_on = datetime.now() def __repr__(self): return f"<id: token: {self.token}"
class User(db.Model): """An admin user capable of viewing reports. :param str email: email address of user :param str password: encrypted password for the user """ __tablename__ = 'user' email = db.Column(db.String, primary_key=True) password = db.Column(db.String) authenticated = db.Column(db.Boolean, default=False) def is_active(self): """True, as all users are active.""" return True def get_id(self): """Return the email address to satisfy Flask-Login's requirements.""" return self.email def is_authenticated(self): """Return True if the user is authenticated.""" return self.authenticated def is_anonymous(self): """False, as anonymous users aren't supported.""" return False
class Issue(db.Model): id = db.Column(db.Integer, primary_key=True) project = db.relationship('Project', backref='issue') project_id = db.Column(db.Integer, db.ForeignKey('project.id'), nullable=False) tag = db.relationship('Tag', backref='issue') tag_id = db.Column(db.Integer, db.ForeignKey('tag.id'), nullable=True) milestone = db.relationship('Milestone', backref='issue') milestone_id = db.Column(db.Integer, db.ForeignKey('milestone.id'), nullable=True) effort = db.relationship('Effort', backref='issue') effort_id = db.Column(db.Integer, db.ForeignKey('effort.id'), nullable=True) assigned_to = db.relationship('Contact', backref='issue') assigned_to_id = db.Column(db.Integer, db.ForeignKey('contact.id'), nullable=True) column_id = db.Column(db.Integer, db.ForeignKey('column.id'), nullable=False, default=1) column = db.relationship('Column', backref='tasks') title = db.Column(db.String(120), nullable=False) description = db.Column(db.Text, nullable=True) created_at = db.Column(db.DateTime, default=db.func.now()) def __init__(self, title, description, project_id, column_id, tag_id, milestone_id, effort_id, assigned_to_id): self.title = title self.description = description self.project_id = project_id self.column_id = column_id self.tag_id = tag_id self.milestone_id = milestone_id self.effort_id = effort_id self.assigned_to_id = assigned_to_id def __repr__(self): return '<Issue %r>' % self.title
class TasksToTournamets(db.Model): id = db.Column(db.Integer, primary_key=True) tournament_id = db.Column(db.Integer, db.ForeignKey('tournament.id')) task_id = db.Column(db.Integer, db.ForeignKey('task.id')) tournament = db.relationship('Tournament', backref=db.backref('taskstotournaments', lazy='select'), lazy='subquery') task = db.relationship('Task', backref=db.backref('taskstotournaments', lazy='select'), lazy='subquery') def __init__(self, tournament_id, task_id, task, tournament): self.tournament_id = tournament_id self.task_id = task_id self.task = task self.tournament = tournament def add(self): db.session.add(self) db.session.commit() return self.id def delete(self): db.session.delete(self) db.session.commit() @staticmethod def get_all(): return TasksToTournamets.query.all()
class Organization(BaseModel): """ Creates an organization """ __tablename__ = "organizations" # attributes name = db.Column(db.String(100)) is_master = db.Column(db.Boolean, default=False, index=True) public_identifier = db.Column(db.String(8), nullable=False, index=True, unique=True) address = db.Column(db.String, nullable=True) users = db.relationship("User", backref="organization") @staticmethod def master_organisation() -> "Organization": return Organization.query.filter_by(is_master=True).first() def __init__(self, public_identifier=None, **kwargs): super(Organization, self).__init__(**kwargs) components = string.ascii_letters + string.digits identifier = "".join(random.choice(components) for i in range(8)) self.public_identifier = public_identifier or identifier
class Stocktaking(db.Model): __versioned__ = {} id = db.Column(db.Integer, primary_key=True) timestamp = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) comment = db.Column(db.Text) close_timestamp = db.Column(db.DateTime) close_user_id = db.Column(db.Integer, db.ForeignKey('user.id')) close_user = db.relationship('User', foreign_keys=[close_user_id], lazy='joined') items = db.relationship('StocktakingItem', lazy='dynamic') def __repr__(self) -> str: return '{!s} [{!s}]'.format(self.id, self.timestamp) def are_items_frozen(self) -> bool: return self.are_items_closed() def are_items_closed(self) -> bool: return self.close_user_id is not None def close_items(self, user: User): if self.close_user_id: raise RuntimeError("Items have been closed.") self.close_user_id = user.id self.close_timestamp = datetime.utcnow()
class User(db.Model): __tablename__ = "users" id = db.Column(db.Integer, primary_key=True, autoincrement=True) email = db.Column(db.String(255), unique=True, index=True, nullable=False) password = db.Column(db.String(255), nullable=False) registered_on = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) admin = db.Column(db.Boolean, nullable=False, default=False) accounts = db.relationship('Account', backref='user', lazy='dynamic') def __init__(self, email, password, admin=False): self.email = email self.password = bcrypt.generate_password_hash( password, app.config.get('BCRYPT_LOG_ROUNDS') ).decode('utf-8') self.registered_on = datetime.datetime.now() self.admin = admin def is_authenticated(self): return True def is_active(self): return True def is_anonymous(self): return False def get_id(self): return self.id def __repr__(self): return '<User {0}>'.format(self.email)
class AnalysisTbl(Base, db.Model): timestamp = db.Column(db.DateTime) aliquot = db.Column(db.Integer) increment = db.Column(db.Integer, nullable=True) analysis_type = db.Column(db.String(80)) irradiation_positionID = db.Column(db.Integer, db.ForeignKey('IrradiationPositionTbl.id')) irradiation_position = db.relationship('IrradiationPositionTbl') @property def step(self): inc = self.increment if inc is not None: s = alphas(inc) else: s = '' return s @property def runid(self): return '{}-{}{}'.format(self.irradiation_position.identifier, self.aliquot, self.step) @property def irradiation_info(self): level = self.irradiation_position.level irrad = level.irradiation return '{}{} {}'.format(irrad.name, level.name, self.irradiation_position.position)
class Account(db.Model): __tablename__ = "accounts" id = db.Column(db.Integer, primary_key=True, autoincrement=True) user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False, index=True) currency_id = db.Column(db.Integer, db.ForeignKey('currencies.id'), nullable=False, index=True) primary_account = db.Column(db.Boolean, nullable=False, default=False) created_on = db.Column(db.DateTime, nullable=False, default=datetime.datetime.utcnow) transactions = db.relationship('Transaction', backref='user', lazy='dynamic') currency = db.relationship('Currency', backref='accounts', lazy='joined') def __init__(self, user_id, currency_id, primary_account=False): self.user_id = user_id self.currency_id = currency_id self.primary_account = primary_account self.created_on = datetime.datetime.utcnow()
class BaseModel(db.Model): __abstract__ = True id = db.Column(db.Integer, primary_key=True) created_at = db.Column(db.DateTime, default=datetime.utcnow) updated_at = db.Column(db.DateTime, default=datetime.utcnow, onupdate=datetime.utcnow)
class Customer(db.Model): __versioned__ = {} id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(60), nullable=False, unique=True) def __repr__(self) -> str: return '{!s}'.format(self.name)
class Unit(db.Model): __versioned__ = {} id = db.Column(db.Integer, primary_key=True) unit = db.Column(db.String(20), nullable=False, unique=True) def __repr__(self) -> str: return '{!s}'.format(self.unit)
class IrradiationPositionTbl(Base, db.Model): identifier = db.Column(db.String(80)) position = db.Column(db.Integer) levelID = db.Column(db.Integer, db.ForeignKey('LevelTbl.id')) level = db.relationship('LevelTbl') sampleID = db.Column(db.Integer, db.ForeignKey('SampleTbl.id')) sample = db.relationship('SampleTbl')
class Vendor(db.Model): __versioned__ = {} id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(60), nullable=False, unique=True) items = db.relationship('Item', lazy='dynamic') def __repr__(self) -> str: return '{!s}'.format(self.name)
class search_index(SearchableIndex, db.Model): __tablename__ = 'search_index' __searchable__ = ['category', 'sub_category', 'description'] Id = db.Column(db.Integer, index=True, primary_key=True) category = db.Column(db.String(50), index=True) sub_category = db.Column(db.String(50), index=True) description = db.Column(db.String(500), index=True) def __repr__(self): return '<search_index {}>'.format(self.category, self.sub_category, self.description)
class Effort(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120), nullable=False) description = db.Column(db.Text, nullable=True) def __init__(self, name, description): self.name = name self.description = description def __repr__(self): return '<Effort %r>' % self.name
class PrincipalInvestigatorTbl(Base, db.Model): id = db.Column(db.Integer, primary_key=True) last_name = db.Column(db.String(120)) first_initial = db.Column(db.String(120)) @property def full_name(self): name = self.last_name if self.first_initial: name = '{}, {}'.format(name, self.first_initial) return name
class Acquisition(db.Model): __versioned__ = {} id = db.Column(db.Integer, primary_key=True) timestamp = db.Column(db.DateTime, nullable=False, default=datetime.utcnow) comment = db.Column(db.Text) items = db.relationship('AcquisitionItem', lazy='dynamic') def __repr__(self) -> str: return '{!s} [{!s}]'.format(self.id, self.timestamp)
class User(db.Model): id = db.Column(db.Integer, primary_key=True) email = db.Column(db.String(120), unique=True, nullable=False, info={'validators': Email()}) password = db.Column(db.String(80), nullable=False) posts = db.relationship('Post', backref='user', lazy='dynamic') def __init__(self, email, password): self.email = email self.password = flask_bcrypt.generate_password_hash(password) def __repr__(self): return '<User %r>' % self.email
class Column(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120), nullable=False) description = db.Column(db.Text, nullable=True) created_at = db.Column(db.DateTime, default=db.func.now()) def __init__(self, name, description): self.name = name self.description = description def __repr__(self): return '<Column %r>' % self.name
class User(db.Model): """ User Model for storing user related details """ __tablename__ = "users" id = db.Column(db.Integer, primary_key=True, autoincrement=True) name = db.Column(db.String(255), nullable=False) email = db.Column(db.String(255), unique=True, nullable=False) password = db.Column(db.String(255), nullable=False) registered_on = db.Column(db.DateTime, nullable=False) def __init__(self, name, email, password): self.name = name self.email = email self.password = bcrypt.generate_password_hash( password, app.config.get('BCRYPT_LOG_ROUNDS')).decode() self.registered_on = datetime.datetime.now() def encode_auth_token(self, user_id): """ Generates the Auth Token :return: string """ try: payload = { 'exp': datetime.datetime.utcnow() + datetime.timedelta(days=0, seconds=60), 'iat': datetime.datetime.utcnow(), 'sub': user_id } return jwt.encode(payload, app.config.get('SECRET_KEY'), algorithm='HS256') except Exception as e: return e @staticmethod def decode_auth_token(auth_token): """ Validates the auth token :param auth_token: :return: integer|string """ try: payload = jwt.decode(auth_token, app.config.get('SECRET_KEY')) return payload['sub'] except jwt.ExpiredSignatureError: return 'Signature expired. Please log in again.' except jwt.InvalidTokenError: return 'Invalid token. Please log in again.'
class Team(Contestant): id = db.Column(db.Integer, db.ForeignKey('contestant.id'), primary_key=True) name = db.Column(db.String(128)) city = db.Column(db.String(128)) invite_code = db.Column(db.String(1024)) creator_id = db.Column(db.Integer, db.ForeignKey('user.id')) creator = db.relationship('User', backref=db.backref('created_teams', lazy='dynamic'), foreign_keys='Team.creator_id') members = db.relationship('User', secondary=user_to_team, backref=db.backref('teams', lazy='select')) __mapper_args__ = { 'polymorphic_identity': 'team' } def __init__(self, name, city, invite_code, creator): self.name = name self.city = city self.invite_code = invite_code self.creator_id = creator.id self.creator = creator self.members.append(creator) def save(self): db.session.add(self) db.session.commit() return self.id def delete(self): db.session.delete(self) db.session.commit() @staticmethod def get_all(): return Team.query.all() @staticmethod def get_by_code(code): return Team.query.filter(Team.invite_code == code).first() def add_new_member(self, user): user.team_id = self.id self.members.append(user) db.session.commit() def remove_member(self, user): self.members.remove(user) db.session.commit() def if_user_in_the_team(self, user): return user in self.members @staticmethod def get_by_id(id): return db.session.query(Team).filter(Team.id == id).first()
class Tag(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120), nullable=False) description = db.Column(db.Text, nullable=True) color = db.Column(db.String(32), nullable=False, default="ffffff") def __init__(self, name, description, color): self.name = name self.description = description self.color = color def __repr__(self): return '<Tag %r>' % self.name
class Contestant(db.Model): id = db.Column(db.Integer, primary_key=True) type = db.Column(db.String(30)) __mapper_args__ = { 'polymorphic_on': type, 'polymorphic_identity': 'contestant' } def __str__(self): return self.__repr__() def __repr__(self): return "<Contestant {} {}".format(self.id, self.type)
class Currency(db.Model): __tablename__ = "currencies" id = db.Column(db.Integer, primary_key=True, autoincrement=True) category = db.Column(db.Enum(CurrencyCategory), nullable=False, index=True) code = db.Column(db.String(5), nullable=False, index=True) name = db.Column(db.String(256), nullable=False) def __init__(self, category, code, name): self.category = category self.code = code self.name = name
class Post(db.Model): id = db.Column(db.Integer, primary_key=True) title = db.Column(db.String(120), nullable=False) body = db.Column(db.Text, nullable=False) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) created_at = db.Column(db.DateTime, default=db.func.now()) def __init__(self, title, body): self.title = title self.body = body self.user_id = g.user.id def __repr__(self): return '<Post %r>' % self.title
class ToDo(db.Model): id = db.Column(db.Integer, primary_key=True) text = db.Column(db.Text, nullable=False) is_complete = db.Column(db.Boolean, nullable=False, default=False) status = db.Column(db.String(120), nullable=False, default='Active') created_at = db.Column(db.DateTime, default=db.func.now()) def __init__(self, text, is_complete, status): self.text = text self.is_complete = is_complete self.status = status def __repr__(self): return '<ToDo %r>' % self.text
class Milestone(db.Model): id = db.Column(db.Integer, primary_key=True) name = db.Column(db.String(120), nullable=False) description = db.Column(db.Text, nullable=True) due_date = db.Column(db.DateTime, default=db.func.now()) status = db.Column(db.String(120), nullable=False, default='Active') def __init__(self, name, description, due_date, status): self.name = name self.description = description self.due_date = due_date self.stats = status def __repr__(self): return '<Milestone %r>' % self.name
class Project(db.Model): id = db.Column(db.Integer, primary_key=True) user = db.relationship('Contact', backref='project') user_id = db.Column(db.Integer, db.ForeignKey('contact.id'), nullable=False) name = db.Column(db.String(120), nullable=False) description = db.Column(db.Text, nullable=True) created_at = db.Column(db.DateTime, default=db.func.now()) def __init__(self, name, description, user_id): self.name = name self.description = description self.user_id = user_id def __repr__(self): return '<Project %r>' % self.name
class Safe(db.Model): __tablename__ = 'safe' pass_id = db.Column(db.Integer, primary_key=True) username = db.Column(db.String(255), unique=True, nullable=False) password = db.Column(db.String(255)) entered_on = db.Column(db.DateTime()) user_id = db.Column(db.Integer, db.ForeignKey('user.id')) user = db.relationship('User', backref=db.backref('safe')) def __init__(self, name=None, password=None): self.username = username self.password = password self.registered_on = datetime.datetime.now()