Les forums (forum/)

Module situé dans zds/forum/.

Modèles (models.py)

class zds.forum.models.Category(*args, **kwargs)

A Category is a simple container for Forums. There is no kind of logic in a Category. It simply here for Forum presentation in a predefined order.

get_forums(user, with_count=False)

get all forums that user can access

Paramètres:
  • user (User) – the related user
  • with_count (bool) – If true will preload thread and post number for each forum of this category
Retourne:

All forums in category, ordered by forum’s position in category

Type retourné:

list[Forum]

class zds.forum.models.Forum(*args, **kwargs)

A Forum, containing Topics. It can be public or restricted to some groups.

can_read(user)

Checks if an user can read current forum. The forum can be read if: - The forum has no access restriction (= no group), or - the user is in our database and is part of the restricted group which is needed to access this forum :param user: the user to check the rights :return: True if the user can read this forum, False otherwise.

get_last_message()
Retourne:the last message on the forum, if there are any.
get_post_count()

Retrieve or agregate the number of posts in this forum. If this number already exists, it must be stored in post_count. Otherwise it will process a SQL query

Retourne:the number of posts for a forum.
get_topic_count()

Retrieve or agregate the number of threads in this forum. If this number already exists, it must be stored in thread_count. Otherwise it will process a SQL query

Retourne:the number of threads in the forum.
has_group

Checks if this forum belongs to at least one group

Retourne:True if it belongs to at least one group
Type retourné:bool
class zds.forum.models.Post(*args, **kwargs)

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.

get_absolute_url()
Retourne:the absolute URL for this post, including page in the topic.
classmethod get_es_django_indexable(force_reindexing=False)

Overridden to prefetch stuffs

get_es_document_source(excluded_fields=None)

Overridden to handle the information of the topic

hide_comment_by_user(user, text_hidden)

Overridden to directly hide the post in ES as well

class zds.forum.models.Topic(*args, **kwargs)

A Topic is a thread of posts. A topic has several states, witch are all independent: - Solved: it was a question, and this question has been answered. The “solved” state is set at author’s discretion. - Locked: none can write on a locked topic. - Sticky: sticky topics are displayed on top of topic lists (ex: on forum page).

add_tags(tag_collection)

Add all tags contained in tag_collection to this topic. If a tag is unknown, it is added to the system. :param tag_collection: A collection of tags.

antispam(user=None)

Check if the user is allowed to post in a topic according to the ZDS_APP[‘forum’][‘spam_limit_seconds’] value. The user can always post if someone else has posted last. If the user is the last poster and there is less than ZDS_APP[‘forum’][‘spam_limit_seconds’] since the last post, the anti-spam is active and the user cannot post. :param user: An user. If undefined, the current user is used. :return: True if the anti-spam is active (user can’t post), False otherwise.

first_post()
Retourne:the first post of a topic, written by topic’s author.
first_unread_post(user=None)

Returns the first post of this topics the current user has never read, or the first post if it has never read this topic. Used in notification menu.

Retourne:The first unread post for this topic and this user.
classmethod get_es_django_indexable(force_reindexing=False)

Overridden to prefetch tags and forum

get_es_document_source(excluded_fields=None)

Overridden to handle the case of tags (M2M field)

get_last_answer()

Gets the last answer in this tread, if any. Note the first post is not considered as an answer, therefore a topic with a single post (the 1st one) will return None. :return: the last answer in the thread, if any.

get_last_post()
Retourne:the last post in the thread.
get_post_count()
Retourne:the number of posts in the topic.
last_read_post()

Returns the last post the current user has read in this topic. If it has never read this topic, returns the first post. Used in “last read post” balloon (base.html line 91). :return: the last post the user has read.

old_post_warning()

Check if the last message was written a long time ago according to ZDS_APP[‘forum’][‘old_post_limit_days’] value.

Retourne:True if the post is old (users are warned), False otherwise.
resolve_last_post_pk_and_pos_read_by_user(user)

get the primary key and position of the last post the user read

Paramètres:user – the current (authenticated) user. Please do not try with unauthenticated user, il would lead to a useless request.
Retourne:the primary key
Type retourné:int
resolve_last_read_post_absolute_url()

resolve the url that leads to the last post the current user has read. If current user is anonymous, just lead to the thread start.

Retourne:the url
Type retourné:str
save(*args, **kwargs)

Overridden to handle the displacement of the topic to another forum

class zds.forum.models.TopicRead(*args, **kwargs)

This model tracks the last post read in a topic by a user. Technically it is a simple joint [user, topic, last read post].

zds.forum.models.delete_post_in_elasticsearch(sender, instance, **kwargs)

catch the pre_delete signal to ensure the deletion in ES

zds.forum.models.delete_topic_in_elasticsearch(sender, instance, **kwargs)

catch the pre_delete signal to ensure the deletion in ES

zds.forum.models.is_read(topic, user=None)

Checks if the user has read the last post of the topic. Returns false if the user read the topic except its last post. Technically this is done by checking if the user has a TopicRead object for the last post of this topic. :param topic: A topic :param user: A user. If undefined, the current user is used. :return:

zds.forum.models.mark_read(topic, user=None)

Mark the last message of a topic as read for the current user. :param topic: A topic.

Les managers (managers.py)

class zds.forum.managers.ForumManager

Custom forum manager.

get_public_forums_of_category(category, with_count=False)

load all public forums for a category

Paramètres:
  • category (zds.forum.models.Category) – the related category
  • with_count (bool) – optional parameter: if true, will preload thread and post number for each forum inside category
class zds.forum.managers.PostManager

Custom post manager.

visibility_check_query(current_user)

Build a subquery that checks if a post is readable by current user :param current_user: :return:

class zds.forum.managers.TopicManager

Custom topic manager.

get_last_topics()

Get last posted topics and prefetch some related properties. Depends on settings.ZDS_APP[‘topic’][‘home_number’] :return: :rtype: django.models.Queryset

last_topics_of_a_member(author, user)

Gets last topics of a member but exclude all topics not accessible for the request user. :param author: Author of topics. :param user: Request user. :return: List of topics.

visibility_check_query(current_user)

Build a subquery that checks if a topic is readable by current user :param current_user: :return: