Les messages privés (mp/)

Module situé dans zds/mp/.

Modèles (models.py)

exception zds.mp.models.NotParticipatingError

Raised when trying to perform an operation requiring the user to be a participant.

exception zds.mp.models.NotReachableError

Raised when a user cannot be reached using private messages (e.g. bots).

class zds.mp.models.PrivatePost(*args, **kwargs)

A private post written by a user.

exception DoesNotExist
exception MultipleObjectsReturned
get_absolute_url()

URL of a single PrivatePost object.

Renvoie:

PrivatePost object URL

Type renvoyé:

str

get_dislikers()

Get the list of the users that disliked this PrivatePost

get_likers()

Get the list of the users that liked this PrivatePost

get_previous()

Return the previous post in the topic or “None” if the post is the first one.

get_user_vote(user)

Get a user vote (like, dislike or neutral)

get_votes()

Get the non-anonymous votes

is_author(user)

Check if the user given is the author of the message.

Paramètres:

user – Potential author of the message.

Renvoie:

true if the user is the author.

is_last_message(private_topic=None)

Check if the current message is the last one of its private topic.

Paramètres:

private_topic – Potential private topic of the message.

Renvoie:

true if the current message is the last.

set_user_vote(user, vote)

Set a user vote (like, dislike or neutral)

class zds.mp.models.PrivatePostVote(*args, **kwargs)

Set of Private Post votes.

exception DoesNotExist
exception MultipleObjectsReturned
class zds.mp.models.PrivateTopic(*args, **kwargs)

Private topic, containing private posts.

We maintain the following invariants :
  • all participants are reachable,

  • no duplicate participant.

A participant is either the author or a mere participant.

exception DoesNotExist
exception MultipleObjectsReturned
add_participant(user, silent=False)

Add a participant to the private topic. If the user is already participating, do nothing. Send the participant_added signal if successful.

Paramètres:
  • user – the user to add to the private topic

  • silent – specify if the participant_added signal should be silent (e.g. no notification)

Lève:

NotReachableError – if the user cannot receive private messages (e.g. a bot)

first_post()

Get the first answer in the PrivateTopic written by topic’s author, if exists.

Renvoie:

PrivateTopic object first answer (PrivatePost)

Type renvoyé:

PrivatePost object or None

first_unread_post(user=None)

Get the first PrivatePost the user has unread.

Paramètres:

user (User object) – The user is reading the PrivateTopic. If None, the current user is used.

Renvoie:

first PrivatePost unread

Type renvoyé:

PrivatePost object or None

get_absolute_url()

URL of a single PrivateTopic object.

Renvoie:

PrivateTopic object URL

Type renvoyé:

str

get_last_answer()

Get the last answer in the PrivateTopic written by topic’s author, if exists.

Renvoie:

PrivateTopic object last answer (PrivatePost)

Type renvoyé:

PrivatePost object or None

get_post_count()

Get the number of private posts in a single PrivateTopic object.

Renvoie:

number of posts in PrivateTopic object

Type renvoyé:

int

is_author(user)

Check if a user is the author of the private topic.

Paramètres:

user – a given user.

Renvoie:

True if the user is the author, False otherwise.

is_participant(user)

Check if a given user is participating in the private topic.

Paramètres:

user – a given user.

Renvoie:

True if the user is the author or a mere participant, False otherwise.

is_unread(user=None)

Check if a user has never read the current PrivateTopic.

Paramètres:

user (User object) – a user as Django User object. If None, the current user is used.

Renvoie:

True if the PrivateTopic was never read

Type renvoyé:

bool

last_read_post(user=None)

Get the last PrivatePost the user has read.

Paramètres:

user (User object) – The user is reading the PrivateTopic. If None, the current user is used.

Renvoie:

last PrivatePost read

Type renvoyé:

PrivatePost object or None

one_participant_remaining()

Check if there is only one participant remaining in the private topic.

Renvoie:

True if there is only one participant remaining, False otherwise.

Type renvoyé:

bool

remove_participant(user)

Remove a participant from the private topic. If the removed participant is the author, set the first mere participant as the author. If the given user is not a participant, do nothing. Send the participant_removed signal if successful.

Paramètres:

user – the user to remove from the private topic.

resolve_last_post_pk_and_pos_read_by_user(user)

Determine the primary key of position of the last post read by a user.

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(user=None)

resolve the url that leads to the last post the current user has read.

Renvoie:

the url

Type renvoyé:

str

set_as_author(user)

Set a participant as the author of the private topic.

The previous author becomes a mere participant. If the user is already the author, nothing happens.

Paramètres:

user – a given user.

Lève:

NotParticipatingError – if the user is not already participating in the private topic.

