Ejemplo n.º 1
0
class User(db.Model, UserMixin):
    __tablename__ = 'user'
    id              = db.Column(db.Integer(), primary_key=True)
    name            = db.Column(db.Unicode(255), unique=True)
    email           = db.Column(db.String(255), unique=True)
    password        = db.Column(db.String(255))
    active          = db.Column(db.Boolean())
    confirmed_at    = db.Column(db.DateTime())
    created_at      = db.Column(db.DateTime(), default=datetime.utcnow)
    models          = db.relationship('Model')
    roles           = db.relationship('Role', secondary=roles_users,
                            backref=db.backref('users', lazy='dynamic'))
Ejemplo n.º 2
0
class UserModel(db.Model):
    __tablename__ = 'users'

    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(120), nullable=False, unique=True)
    email = db.Column(db.String(120), nullable=False, unique=True)
    name = db.Column(db.String(150), nullable=False)
    password = db.Column(db.String(100), nullable=False)
    posts = db.relationship('PostModel',
                            cascade='all,delete',
                            backref='author')
    comments = db.relationship('CommentModel',
                               cascade='all,delete',
                               backref='author')
    likes = db.relationship('LikeModel', cascade='all,delete', backref='user')
    activated = db.Column(db.Boolean, default=False)

    @classmethod
    def find_by_username(cls, username):
        return cls.query.filter_by(username=username).first()

    @classmethod
    def find_by_email(cls, email):
        return cls.query.filter_by(email=email).first()

    @classmethod
    def find_by_id(cls, user_id):
        return cls.query.filter_by(id=user_id).first()

    @classmethod
    def set_password(cls, password):
        return password_hasher.hash(password)

    def send_confirmation_email(self):
        link = request.url_root[:-1] + url_for(
            'resources.users.activate_account', user_id=self.id)
        subject = 'Account Confirmation'
        text = f"Please click the link to register: {link}"
        html = f'<html>Please click the link to register: <a href="{link}"> {link}</a></html>'
        return Mailer.send(self.email, subject, text, html)

    def verify_password(self, password):
        return password_hasher.verify(self.password, password)

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()

    def delete_from_db(self):
        db.session.delete(self)
        db.session.commit()
Ejemplo n.º 3
0
class CommentModel(db.Model):

    __tablename__ = 'comments'

    id = db.Column(db.Integer, primary_key=True)
    body = db.Column(db.Text, nullable=False)
    created_date = db.Column(db.DateTime, default=datetime.now)
    author_id = db.Column(db.Integer, db.ForeignKey('users.id'))
    post_id = db.Column(db.Integer, db.ForeignKey('posts.id'))
    likes = db.relationship('LikeModel', cascade='all,delete', backref='comment')

    @classmethod
    def find_by_id(cls, comment_id):
        return cls.query.filter_by(id=comment_id).first()

    def verify_comment_author(self, user_id):
        if user_id == self.author_id:
            return True
        else:
            return False

    def save_to_db(self):
        db.session.add(self)
        db.session.commit()


    def delete_from_db(self):
        db.session.delete(self)
        db.session.commit()
Ejemplo n.º 4
0
class Owner(db.Model):
    __tablename__ = "owners"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False)
    pet = db.relationship('Pet', backref='owner', uselist=False)

    def __init__(self, name):
        self.name = name
Ejemplo n.º 5
0
class Feature(db.Model):
    __tablename__ = "feature"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False)
    ethnicity_id = db.relationship('ethnicity_id',
                                   backref='ethnicity',
                                   uselist=False)
    approval_status_id = db.relationship('approval_status_id',
                                         backref='approval_status',
                                         uselist=False)
    feature_type_id = db.relationship('feature_type_id',
                                      backref='feature_type',
                                      uselist=False)
    direction_id = db.relationship('direction_id',
                                   backref='direction',
                                   uselist=False)
    map_id = db.relationship('map_id', backref='map', uselist=False)
Ejemplo n.º 6
0
class Ethnicity(db.Model):
    __tablename__ = "ethnicity"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False)
    code = db.Column(db.String(64), nullable=False)
    continent_id = db.relationship('continent_id',
                                   backref='continent',
                                   uselist=False)
Ejemplo n.º 7
0
class Catalog(db.Model):
    __tablename__ = "catalog_detail"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False)
    pcode = db.relationship('pcode', backref='product_code', uselist=False)

    def __init__(self, pcode, name):
        self.pcode = pcode
        self.name = name
