Beispiel #1
0
class Script(db.Model):
    """Script Model"""
    id = db.Column(db.GUID(), default=uuid.uuid4, primary_key=True, autoincrement=False)
    name = db.Column(db.String(120), nullable=False)
    slug = db.Column(db.String(80), unique=True, nullable=False)
    description = db.Column(db.Text(), default='')
    created_at = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
    updated_at = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
    user_id = db.Column(db.GUID(), db.ForeignKey('user.id'))
    status = db.Column(db.String(80), nullable=False, default='PENDING')
    logs = db.relationship('ScriptLog',
                           backref=db.backref('script'),
                           cascade='all, delete-orphan',
                           lazy='dynamic')
    executions = db.relationship('Execution',
                                 backref=db.backref('script'),
                                 cascade='all, delete-orphan',
                                 lazy='dynamic')
    public = db.Column(db.Boolean(), default=False, nullable=False)

    def __init__(self, name, slug, user_id):
        self.name = name
        self.slug = slug
        self.user_id = user_id

    def __repr__(self):
        return '<Script %r>' % self.name

    def serialize(self, include=None):
        """Return object data in easily serializeable format"""
        include = include if include else []
        script = {
            'id': self.id,
            'name': self.name,
            'slug': self.slug,
            'description': self.description,
            'created_at': self.created_at.isoformat(),
            'updated_at': self.updated_at.isoformat(),
            'user_id': self.user_id,
            'status': self.status,
            'public': self.public or False
        }
        if 'logs' in include:
            script['logs'] = self.serialize_logs
        if 'user' in include:
            script['user'] = self.user.serialize()
        if 'executions' in include:
            script['executions'] = self.serialize_executions
        return script

    @property
    def serialize_logs(self):
        """Serialize Logs"""
        return [item.serialize() for item in self.logs]

    @property
    def serialize_executions(self):
        """Serialize Logs"""
        return [item.serialize() for item in self.executions]
class ExecutionLog(db.Model):
    """ExecutionLog Model"""
    __tablename__ = 'execution_log'
    id = db.Column(db.Integer(), primary_key=True)
    text = db.Column(db.Text())
    level = db.Column(db.String(80), nullable=False, default='DEBUG')
    register_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
    execution_id = db.Column(db.GUID(), db.ForeignKey('execution.id'))

    def __init__(self, text, level, execution_id):
        self.text = text
        self.level = level
        self.execution_id = execution_id

    def __repr__(self):
        return '<ExecutionLog %r>' % self.id

    def serialize(self):
        """Return object data in easily serializeable format"""
        return {
            'id': self.id,
            'text': self.text,
            'level': self.level,
            'register_date': self.register_date.isoformat(),
            'execution_id': self.execution_id
        }
Beispiel #3
0
class Execution(db.Model):
    """Execution Model"""
    id = db.Column(db.GUID(),
                   default=uuid.uuid4,
                   primary_key=True,
                   autoincrement=False)
    start_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
    end_date = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
    status = db.Column(db.String(10), default='PENDING')
    progress = db.Column(db.Integer(), default=0)
    params = db.Column(JSONB, default={})
    results = db.Column(JSONB, default={})
    logs = db.relationship('ExecutionLog',
                           backref=db.backref('execution'),
                           cascade='all, delete-orphan',
                           lazy='dynamic')
    script_id = db.Column(db.GUID(), db.ForeignKey('script.id'))
    user_id = db.Column(db.GUID(), db.ForeignKey('user.id'))

    def __init__(self, script_id, params, user_id):
        self.script_id = script_id
        self.params = params
        self.user_id = user_id

    def __repr__(self):
        return '<Execution %r>' % self.id

    def serialize(self, include=None):
        """Return object data in easily serializeable format"""
        include = include if include else []
        end_date_formatted = None
        if self.end_date:
            end_date_formatted = self.end_date.isoformat()
        execution = {
            'id': self.id,
            'script_id': self.script_id,
            'user_id': self.user_id,
            'start_date': self.start_date.isoformat(),
            'end_date': end_date_formatted,
            'status': self.status,
            'progress': self.progress,
            'params': self.params,
            'results': self.results,
        }
        if 'logs' in include:
            execution['logs'] = self.serialize_logs
        if 'user' in include:
            execution['user'] = self.user.serialize()
        if 'script' in include:
            execution['script'] = self.script.serialize()
        return execution

    @property
    def serialize_logs(self):
        """Serialize Logs"""
        return [item.serialize() for item in self.logs]
Beispiel #4
0
class User(db.Model):
    """User Model"""
    id = db.Column(db.GUID(),
                   default=uuid.uuid4,
                   primary_key=True,
                   autoincrement=False)
    email = db.Column(db.String(120), unique=True, nullable=False)
    name = db.Column(db.String(120), nullable=False)
    country = db.Column(db.String(120))
    institution = db.Column(db.String(120))
    password = db.Column(db.String(200), nullable=False)
    created_at = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
    updated_at = db.Column(db.DateTime(), default=datetime.datetime.utcnow)
    role = db.Column(db.String(10))
    scripts = db.relationship('Script',
                              backref=db.backref('user'),
                              cascade='all, delete-orphan',
                              lazy='dynamic')
    executions = db.relationship('Execution',
                                 backref=db.backref('user'),
                                 cascade='all, delete-orphan',
                                 lazy='dynamic')

    def __init__(self,
                 email,
                 password,
                 name,
                 country,
                 institution,
                 role='USER'):
        self.email = email
        self.password = self.set_password(password=password)
        self.role = role
        self.name = name
        self.country = country
        self.institution = institution

    def __repr__(self):
        return '<User %r>' % self.email

    def serialize(self, include=None):
        """Return object data in easily serializeable format"""
        include = include if include else []
        user = {
            'id': self.id,
            'email': self.email,
            'created_at': self.created_at.isoformat(),
            'updated_at': self.updated_at.isoformat(),
            'role': self.role,
            'name': self.name,
            'country': self.country,
            'institution': self.institution
        }
        if 'scripts' in include:
            user['scripts'] = self.serialize_scripts
        return user

    @property
    def serialize_scripts(self):
        """Serialize Scripts"""
        return [item.serialize() for item in self.scripts]

    def set_password(self, password):
        return generate_password_hash(password)

    def check_password(self, password):
        return check_password_hash(self.password, password)