Esempio n. 1
0
    def is_unread(self, user, include_children=True):
        """
        Returns True if the user hasn't read the topic
        """
        if not user.is_authenticated():
            return True

        if include_children:
            topicread = TopicsRead.query.\
                filter(TopicsRead.user_id == user.id). \
                filter(TopicsRead.topic_id == Topic.id). \
                filter(Topic.forum_id.in_(get_forum_ids(self))). \
                order_by(TopicsRead.last_read.desc()). \
                first()
        else:
            topicread = TopicsRead.query.\
                filter(TopicsRead.user_id == user.id). \
                filter(TopicsRead.topic_id == Topic.id). \
                filter(Topic.forum_id == self.id). \
                order_by(TopicsRead.last_read.desc()). \
                first()

        # If no entry is found, the user hasn't read the topic
        if not topicread:
            return True
        # If the entry is older than the last post, the user hasn't read it
        if topicread.last_read < self.last_post.date_created:
            return True
        return False
Esempio n. 2
0
    def delete(self, users=None):
        """Deletes a topic with the corresponding posts. If a list with
        user objects is passed it will also update their post counts

        :param users: A list with user objects
        """
        # Grab the second last topic in the forum + parents/childs
        topic = Topic.query.\
            filter(Topic.forum_id.in_(get_forum_ids(self.forum))).\
            order_by(Topic.last_post_id.desc()).limit(2).offset(0).all()

        # check if the topic is the most recently one in this forum
        try:
            forum = self.forum
            # you want to delete the topic with the last post
            if self.id == topic[0].id:
                # Now the second last post will be the last post
                while forum is not None and not forum.is_category:
                    forum.last_post_id = topic[1].last_post_id
                    forum.save()
                    forum = forum.parent
        # Catch an IndexError when you delete the last topic in the forum
        except IndexError:
            while forum is not None and not forum.is_category:
                forum.last_post_id = 0
                forum.save()
                forum = forum.parent

        # These things needs to be stored in a variable before they are deleted
        forum = self.forum

        # Delete the topic
        db.session.delete(self)
        db.session.commit()

        # Update the post counts
        if users:
            # If someone knows a better method for this,
            # feel free to improve it :)
            for user in users:
                user.post_count = Post.query.filter_by(user_id=user.id).count()
                db.session.commit()

        while forum is not None and not forum.is_category:
            forum.topic_count -= 1
            forum.post_count -= 1

            forum = forum.parent

        db.session.commit()
        return self
Esempio n. 3
0
    def get_topic_count(self, include_children=True):
        """
        Returns the amount of topics within the current forum or it's children.
        Children can be excluded by setting the second parameter to 'false'.
        """

        if include_children:
            return Topic.query.\
                filter(Topic.forum_id.in_(get_forum_ids(self))). \
                count()
        else:
            return Topic.query.\
                filter(Topic.forum_id == self.id).\
                count()
Esempio n. 4
0
    def delete(self):
        """Deletes a post and returns self"""
        # This will delete the whole topic
        if self.topic.first_post_id == self.id:
            self.topic.delete()
            return self

        # Delete the last post
        if self.topic.last_post_id == self.id:

            # Now the second last post will be the last post
            self.topic.last_post_id = self.topic.second_last_post

            # check if the last_post is also the last post in the forum
            topic = Topic.query.\
                filter(Topic.forum_id.in_(get_forum_ids(self.topic.forum))).\
                order_by(Topic.last_post_id.desc()).first()

            if self.topic.last_post_id == topic.last_post_id:
                # Update the parent forums
                forum = self.topic.forum
                while forum is not None and not forum.is_category:
                    forum.last_post_id = self.topic.second_last_post
                    forum = forum.parent
                db.session.commit()

        # Update the post counts
        forum = self.topic.forum
        while forum is not None and not forum.is_category:
            forum.post_count -= 1
            forum = forum.parent

        self.user.post_count -= 1
        self.topic.post_count -= 1

        db.session.delete(self)
        db.session.commit()
        return self
Esempio n. 5
0
    def get_last_post(self, include_children=True):
        """
        Returns the latest post within the current forum or it's children.
        Children can be excluded by setting the second parameter to 'false'.
        """

        if include_children:
            post = Post.query.\
                filter(Post.topic_id == Topic.id). \
                filter(Topic.forum_id.in_(get_forum_ids(self))). \
                order_by(Post.date_created.desc()). \
                first()
        else:
            post = Post.query.\
                filter(Post.topic_id == Topic.id).\
                filter(Topic.forum_id == self.id).\
                order_by(Post.date_created.desc()).\
                first()

        # Load the topic and user before we cache
        post.topic
        post.user

        return post