Beispiel #1
0
class Tag(db.Model):

    __tablename__ = "tags"

    query_class = TagQuery

    id = db.Column(db.Integer, primary_key=True)
    slug = db.Column(db.Unicode(80), unique=True)
    posts = db.dynamic_loader(Post, secondary=post_tags, query_class=PostQuery)

    _name = db.Column("name", db.Unicode(80), unique=True)

    def __str__(self):
        return self.name

    def _get_name(self):
        return self._name

    def _set_name(self, name):
        self._name = name.lower().strip()
        self.slug = slugify(name)

    name = db.synonym("_name", descriptor=property(_get_name, _set_name))

    @cached_property
    def url(self):
        return url_for("frontend.tag", slug=self.slug)

    num_posts = db.column_property(
        db.select([db.func.count(post_tags.c.post_id)]).where(
            db.and_(post_tags.c.tag_id == id, Post.id == post_tags.c.post_id,
                    Post.access == Post.PUBLIC)).as_scalar())
Beispiel #2
0
    def validate_username(self, field):
        user = User.query.filter(db.and_(
                                 User.username.like(field.data),
                                 db.not_(User.id == self.user.id))).first()

        if user:
            raise ValidationError(message=gettext("This username is taken"))
Beispiel #3
0
    def validate_username(self, field):
        user = User.query.filter(
            db.and_(User.username.like(field.data),
                    db.not_(User.id == self.user.id))).first()

        if user:
            raise ValidationError(message=gettext("This username is taken"))
Beispiel #4
0
    def restricted(self, user):

        if user and user.is_moderator:
            return self

        q = self.join(Post)
        criteria = [Post.access == Post.PUBLIC]

        if user:
            criteria.append(Post.author_id == user.id)
            if user.friends:
                criteria.append(db.and_(Post.access == Post.FRIENDS, Post.author_id.in_(user.friends)))

        return q.filter(reduce(db.or_, criteria))
Beispiel #5
0
    def restricted(self, user):

        if user and user.is_moderator:
            return self
       
        q = self.join(Post)
        criteria = [Post.access==Post.PUBLIC]

        if user:
            criteria.append(Post.author_id==user.id)
            if user.friends:
                criteria.append(db.and_(Post.access==Post.FRIENDS,
                                        Post.author_id.in_(user.friends)))
        
        return q.filter(reduce(db.or_, criteria))
Beispiel #6
0
    def restricted(self, user=None):
        """
        Returns posts filtered for a) public posts b) posts authored by
        the user or c) posts authored by friends
        """

        if user and user.is_moderator:
            return self

        criteria = [Post.access == Post.PUBLIC]

        if user:
            criteria.append(Post.author_id == user.id)
            if user.friends:
                criteria.append(db.and_(Post.access == Post.FRIENDS,
                                        Post.author_id.in_(user.friends)))

        return self.filter(reduce(db.or_, criteria))
Beispiel #7
0
    def restricted(self, user=None):
        """
        Returns posts filtered for a) public posts b) posts authored by
        the user or c) posts authored by friends
        """

        if user and user.is_moderator:
            return self

        criteria = [Post.access == Post.PUBLIC]

        if user:
            criteria.append(Post.author_id == user.id)
            if user.friends:
                criteria.append(
                    db.and_(Post.access == Post.FRIENDS,
                            Post.author_id.in_(user.friends)))

        return self.filter(reduce(db.or_, criteria))
Beispiel #8
0
 def validate_email(self, field):
     user = User.query.filter(db.and_(
                              User.email.like(field.data),
                              db.not_(User.id==self.user.id))).first()
     if user:
         raise ValidationError, gettext("This email is taken")