Les forums (forum/)

Module situé dans zds/forum/.

Modèles (models.py)

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

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

exception DoesNotExist
exception MultipleObjectsReturned
can_read(user)

Checks if a 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()
Renvoie:

the last message on the forum, if there are any.

get_post_count()

Retrieve or aggregate 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.

Renvoie:

the number of posts for a forum.

get_topic_count()

Retrieve or aggregate 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.

Renvoie:

the number of threads in the forum.

property has_group

Checks if this forum belongs to at least one group

Renvoie:

True if it belongs to at least one group

Type renvoyé:

bool

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

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

exception DoesNotExist
exception MultipleObjectsReturned
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

Renvoie:

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

Type renvoyé:

list[Forum]

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

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.

exception DoesNotExist
exception MultipleObjectsReturned
get_absolute_url()
Renvoie:

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

classmethod get_es_mapping()

Overridden to add pk into mapping.

Renvoie:

mapping object

Type renvoyé:

elasticsearch_dsl.Mapping

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).

exception DoesNotExist
exception MultipleObjectsReturned
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: A user. If undefined, the current user is used. :return: True if the anti-spam is active (user can’t post), False otherwise.

first_post()
Renvoie:

the first post of a topic, written by topic’s author.

first_unread_post(user: User | None = 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.

Paramètres:

user – The user who potentially has read a post. If None will get request user

Renvoie:

The first unread post for this topic and this user. If the topic was read, gets last_post or None if no posts was found after OP.

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)

classmethod get_es_mapping()

Overridden to add pk into mapping.

Renvoie:

mapping object

Type renvoyé:

elasticsearch_dsl.Mapping

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. :rtype: Post

get_last_post()
Renvoie:

the last post in the thread.

get_post_count()
Renvoie:

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.

Renvoie:

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.

Renvoie:

the primary key

Type renvoyé:

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.

Renvoie:

the url

Type renvoyé:

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].

exception DoesNotExist
exception MultipleObjectsReturned
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.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(*args, **kwargs)

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.ForumCategory) – 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(*args, **kwargs)

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(*args, **kwargs)

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:

Vues (views.py)

class zds.forum.views.CategoriesForumsListView(**kwargs)
get_context_data(**kwargs)

Get the context for this view.

class zds.forum.views.FindFollowedTopic(**kwargs)
get_context_data(**kwargs)

Get the context for this view. This method is surcharged to modify the paginator and information given at the template.

get_object(queryset=None)

Return the object the view is displaying.

Require self.queryset and a pk or slug argument in the URLconf. Subclasses can override this to return any object.

get_queryset()

Return the list of items for this view.

The return value must be an iterable and may be an instance of QuerySet in which case QuerySet specific behavior will be enabled.

class zds.forum.views.FindPost(**kwargs)
get_context_data(**kwargs)

Get the context for this view. This method is surcharged to modify the paginator and information given at the template.

get_object(queryset=None)

Return the object the view is displaying.

Require self.queryset and a pk or slug argument in the URLconf. Subclasses can override this to return any object.

get_queryset()

Return the list of items for this view.

The return value must be an iterable and may be an instance of QuerySet in which case QuerySet specific behavior will be enabled.

class zds.forum.views.FindTopic(**kwargs)
get_context_data(**kwargs)

Get the context for this view. This method is surcharged to modify the paginator and information given at the template.

get_object(queryset=None)

Return the object the view is displaying.

Require self.queryset and a pk or slug argument in the URLconf. Subclasses can override this to return any object.

get_queryset()

Return the list of items for this view.

The return value must be an iterable and may be an instance of QuerySet in which case QuerySet specific behavior will be enabled.

class zds.forum.views.FindTopicByTag(**kwargs)
filter_queryset(queryset, filter_param)

Filter the queryset queryset, given the selected filter_param. Default implementation does no filtering at all.

get_context_data(*args, **kwargs)

Get the context for this view. This method is surcharged to modify the paginator and information given at the template.

get_object(queryset=None)

Return the object the view is displaying.

Require self.queryset and a pk or slug argument in the URLconf. Subclasses can override this to return any object.

get_queryset()

Return the list of items for this view.

The return value must be an iterable and may be an instance of QuerySet in which case QuerySet specific behavior will be enabled.

class zds.forum.views.ForumCategoryForumsDetailView(**kwargs)
get_context_data(**kwargs)

Insert the single object into the context dict.

class zds.forum.views.ForumTopicsListView(**kwargs)
filter_queryset(queryset, filter_param)

Filter the queryset queryset, given the selected filter_param. Default implementation does no filtering at all.

get(request, *args, **kwargs)

Handle GET requests: instantiate a blank version of the form.

get_context_data(**kwargs)

Get the context for this view. This method is surcharged to modify the paginator and information given at the template.

get_object(queryset=None)

Return the object the view is displaying.

Require self.queryset and a pk or slug argument in the URLconf. Subclasses can override this to return any object.

get_queryset()

Return the list of items for this view.

The return value must be an iterable and may be an instance of QuerySet in which case QuerySet specific behavior will be enabled.

post(request, *args, **kwargs)

