Les utilitaires (utils/)

Module situé dans zds/utils/. Il regroupe certaines fonctions et objets utiles à tout les autres modules.

Modèles (models.py)

class zds.utils.models.Alert(*args, **kwargs)

Alerts on all kinds of Comments and PublishedContents.


Used to retrieve comment URLs (simple call to get_absolute_url doesn’t work: objects are retrived as Comment and not subclasses) As real Comment implementation (subclasses) can’t be hard-coded due to unresolvable import loops, use InheritanceManager from django-model- utils.

solve(moderator, resolve_reason='', msg_title='', msg_content='')

Solve alert and send a PrivateTopic to the alert author if a reason is given

Parameters:resolve_reason (str) – reason
class zds.utils.models.Category(*args, **kwargs)

Common category for several concepts of the application.

class zds.utils.models.CategorySubCategory(*args, **kwargs)

ManyToMany between Category and SubCategory but save a boolean to know if category is his main category.

class zds.utils.models.Comment(*args, **kwargs)

Comment in forum, articles, tutorial, chapter, etc.


Get the list of the users that disliked this Comment


Get the list of the users that liked this Comment


Get a user vote (like, dislike or neutral)


Get the non-anonymous votes

hide_comment_by_user(user, text_hidden)

Hide a comment and save it

  • user – the user that hid the comment
  • text_hidden – the hide reason

set_user_vote(user, vote)

Set a user vote (like, dislike or neutral)

class zds.utils.models.CommentEdit(*args, **kwargs)

Archive for editing a comment.

class zds.utils.models.CommentVote(*args, **kwargs)

Set of comment votes.

class zds.utils.models.Hat(*args, **kwargs)

Hats are labels that users can add to their messages. Each member can be allowed to use several hats. A hat may also be linked to a group, which allows all members of the group to use it. It can be used for exemple to allow members to identify that a moderation message was posted by a staff member.


Return all users being allowed to use this hat.

class zds.utils.models.HatRequest(*args, **kwargs)

A hat requested by a user.


Get hat that matches this request. If it doesn’t exist, it is created or None is returned according to the create parameter.


Notify the request author about the decision that has been made.

solve(is_granted, moderator=None, comment='', hat_name=None)

Solve a hat request by granting or denying the requested hat according to the is_granted parameter.

class zds.utils.models.HelpWriting(*args, **kwargs)

Tutorial Help

class zds.utils.models.Licence(*args, **kwargs)

Publication licence.

class zds.utils.models.SubCategory(*args, **kwargs)

Common subcategory for several concepts of the application.


Get the parent of the category.

Returns:the parent category.
Return type:Category
class zds.utils.models.Tag(*args, **kwargs)

Set of tags.

zds.utils.models.get_hat_from_request(request, author=None)

Return a hat that will be used for a post. This checks that the user is allowed to use this hat.

zds.utils.models.get_hat_to_add(hat_name, user)

Return a hat that will be added to a user. This function creates the hat if it does not exist, so be sure you will need it!

zds.utils.models.image_path_category(instance, filename)

Return path to an image.

zds.utils.models.image_path_help(instance, filename)

Return path to an image.

zds.utils.models.prevent_users_getting_hat_linked_to_group(sender, instance, **kwargs)

When a hat is saved with a linked group, all users that have gotten it by another way lose it to prevent a hat from being linked to a user through their profile and one of their groups. Hat requests for this hat are also canceled.

Forums (forums.py)

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

create topic in forum


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

Messages privés (mps.py)

zds.utils.mps.send_message_mp(author, n_topic, text, send_by_mail=True, direct=False, hat=None)

Send a post in an MP. Most of the param are obvious, excepted : * direct : send a mail directly without mp (ex : ban members who wont connect again) * leave : the author leave the conversation (usefull for the bot : it wont read the response a member could send)

zds.utils.mps.send_mp(author, users, title, subtitle, text, send_by_mail=True, leave=True, direct=False, mark_as_read=False, hat=None)

Send MP at members. Most of the param are obvious, excepted : * direct : send a mail directly without mp (ex : ban members who wont connect again) * leave : the author leave the conversation (usefull for the bot : it wont read the response a member could send)

Les processeurs de contexte (context_processor.py)

La doc de Django explique le principe des context_processors comme suit :

Un processeur de contexte possède une interface très simple : ce n’est qu’une fonction Python acceptant un paramètre, un objet HttpRequest, et renvoyant un dictionnaire qui est ensuite ajouté au contexte de gabarit. Chaque processeur de contexte doit renvoyer un dictionnaire.

Les processeurs de contexte personnalisés peuvent se trouver n’importe où dans le code. Tout ce que Django demande, c’est que le réglage TEMPLATE_CONTEXT_PROCESSORS contienne le chemin vers le processeur personnalisé.

(pour plus de détails, voir la documenation de Django à ce sujet)


A context processor with all APP settings.


Get Git version information.

Same as get_git_version(), but cached with a simple timeout of one hour.


Get Git version information.

Since retrieving the Git version can be rather slow (due to I/O on the filesystem and, most importantly, forced garbage collections triggered by GitPython), you must consider using get_cached_git_version() (which behaves similarly).


A context processor to include the git version on all pages.

Utilitaires pour formulaires (forms.py)

class zds.utils.forms.TagValidator

validate tags


Check the length is in correct range. See Tag.label max length to have the true upper bound.

Parameters:tag – the tag lavel to validate
Returns:True if length is valid

Validate whether the tag slug is good

Returns:True if the tag slug is good

validate a string composed as tag1,tag2.

Parameters:raw_string (basestring) – the string to be validate. If None this is considered as a empty str.
Returns:True if raw_string is fully valid, False if at least one error appears. See self.errors to get all internationalized error.

Same as validate_raw_string but with a list of tag labels.

Returns:True if v is fully valid, False if at least one error appears. See self.errors

to get all internationalized error.


Checks the tag does not contain utf8mb4 chars.

Returns:True if no utf8mb4 string is found

Autres (misc.py)


returns a md5 hexdigest of group of files to check if they have change


This string contains at least one character of more than 3 bytes


Converts a name in camel case to underscore.

zds.utils.misc.has_changed(instance, field, manager='objects')

Returns true if a field has changed in a model May be used in a model.save() method.