Ejemplo n.º 8
0
class FeatureType(db.Model):
    __tablename__ = "feature_types"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False)
    code = db.Column(db.String(64), nullable=False)
    feature = db.relationship('Feature', backref='feature', uselist=False)

    def __init__(self, name, code):
        self.name = name
        self.code = code
Ejemplo n.º 9
0
class Continent(db.Model):
    __tablename__ = "continents"

    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(64), nullable=False)
    code = db.Column(db.String(64), nullable=False)
    ethnicity = db.relationship('Ethnicity',
                                backref='continent',
                                uselist=False)

    def __init__(self, name, code):
        self.name = name
        self.code = code
Ejemplo n.º 10
0
class Model(db.Model):
    __tablename__   = 'model'
    query_class     = ModelQuery
    id              = db.Column(db.Integer(), primary_key=True)
    name            = db.Column(db.Unicode(255), unique=True)
    description     = db.Column(db.Unicode())
    owner           = db.relationship('User')
    owner_id        = db.Column(db.Integer(), db.ForeignKey('user.id'))
    created_at      = db.Column(db.DateTime(), default=datetime.utcnow)
    updated_at      = db.Column(db.DateTime(), default=datetime.utcnow)
    search_vector   = db.Column(TSVectorType('name', 'description'))

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

    @property
    def repo_path(self):
        return path.join(current_app.config['REPO_DIR'], self.name)

    @property
    def archive_path(self):
        return path.join(current_app.config['ARCHIVE_DIR'], self.name)

    @property
    def meta_path(self):
        return path.join(self.repo_path, 'meta.json')

    @property
    def model_path(self):
        return path.join(self.repo_path, 'model.json')

    @property
    def repo(self):
        return Repo(self.repo_path) if path.exists(self.repo_path) else None

    def register(self, user):
        """registers the model to the specified user"""
        self.owner = user
        if self.repo is None:
            self.make_repo()

    def archive(self, version=None):
        """returns path for a specific version's archive.
        if version is None, returns latest version"""
        if self.repo is None:
            raise ModelNotFoundException

        if version is None:
            version = self.latest
        if version not in self.versions:
            raise ModelNotFoundException
        return path.join(self.archive_path, '{}.tar'.format(version))

    def make_repo(self):
        """creates a new git repo for the model,
        if it doesn't already exist"""
        if path.exists(self.repo_path):
            raise ModelConflictException
        Repo.init(self.repo_path)

    @property
    def latest(self):
        """returns the latest version"""
        if not self.versions:
            return None
        return self.versions[-1]

    @property
    def versions(self):
        """all available versions"""
        repo = self.repo
        if repo is None or not repo.tags:
            return []
        return [tag.name for tag in repo.tags]

    def publish(self, meta_data, model_data, version):
        """updates a repo for the model (publishes a new version)"""
        repo = self.repo
        if repo is None:
            raise ModelNotFoundException

        # the new version must be the newest
        if self.latest is not None and LooseVersion(self.latest) >= LooseVersion(version):
            raise ModelConflictException('Published version must be newer than {}'.format(self.latest))

        with open(self.meta_path, 'w') as f:
            json.dump(meta_data, f)

        # TODO this should be PMML or something
        with open(self.model_path, 'w') as f:
            json.dump(model_data, f)

        repo.index.add(['*'])
        author = Actor(self.owner.name, self.owner.email)
        repo.index.commit(version, author=author, committer=author)
        repo.create_tag(version)
        self.description = meta_data.get('description', '')
        self.updated_at = datetime.utcnow()

    def make_archive(self, version):
        """creates a tar archive for a specific version of the model"""
        if not path.exists(self.archive_path):
            makedirs(self.archive_path)
        self.repo.archive(open(self.archive(version), 'wb'), version)

    def delete(self, version):
        """deletes a specific version"""
        if version not in self.versions:
            raise ModelNotFoundException
        remove(self.archive(version))
        self.repo.delete_tag(version)

    def destroy(self):
        """destroys the entire package"""
        shutil.rmtree(self.repo_path)
        shutil.rmtree(self.archive_path)

    @property
    def meta(self):
        """model metadata"""
        return json.load(open(self.meta_path, 'r'))