mixins.py 6.76 KB
Newer Older
1 2 3 4 5
# -*- mode: python; coding: utf-8 -*-
# Re2o est un logiciel d'administration développé initiallement au rezometz. Il
# se veut agnostique au réseau considéré, de manière à être installable en
# quelques clics.
#
6
# Copyright © 2018  Gabriel Détraz
7
# Copyright © 2017  Charlie Jacomme
8 9 10 11 12 13 14 15 16 17 18 19 20 21
#
# This program is free software; you can redistribute it and/or modify
# it under the terms of the GNU General Public License as published by
# the Free Software Foundation; either version 2 of the License, or
# (at your option) any later version.
#
# This program is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
# GNU General Public License for more details.
#
# You should have received a copy of the GNU General Public License along
# with this program; if not, write to the Free Software Foundation, Inc.,
# 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
22 23 24
"""re2o.mixins
A set of mixins used all over the project to avoid duplicating code
"""
25

26
from reversion import revisions as reversion
27
from django.utils.functional import cached_property
28
from django.db import transaction
29 30 31


class RevMixin(object):
32 33 34
    """ A mixin to subclass the save and delete function of a model
    to enforce the versioning of the object before those actions
    really happen """
35
    def save(self, *args, **kwargs):
36
        """ Creates a version of this object and save it to database """
37
        if self.pk is None:
38 39 40
            with transaction.atomic(), reversion.create_revision():
                reversion.set_comment("Création")
                return super(RevMixin, self).save(*args, **kwargs)
41 42 43
        return super(RevMixin, self).save(*args, **kwargs)

    def delete(self, *args, **kwargs):
44
        """ Creates a version of this object and delete it from database """
45 46 47
        with transaction.atomic(), reversion.create_revision():
            reversion.set_comment("Suppresion")
            return super(RevMixin, self).delete(*args, **kwargs)
48 49 50


class FormRevMixin(object):
51 52
    """ A mixin to subclass the save function of a form
    to enforce the versionning of the object before it is really edited """
53
    def save(self, *args, **kwargs):
54
        """ Create a version of this object and save it to database """
55
        if reversion.get_comment() != "" and self.changed_data != []:
Maël Kervella's avatar
Maël Kervella committed
56 57 58 59
            reversion.set_comment(
                reversion.get_comment() + ",%s"
                % ', '.join(field for field in self.changed_data)
            )
60
        elif self.changed_data:
Maël Kervella's avatar
Maël Kervella committed
61 62 63 64
            reversion.set_comment(
                "Champs modifié(s) : %s"
                % ', '.join(field for field in self.changed_data)
            )
65 66 67
        return super(FormRevMixin, self).save(*args, **kwargs)


68
class AclMixin(object):
Gabriel Detraz's avatar
Gabriel Detraz committed
69 70 71
    """This mixin is used in nearly every class/models defined in re2o apps.
    It is used by acl, in models (decorators can_...) and in templates tags
    :get_instance: Applied on a class, take an id argument, return an instance
Maël Kervella's avatar
Maël Kervella committed
72 73 74 75 76 77 78 79 80 81
    :can_create: Applied on a class, take the requested user, return if the
        user can do the creation
    :can_edit: Applied on an instance, return if the user can edit the
        instance
    :can_delete: Applied on an instance, return if the user can delete the
        instance
    :can_view: Applied on an instance, return if the user can view the
        instance
    :can_view_all: Applied on a class, return if the user can view all
        instances"""
Gabriel Detraz's avatar
Gabriel Detraz committed
82

83 84
    @classmethod
    def get_classname(cls):
85
        """ Returns the name of the class where this mixin is used """
86 87 88 89
        return str(cls.__name__).lower()

    @classmethod
    def get_modulename(cls):
90
        """ Returns the name of the module where this mixin is used """
91 92
        return str(cls.__module__).split('.')[0].lower()

93
    @classmethod
94
    def get_instance(cls, *_args, **kwargs):
95 96 97 98 99 100
        """Récupère une instance
        :param objectid: Instance id à trouver
        :return: Une instance de la classe évidemment"""
        object_id = kwargs.get(cls.get_classname() + 'id')
        return cls.objects.get(pk=object_id)

101
    @classmethod
102
    def can_create(cls, user_request, *_args, **_kwargs):
103 104
        """Verifie que l'user a les bons droits pour créer
        un object
105 106
        :param user_request: instance utilisateur qui fait la requête
        :return: soit True, soit False avec la raison de l'échec"""
Maël Kervella's avatar
Maël Kervella committed
107 108 109 110 111 112
        return (
            user_request.has_perm(
                cls.get_modulename() + '.add_' + cls.get_classname()
            ),
            u"Vous n'avez pas le droit de créer un " + cls.get_classname()
        )
113

114
    def can_edit(self, user_request, *_args, **_kwargs):
115 116 117
        """Verifie que l'user a les bons droits pour editer
        cette instance
        :param self: Instance à editer
118 119
        :param user_request: Utilisateur qui fait la requête
        :return: soit True, soit False avec la raison de l'échec"""
Maël Kervella's avatar
Maël Kervella committed
120 121 122 123 124 125
        return (
            user_request.has_perm(
                self.get_modulename() + '.change_' + self.get_classname()
            ),
            u"Vous n'avez pas le droit d'éditer des " + self.get_classname()
        )
126

127
    def can_delete(self, user_request, *_args, **_kwargs):
128 129 130
        """Verifie que l'user a les bons droits pour delete
        cette instance
        :param self: Instance à delete
131 132
        :param user_request: Utilisateur qui fait la requête
        :return: soit True, soit False avec la raison de l'échec"""
Maël Kervella's avatar
Maël Kervella committed
133 134 135 136 137 138
        return (
            user_request.has_perm(
                self.get_modulename() + '.delete_' + self.get_classname()
            ),
            u"Vous n'avez pas le droit d'éditer des " + self.get_classname()
        )
139 140

    @classmethod
141
    def can_view_all(cls, user_request, *_args, **_kwargs):
142
        """Vérifie qu'on peut bien afficher l'ensemble des objets,
143 144 145
        droit particulier view objet correspondant
        :param user_request: instance user qui fait l'edition
        :return: True ou False avec la raison de l'échec le cas échéant"""
Maël Kervella's avatar
Maël Kervella committed
146 147 148 149 150 151
        return (
            user_request.has_perm(
                cls.get_modulename() + '.view_' + cls.get_classname()
            ),
            u"Vous n'avez pas le droit de voir des " + cls.get_classname()
        )
152

153
    def can_view(self, user_request, *_args, **_kwargs):
154 155
        """Vérifie qu'on peut bien voir cette instance particulière avec
        droit view objet
156
        :param self: instance à voir
157 158
        :param user_request: instance user qui fait l'edition
        :return: True ou False avec la raison de l'échec le cas échéant"""
Maël Kervella's avatar
Maël Kervella committed
159 160 161 162 163 164
        return (
            user_request.has_perm(
                self.get_modulename() + '.view_' + self.get_classname()
            ),
            u"Vous n'avez pas le droit de voir des " + self.get_classname()
        )
165 166