slug()

PrivateTopic doesn’t have a slug attribute of a private topic. To be compatible with older private topic, the slug is always re-calculated when we need one. :return: title slugify.

class zds.mp.models.PrivateTopicRead(*args, **kwargs)

Small model which keeps track of the user viewing private topics.

It remembers the topic he looked and what was the last private Post at this time.

exception DoesNotExist
exception MultipleObjectsReturned
zds.mp.models.filter_reachable(users)

Returns a list with only reachable users.

Paramètres:

user – a list of users

Renvoie:

list of reachable users.

zds.mp.models.is_privatetopic_unread(privatetopic, user=None)

Check if a private topic has been read by a user since it last post was added.

Paramètres:
  • privatetopic (PrivateTopic object) – a PrivateTopic to check

  • user (User object) – a user as Django User object. If None, the current user is used

Renvoie:

True if the PrivateTopic was never read

Type renvoyé:

bool

zds.mp.models.is_reachable(user)

Check if a user is reachable. Unreachable users are unable to read replies to their messages (e.g. bots).

Paramètres:

user – a given user

Renvoie:

True if the user is reachable, False otherwise.

zds.mp.models.mark_read(privatetopic, user=None)

Mark a private topic as read for the user.

Paramètres:
  • privatetopic (PrivateTopic object) – a PrivateTopic to check

  • user (User object) – a user as Django User object. If None, the current user is used

Renvoie:

nothing is returned

Type renvoyé:

None

Vues (views.py)

class zds.mp.views.PrivatePostAnswer(**kwargs)

Create a post to answer in a private topic.

form_class

alias de PrivatePostForm

form_valid(form)

If the form is valid, save the associated model.

get_form(form_class=<class 'zds.mp.forms.PrivatePostForm'>)

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

model

alias de PrivateTopic

model_quote

alias de PrivatePost

class zds.mp.views.PrivatePostEdit(**kwargs)

Edit a post in a private topic.

form_class

alias de PrivatePostForm

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.mp.forms.PrivatePostForm'>)

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

alias de PrivatePost

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.mp.views.PrivatePostList(**kwargs)

Display a private topic and its posts using a pager.

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_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.mp.views.PrivatePostUnread(**kwargs)

Mark a private post as not read.

get(request, *args, **kwargs)

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

model

alias de PrivatePost

static perform_unread_private_post(post, user)

Mark the private post as unread.

class zds.mp.views.PrivateTopicAddParticipant(**kwargs)

Add a participant to a private topic.

model

alias de PrivateTopic

class zds.mp.views.PrivateTopicEdit(**kwargs)

Edit a private topic.

form_class

alias de PrivateTopicEditForm

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

alias de PrivateTopic

class zds.mp.views.PrivateTopicLeaveDetail(**kwargs)

Leave a private topic.

model

alias de PrivateTopic

class zds.mp.views.PrivateTopicLeaveList(**kwargs)

Leave a list of private topics.

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.mp.views.PrivateTopicList(**kwargs)

Display the list of private topics of a member.

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.mp.views.PrivateTopicNew(**kwargs)

Create a new private topic.

form_class

alias de PrivateTopicForm

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.mp.forms.PrivateTopicForm'>)

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.

Les utilitaires (utils.py)

Ce module ajoute des fonctions utiles permettant de gérer l’ajout et la réponse à des messages privés.

Il est important d’utiliser ces méthodes pour réaliser ces actions sous peine d’oublier des actions.

Ces méthodes permettent un lien direct avec le module « notifications ».

zds.mp.utils.send_message_mp(author, n_topic, text, send_by_mail=True, force_email=False, hat=None, no_notification_for=None)

Send a post in an MP.

Paramètres:
  • author – sender of the private message

  • n_topic – topic in which it will be sent

  • text – content of the message

  • send_by_mail – if True, also notify by email

  • force_email – if True, send email even if the user has not enabled email notifications

  • hat – hat attached to the message

  • no_notification_for – list of participants who won’t be notified of the message

zds.mp.utils.send_mp(author, users, title, subtitle, text, send_by_mail=True, force_email=False, leave=True, hat=None, automatically_read=None)

Send a private message in a new private topic.

Paramètres:
  • author – sender of the message and author of the private topic

  • users – list of users receiving the message (participants of the private topic)

  • title – title of the private topic

  • subtitle – subtitle of the private topic

  • text – content of the private message

  • send_by_mail – if True, also notify by email

  • force_email – if True, send email even if the user has not enabled email notifications

  • leave – if True, do not add the sender to the topic

  • hat – hat with which to send the private message

  • automatically_read – a user or a list of users that will automatically be marked as having read of the mp

Lève:

UnreachableUserError