Пример #1
0
class Post(Comment):
    """
    A forum post written by an user.
    A post can be marked as useful: topic's author (or admin) can declare any topic as "useful", and this post is
    displayed as is on front.
    """

    topic = models.ForeignKey(Topic, verbose_name='Sujet', db_index=True)

    is_useful = models.BooleanField('Est utile', default=False)
    objects = PostManager()

    def __str__(self):
        return '<Post pour "{0}", #{1}>'.format(self.topic, self.pk)

    def get_absolute_url(self):
        """
        :return: the absolute URL for this post, including page in the topic.
        """
        page = int(ceil(float(self.position) / settings.ZDS_APP['forum']['posts_per_page']))

        return '{0}?page={1}#p{2}'.format(
            self.topic.get_absolute_url(),
            page,
            self.pk)

    def get_notification_title(self):
        return self.topic.title
Пример #2
0
class Post(Comment, AbstractESDjangoIndexable):
    """
    A forum post written by an user.
    A post can be marked as useful: topic's author (or admin) can declare any topic as "useful", and this post is
    displayed as is on front.
    """
    objects_per_batch = 2000

    topic = models.ForeignKey(Topic, verbose_name='Sujet', db_index=True)

    is_useful = models.BooleanField('Est utile', default=False)
    objects = PostManager()

    def __str__(self):
        return "<Post pour '{0}', #{1}>".format(self.topic, self.pk)

    def get_absolute_url(self):
        """
        :return: the absolute URL for this post, including page in the topic.
        """
        page = int(
            ceil(
                float(self.position) /
                settings.ZDS_APP['forum']['posts_per_page']))

        return '{0}?page={1}#p{2}'.format(self.topic.get_absolute_url(), page,
                                          self.pk)

    def get_notification_title(self):
        return self.topic.title

    @classmethod
    def get_es_mapping(cls):
        es_mapping = super(Post, cls).get_es_mapping()

        es_mapping.field('text_html', Text())
        es_mapping.field('is_useful', Boolean())
        es_mapping.field('is_visible', Boolean())
        es_mapping.field('position', Integer())
        es_mapping.field('like_dislike_ratio', Float())
        es_mapping.field('pubdate', Date())
        es_mapping.field('forum_pk', Integer())
        es_mapping.field('topic_pk', Integer())

        # not indexed:
        es_mapping.field('get_absolute_url', Keyword(index=False))
        es_mapping.field('topic_title', Text(index=False))
        es_mapping.field('forum_title', Text(index=False))
        es_mapping.field('forum_get_absolute_url', Keyword(index=False))

        return es_mapping

    @classmethod
    def get_es_django_indexable(cls, force_reindexing=False):
        """Overridden to prefetch stuffs
        """

        q = super(Post, cls).get_es_django_indexable(force_reindexing)\
            .prefetch_related('topic')\
            .prefetch_related('topic__forum')

        return q

    def get_es_document_source(self, excluded_fields=None):
        """Overridden to handle the information of the topic
        """

        excluded_fields = excluded_fields or []
        excluded_fields.extend([
            'like_dislike_ratio', 'topic_title', 'topic_pk', 'forum_title',
            'forum_pk', 'forum_get_absolute_url'
        ])

        data = super(
            Post, self).get_es_document_source(excluded_fields=excluded_fields)

        data['like_dislike_ratio'] = \
            (self.like / self.dislike) if self.dislike != 0 else self.like if self.like != 0 else 1

        data['topic_pk'] = self.topic.pk
        data['topic_title'] = self.topic.title

        data['forum_pk'] = self.topic.forum.pk
        data['forum_title'] = self.topic.forum.title
        data['forum_get_absolute_url'] = self.topic.forum.get_absolute_url()

        return data

    def hide_comment_by_user(self, user, text_hidden):
        """Overridden to directly hide the post in ES as well
        """

        super(Post, self).hide_comment_by_user(user, text_hidden)

        index_manager = ESIndexManager(**settings.ES_SEARCH_INDEX)
        index_manager.update_single_document(self, {'is_visible': False})
Пример #3
0
class Post(Comment, AbstractESDjangoIndexable):
    """
    A forum post written by a user.
    A post can be marked as useful: topic's author (or admin) can declare any topic as "useful", and this post is
    displayed as is on front.
    """

    objects_per_batch = 2000

    topic = models.ForeignKey(Topic,
                              verbose_name="Sujet",
                              db_index=True,
                              on_delete=models.CASCADE)

    is_useful = models.BooleanField("Est utile", default=False)
    objects = PostManager()

    def __str__(self):
        return f"<Post pour '{self.topic}', #{self.pk}>"

    def get_absolute_url(self):
        """
        :return: the absolute URL for this post, including page in the topic.
        """
        page = int(
            ceil(
                float(self.position) /
                settings.ZDS_APP["forum"]["posts_per_page"]))

        return f"{self.topic.get_absolute_url()}?page={page}#p{self.pk}"

    def get_notification_title(self):
        return self.topic.title

    @classmethod
    def get_es_mapping(cls):
        es_mapping = super().get_es_mapping()

        es_mapping.field("text_html", Text())
        es_mapping.field("is_useful", Boolean())
        es_mapping.field("is_visible", Boolean())
        es_mapping.field("position", Integer())
        es_mapping.field("like_dislike_ratio", Float())
        es_mapping.field("pubdate", Date())
        es_mapping.field("forum_pk", Integer())
        es_mapping.field("topic_pk", Integer())

        # not indexed:
        es_mapping.field("get_absolute_url", Keyword(index=False))
        es_mapping.field("topic_title", Text(index=False))
        es_mapping.field("forum_title", Text(index=False))
        es_mapping.field("forum_get_absolute_url", Keyword(index=False))

        return es_mapping

    @classmethod
    def get_es_django_indexable(cls, force_reindexing=False):
        """Overridden to prefetch stuffs"""

        q = super().get_es_django_indexable(force_reindexing).prefetch_related(
            "topic").prefetch_related("topic__forum")

        return q

    def get_es_document_source(self, excluded_fields=None):
        """Overridden to handle the information of the topic"""

        excluded_fields = excluded_fields or []
        excluded_fields.extend([
            "like_dislike_ratio", "topic_title", "topic_pk", "forum_title",
            "forum_pk", "forum_get_absolute_url"
        ])

        data = super().get_es_document_source(excluded_fields=excluded_fields)

        data["like_dislike_ratio"] = ((self.like /
                                       self.dislike) if self.dislike != 0 else
                                      self.like if self.like != 0 else 1)

        data["topic_pk"] = self.topic.pk
        data["topic_title"] = self.topic.title

        data["forum_pk"] = self.topic.forum.pk
        data["forum_title"] = self.topic.forum.title
        data["forum_get_absolute_url"] = self.topic.forum.get_absolute_url()

        return data

    def hide_comment_by_user(self, user, text_hidden):
        """Overridden to directly hide the post in ES as well"""

        super().hide_comment_by_user(user, text_hidden)

        index_manager = ESIndexManager(**settings.ES_SEARCH_INDEX)
        index_manager.update_single_document(self, {"is_visible": False})