Handle POST requests: instantiate a form instance with the passed POST variables and then check if it’s valid.

class zds.forum.views.LastTopicsListView(**kwargs)
get_context_data(**kwargs)

Get the context for this view.

get_queryset()

Return the list of items for this view.

The return value must be an iterable and may be an instance of QuerySet in which case QuerySet specific behavior will be enabled.

class zds.forum.views.ManageGitHubIssue(**kwargs)
post(request, *args, **kwargs)

Handle POST requests: instantiate a form instance with the passed POST variables and then check if it’s valid.

class zds.forum.views.PostEdit(**kwargs)
form_class

alias de PostForm

form_valid(form)

If the form is valid, save the associated model.

get(request, *args, **kwargs)

Handle GET requests: instantiate a blank version of the form.

get_form(form_class=<class 'zds.forum.forms.PostForm'>)

Return an instance of the form to be used in this view.

post(request, *args, **kwargs)

Handle POST requests: instantiate a form instance with the passed POST variables and then check if it’s valid.

class zds.forum.views.PostNew(**kwargs)
form_class

alias de PostForm

form_valid(form)

If the form is valid, save the associated model.

get_form(form_class=<class 'zds.forum.forms.PostForm'>)

Return an instance of the form to be used in this view.

get_object(queryset=None)

Return the object the view is displaying.

Require self.queryset and a pk or slug argument in the URLconf. Subclasses can override this to return any object.

model_quote

alias de Post

class zds.forum.views.PostSignal(**kwargs)
post(request, *args, **kwargs)

Handle POST requests: instantiate a form instance with the passed POST variables and then check if it’s valid.

class zds.forum.views.PostUnread(**kwargs)
get(request, *args, **kwargs)

Handle GET requests: instantiate a blank version of the form.

class zds.forum.views.PostUseful(**kwargs)
post(request, *args, **kwargs)

Handle POST requests: instantiate a form instance with the passed POST variables and then check if it’s valid.

class zds.forum.views.TopicEdit(**kwargs)
featured_request_allowed()

override this function if you want to add extra condition for a request to be allowed (or not)

Type renvoyé:

bool

form_class

alias de TopicForm

form_valid(form)

If the form is valid, save the associated model.

get(request, *args, **kwargs)

Handle GET requests: instantiate a blank version of the form.

get_form(form_class=<class 'zds.forum.forms.TopicForm'>)

Return an instance of the form to be used in this view.

get_object(queryset=None)

Return the object the view is displaying.

Require self.queryset and a pk or slug argument in the URLconf. Subclasses can override this to return any object.

post(request, *args, **kwargs)

Handle POST requests: instantiate a form instance with the passed POST variables and then check if it’s valid.

class zds.forum.views.TopicNew(**kwargs)
form_class

alias de TopicForm

form_valid(form)

If the form is valid, save the associated model.

get(request, *args, **kwargs)

Handle GET requests: instantiate a blank version of the form.

get_form(form_class=<class 'zds.forum.forms.TopicForm'>)

Return an instance of the form to be used in this view.

get_object(queryset=None)

Return the object the view is displaying.

Require self.queryset and a pk or slug argument in the URLconf. Subclasses can override this to return any object.

post(request, *args, **kwargs)

Handle POST requests: instantiate a form instance with the passed POST variables and then check if it’s valid.

class zds.forum.views.TopicPostsListView(**kwargs)
featured_request_allowed()

override this function if you want to add extra condition for a request to be allowed (or not)

Type renvoyé:

bool

get_context_data(**kwargs)

Get the context for this view. This method is surcharged to modify the paginator and information given at the template.

get_object(queryset=None)

Return the object the view is displaying.

Require self.queryset and a pk or slug argument in the URLconf. Subclasses can override this to return any object.

get_queryset()

Return the list of items for this view.

The return value must be an iterable and may be an instance of QuerySet in which case QuerySet specific behavior will be enabled.

Les utilitaires (utils.py)

class zds.forum.utils.CreatePostView(**kwargs)
get(request, *args, **kwargs)

Handle GET requests: instantiate a blank version of the form.

post(request, *args, **kwargs)

Handle POST requests: instantiate a form instance with the passed POST variables and then check if it’s valid.

zds.forum.utils.create_topic(request, author, forum, title, subtitle, text, tags='', related_publishable_content=None)

create topic in forum

zds.forum.utils.get_authorized_forums_pk(user)

Find forums the user is allowed to visit.

Paramètres:

user – concerned user.

Renvoie:

pk of authorized forums

zds.forum.utils.get_tag_by_title(title)

Extract tags from title. In a title, tags can be set this way: > [Tag 1][Tag 2] There is the real title Rules to detect tags: - Tags are enclosed in square brackets. This allows multi-word tags instead of hashtags. - Tags can embed square brackets: [Tag] is a valid tag and must be written [[Tag]] in the raw title - All tags must be declared at the beginning of the title. Example: _ »Title [tag] »_ will not create a tag. - Tags and title correctness (example: empty tag/title detection) is not checked here :param title: The raw title :return: A tuple: (the tag list, the title without the tags).