Example #1
0
class User(UserMixin, SurrogatePK, Model):
    username = Column(db.String(80), unique=True, nullable=False)
    email = Column(db.String)

    password = db.Column(db.String, nullable=False)
    created_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)

    # oauth related stuff
    social_id = Column(db.String)
    first_name = Column(db.String)
    last_name = Column(db.String)

    def __init__(self,
                 username,
                 password=None,
                 email=None,
                 social_id=None,
                 first_name=None,
                 last_name=None):
        self.username = username
        self.email = email
        if password:
            self.set_password(password)
        else:
            self.password = None

        self.social_id = social_id
        self.first_name = first_name
        self.last_name = last_name

    def set_password(self, password):
        self.update(
            password=bcrypt.generate_password_hash(password).decode('utf-8'))
        return

    def check_password(self, value):
        if not self.password:
            # still do the computation
            return bcrypt.generate_password_hash(value) and False
        return bcrypt.check_password_hash(self.password, value)

    def generate_auth_token(self):
        s = Serializer(current_app.config['SECRET_KEY'])
        return s.dumps({'id': self.id})

    @staticmethod
    def verify_auth_token(token):
        if not token:
            return None
        s = Serializer(current_app.config['SECRET_KEY'])
        try:
            data = s.loads(token)
        except SignatureExpired:
            return None  # valid token, but expired
        except BadSignature:
            return None  # invalid token
        user = User.query.get(data['id'])
        return user
Example #2
0
class CarveSession(SurrogatePK, Model):
    # StatusQueried for queried carves that did not hit nodes yet
    StatusQueried = "QUERIED"
    # StatusInitialized for initialized carves
    StatusInitialized = "INITIALIZED"
    # StatusInProgress for carves that are on-going
    StatusInProgress = "IN PROGRESS"
    #  StatusCompleted for carves that finalized
    StatusCompleted = "COMPLETED"

    node_id = reference_col('node', nullable=False)
    session_id = Column(db.String, nullable=False)
    carve_guid = Column(db.String, nullable=False)

    carve_size = Column(db.Integer)
    block_size = Column(db.Integer)
    block_count = Column(db.Integer)
    completed_blocks = Column(db.Integer, default=0)
    archive = Column(db.String())

    request_id = Column(db.String, nullable=False)
    status = Column(db.String, nullable=False)

    created_at = Column(db.DateTime,
                        nullable=False,
                        default=dt.datetime.utcnow)
    updated_at = Column(db.DateTime, nullable=False)
    node = relationship('Node',
                        backref=db.backref('carve_session', lazy='dynamic'))

    def __init___(self,
                  node_id,
                  session_id=None,
                  carve_guid=None,
                  carve_size=0,
                  block_size=0,
                  block_count=0,
                  archive=None,
                  request_id=None):
        self.node_id = node_id
        self.session_id = session_id
        self.carve_guid = carve_guid
        self.carve_size = carve_size
        self.block_size = block_size
        self.block_count = block_count
        self.archive = archive
        self.request_id = request_id

    def to_dict(self):
        """Return object data in easily serializeable format"""
        return {
            'id': self.id,
            'node_id': self.node_id,
            'session_id': self.session_id,
            'carve_guid': self.carve_guid,
            'carve_size': self.carve_size,
            'block_count': self.block_count,
            'archive': self.archive,
            'created_at': dump_datetime(self.created_at),
        }