def drop_db(): ''' Drop the database ''' DB.drop_all() print("Dropped database with URI: {0}"\ .format(current_app.config["SQLALCHEMY_DATABASE_URI"]))
def recreate_db(): ''' Recreates the database. ''' DB.drop_all() DB.create_all() DB.session.commit() print("Recreated Database with URI: {0}"\ .format(current_app.config["SQLALCHEMY_DATABASE_URI"]))
class User(DB.Model): __tablename__ = "users" id = DB.Column(DB.Integer, primary_key=True, autoincrement=True) # Umbennen in uid username = DB.Column(DB.String(128), unique=True, nullable=False) email = DB.Column(DB.String(128), unique=True, nullable=False) password = DB.Column(DB.String(255), nullable=False) admin = DB.Column(DB.Boolean, default=False, nullable=False) active = DB.Column(DB.Boolean, default=True, nullable=False) created_at = DB.Column(DB.DateTime, nullable=False) def __init__(self, username, email, password, created_at=datetime.datetime.utcnow(), admin=False): self.username = username self.email = email self.password = self.generate_hash(password) self.admin = admin self.created_at = created_at def get_dict(self): ''' Returns the users data ''' return { "id": self.id, "username": self.username, "email": self.email, "admin": self.admin, "created_at": self.created_at } @staticmethod def generate_hash(password): ''' Generates the password hash ''' return BCRYPT.generate_password_hash(password, current_app.config.get('BCRYPT_LOG_ROUNDS')).decode() @staticmethod def encode_auth_token(user_id): ''' Generates the auth token ''' payload = { 'exp': datetime.datetime.utcnow() + datetime.timedelta( days=current_app.config.get('TOKEN_EXPIRATION_DAYS'), seconds=current_app.config.get('TOKEN_EXPIRATION_SECONDS') ), 'iat': datetime.datetime.utcnow(), 'sub': user_id } return jwt.encode(payload, current_app.config.get('SECRET_BCRYPT'), algorithm='HS256') @staticmethod def decode_auth_token(auth_token): ''' Decodes the auth token ''' payload = jwt.decode(auth_token, current_app.config.get('SECRET_BCRYPT')) return payload['sub']
class Job(DB.Model): ''' Job Model ''' __tablename__ = "jobs" id = DB.Column(DB.Integer, primary_key=True, autoincrement=True) user_id = DB.Column(DB.Integer, nullable=False) task = DB.Column(DB.JSON, nullable=True) status = DB.Column(DB.String(128), nullable=False) submitted = DB.Column(DB.DateTime, nullable=False) last_update = DB.Column(DB.DateTime, nullable=False) consumed_credits = DB.Column(DB.Float, nullable=False) def __init__(self, user_id, task, status="submitted", submitted=datetime.utcnow(), last_update=datetime.utcnow(), consumed_credits=0): self.user_id = user_id self.task = task self.status = status self.submitted = submitted self.last_update = last_update self.consumed_credits = consumed_credits def get_dict(self): return { "job_id": self.id, "user_id": self.user_id, "task": self.task, "status": self.status, "submitted": self.submitted, "updated": self.last_update, "consumed_credits": self.consumed_credits } def get_small_dict(self): return { "job_id": self.id, "user_id": self.user_id, "status": self.status, "submitted": self.submitted, "updated": self.last_update, "consumed_credits": self.consumed_credits }
class Process(DB.Model): ''' Process Model ''' __tablename__ = "processes" id = DB.Column(DB.Integer, primary_key=True, autoincrement=True) user_id = DB.Column(DB.Integer, nullable=False) process_id = DB.Column(DB.String(128), nullable=False) description = DB.Column(DB.String(512), nullable=False) git_uri = DB.Column(DB.String(128), nullable=True) git_ref = DB.Column(DB.String(128), nullable=True) git_dir = DB.Column(DB.String(128), nullable=True) process_type = DB.Column(DB.String(128), nullable=False) args = DB.Column(DB.JSON, nullable=True) created_at = DB.Column(DB.DateTime, nullable=False) def __init__(self, user_id, process_id, description, process_type, git_uri=None, git_ref=None, git_dir=None, args=args, created_at=datetime.utcnow()): self.user_id = user_id self.process_id = process_id self.description = description self.git_uri = git_uri self.git_ref = git_ref self.git_dir = git_dir self.process_type = process_type self.args = args self.created_at = created_at def get_description(self): ''' Short process description ''' return {"process_id": self.process_id, "description": self.description} def get_small_dict(self): ''' Reduced view that hides sensitive information ''' return { "process_id": self.process_id, "description": self.description, "args": self.args, "created_at": self.created_at } def get_dict(self): return { "user_id": self.user_id, "process_id": self.process_id, "description": self.description, "git_uri": self.git_uri, "git_ref": self.git_ref, "git_dir": self.git_dir, "process_type": self.process_type, "args": self.args, "created_at": self.created_at }
from service import DB DB.create_all()