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.

exception DoesNotExist
exception MultipleObjectsReturned
get_comment_subclass()

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.

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

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

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

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

exception DoesNotExist
exception MultipleObjectsReturned
get_dislikers()

Get the list of the users that disliked this Comment

get_likers()

Get the list of the users that liked this Comment

get_user_vote(user)

Get a user vote (like, dislike or neutral)

get_votes(type=None)

Get the non-anonymous votes

hide_comment_by_user(user, text_hidden)

Hide a comment and save it

Parameters:
  • user – the user that hid the comment
  • text_hidden – the hide reason
Returns:

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.

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

Set of comment votes.

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

exception DoesNotExist
exception MultipleObjectsReturned
get_users()

Return all users being allowed to use this hat.

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

A hat requested by a user.

exception DoesNotExist
exception MultipleObjectsReturned
get_hat(create=False)

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

notify_member()

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

exception DoesNotExist
exception MultipleObjectsReturned
save(*args, **kwargs)

Saves the current instance. Override this in a subclass if you want to control the saving process.

The ‘force_insert’ and ‘force_update’ parameters can be used to insist that the “save” must be an SQL insert or update (or equivalent for non-SQL backends), respectively. Normally, they should not be set.

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

Publication licence.

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

Common subcategory for several concepts of the application.

exception DoesNotExist
exception MultipleObjectsReturned
get_parent_category()

Get the parent of the category.

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

Set of tags.

exception DoesNotExist
exception MultipleObjectsReturned
save(*args, **kwargs)

Saves the current instance. Override this in a subclass if you want to control the saving process.

The ‘force_insert’ and ‘force_update’ parameters can be used to insist that the “save” must be an SQL insert or update (or equivalent for non-SQL backends), respectively. Normally, they should not be set.

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)

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

Handles GET requests and instantiates a blank version of the form.

post(request, *args, **kwargs)

Handles POST requests, instantiating a form instance with the passed POST variables and then checked for validity.

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

create topic in forum

zds.utils.forums.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).

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)

zds.utils.context_processor.app_settings(request)

A context processor with all APP settings.

zds.utils.context_processor.get_version()

Retrieve version informations from zds/_version.py.

zds.utils.context_processor.version(request)

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

Utilitaires pour formulaires (forms.py)

class zds.utils.forms.CommonLayoutEditor(*args, **kwargs)
class zds.utils.forms.CommonLayoutModalText(*args, **kwargs)
class zds.utils.forms.CommonLayoutVersionEditor(*args, **kwargs)
class zds.utils.forms.TagValidator

validate tags

validate_length(tag)

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_no_empty_slug(tag)

Validate whether the tag slug is good

Parameters:tag
Returns:True if the tag slug is good
validate_raw_string(raw_string)

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.
validate_string_list(string_list)

Same as validate_raw_string but with a list of tag labels.

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

to get all internationalized error.

validate_utf8mb4(tag)

Checks the tag does not contain utf8mb4 chars.

Parameters:tag
Returns:True if no utf8mb4 string is found

Autres (misc.py)

zds.utils.misc.compute_hash(filenames)

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

zds.utils.misc.contains_utf8mb4(s)

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

zds.utils.misc.convert_camel_to_underscore(camel_case)

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.