Ejemplo n.º 1
0
class Tag(db.Model):
    __tablename__ = 'tags'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(100))
    slug = db.Column(db.String(), index=True)
    description = db.Column(db.String())
    created_at = db.Column(db.DateTime(), default=datetime.utcnow, index=True)
    updated_at = db.Column(db.DateTime())

    def __repr__(self):
        return self.name
Ejemplo n.º 2
0
class Article(db.Model):
    __tablename__ = 'articles'

    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(100), nullable=False)
    description = db.Column(db.Text, nullable=False)
    slug = db.Column(db.Text, unique=True)
    body = db.Column(db.Text)

    created_at = db.Column(db.DateTime(), default=datetime.utcnow, index=True)
    updated_at = db.Column(db.DateTime(), onupdate=datetime.utcnow)
    publish_on = db.Column(db.DateTime,
                           index=True,
                           default=datetime.utcnow,
                           nullable=False)

    user_id = db.Column(db.Integer, db.ForeignKey('users.id'), nullable=False)
    user = relationship('User', backref=db.backref('articles'))
    '''
    liked_by = relationship(
        'user',
        secondary=likesModel,
        backref='likes',
        lazy='dynamic')
    '''

    tags = relationship('Tag', secondary=articles_tags, backref='articles')

    categories = relationship('Category',
                              secondary=articles_categories,
                              backref='articles')

    # comments = relationship('Comment', backref='article', lazy='dynamic')

    def add_tag(self, tag):
        if tag not in self.tags:
            self.tags.append(tag)
            return True
        return False

    def remove_tag(self, tag):
        if tag in self.tags:
            self.tags.remove(tag)
            return True
        return False

    def __repr__(self):
        return '<Article %r>' % self.body

    def __str__(self):
        return '<Article {}>'.format(self.body)
Ejemplo n.º 3
0
class User(db.Model):
    __tablename__ = 'users'
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(80), unique=True, nullable=False)
    email = db.Column(db.String(100), unique=True, nullable=False)
    password = db.Column(db.Binary(128), nullable=True)
    created_at = db.Column(db.DateTime,
                           nullable=False,
                           default=datetime.utcnow)
    updated_at = db.Column(db.DateTime,
                           nullable=False,
                           default=datetime.utcnow)
    first_name = db.Column(db.String(300), nullable=False)
    last_name = db.Column(db.String(300), nullable=False)

    roles = db.relationship('Role', secondary=users_roles, backref='users')
    '''
    articles = db.relationship('Article',
                               foreign_keys='articles.user_id',
                               backref='user', lazy='dynamic')
    
    comments = db.relationship('Comment',
                               foreign_keys='comments.user_id',
                               backref='user', lazy='dynamic')
    '''
    '''
        following = db.relationship(
            'User', secondary=user_subscriptions,
            primaryjoin=(user_subscriptions.c.follower_id == id),
            secondaryjoin=(user_subscriptions.c.following_id == id),
            backref=db.backref('followers', lazy='dynamic'), lazy='dynamic')
    '''
    def __repr__(self):
        return '<User {}>'.format(self.username)

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

    def follow(self, user):
        if not self.is_following(user):
            self.followed.append(user)

    def unfollow(self, user):
        if self.is_following(user):
            self.following.remove(user)

    def is_following(self, user):
        return self.followed.filter(
            user_subscriptions.c.followed_id == user.id).count() > 0

    def is_admin_or_author(self):
        return db.session.query(
            User.id).filter(((User.roles.any(name='ROLE_AUTHOR')) |
                             (User.roles.any(name='ROLE_ADMIN')))
                            & (User.id == self.id)).count() > 0
        # User.query.filter(User.roles.any(name='ROLE_ADMIN')).all()
        # User.query.join(User.roles).filter_by(genre_id=genre.id).all()

    def is_admin(self):
        return 'ROLE_ADMIN' in [r.name for r in self.roles]

    def is_not_admin(self):
        return not self.is_admin()

    def is_author(self):
        return 'ROLE_AUTHOR' in [r.name for r in self.roles]

    def is_not_author(self):
        return not self.is_author()
Ejemplo n.º 4
0
class Role(db.Model):
    __tablename__ = 'roles'
    id = db.Column(db.Integer, primary_key=True)
    name = db.Column(db.String(80), unique=True, nullable=False)
    description = db.Column(db.String(100), nullable=True)