2018-03-28 01:52:07 +00:00
|
|
|
# -*- 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.
|
|
|
|
#
|
2018-03-28 15:15:29 +00:00
|
|
|
# Copyright © 2018 Gabriel Détraz
|
2018-03-31 15:18:39 +00:00
|
|
|
# Copyright © 2017 Charlie Jacomme
|
2018-03-28 01:52:07 +00:00
|
|
|
#
|
|
|
|
# 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.
|
2018-04-14 19:29:16 +00:00
|
|
|
"""re2o.mixins
|
|
|
|
A set of mixins used all over the project to avoid duplicating code
|
|
|
|
"""
|
2018-03-28 01:52:07 +00:00
|
|
|
|
2018-03-31 15:18:39 +00:00
|
|
|
from reversion import revisions as reversion
|
2018-04-29 16:34:05 +00:00
|
|
|
from django.db import transaction
|
2018-06-23 17:53:10 +00:00
|
|
|
from django.utils.translation import ugettext as _
|
2018-03-31 15:18:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class RevMixin(object):
|
2018-04-14 19:29:16 +00:00
|
|
|
""" A mixin to subclass the save and delete function of a model
|
|
|
|
to enforce the versioning of the object before those actions
|
|
|
|
really happen """
|
2018-03-31 15:18:39 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2018-04-14 19:29:16 +00:00
|
|
|
""" Creates a version of this object and save it to database """
|
2018-03-31 15:18:39 +00:00
|
|
|
if self.pk is None:
|
2018-04-29 16:34:05 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
2018-06-23 17:53:10 +00:00
|
|
|
reversion.set_comment("Creation")
|
2018-04-29 16:34:05 +00:00
|
|
|
return super(RevMixin, self).save(*args, **kwargs)
|
2018-03-31 15:18:39 +00:00
|
|
|
return super(RevMixin, self).save(*args, **kwargs)
|
|
|
|
|
|
|
|
def delete(self, *args, **kwargs):
|
2018-04-14 19:29:16 +00:00
|
|
|
""" Creates a version of this object and delete it from database """
|
2018-04-29 16:34:05 +00:00
|
|
|
with transaction.atomic(), reversion.create_revision():
|
2018-06-23 17:53:10 +00:00
|
|
|
reversion.set_comment("Deletion")
|
2018-04-29 16:34:05 +00:00
|
|
|
return super(RevMixin, self).delete(*args, **kwargs)
|
2018-03-31 15:18:39 +00:00
|
|
|
|
|
|
|
|
|
|
|
class FormRevMixin(object):
|
2018-04-14 19:29:16 +00:00
|
|
|
""" A mixin to subclass the save function of a form
|
|
|
|
to enforce the versionning of the object before it is really edited """
|
2018-03-31 15:18:39 +00:00
|
|
|
def save(self, *args, **kwargs):
|
2018-04-14 19:29:16 +00:00
|
|
|
""" Create a version of this object and save it to database """
|
2018-03-31 15:18:39 +00:00
|
|
|
if reversion.get_comment() != "" and self.changed_data != []:
|
2018-04-13 23:42:22 +00:00
|
|
|
reversion.set_comment(
|
|
|
|
reversion.get_comment() + ",%s"
|
|
|
|
% ', '.join(field for field in self.changed_data)
|
|
|
|
)
|
2018-03-31 16:10:24 +00:00
|
|
|
elif self.changed_data:
|
2018-04-13 23:42:22 +00:00
|
|
|
reversion.set_comment(
|
2018-06-23 17:53:10 +00:00
|
|
|
"Field(s) altered : %s"
|
2018-04-13 23:42:22 +00:00
|
|
|
% ', '.join(field for field in self.changed_data)
|
|
|
|
)
|
2018-03-31 15:18:39 +00:00
|
|
|
return super(FormRevMixin, self).save(*args, **kwargs)
|
|
|
|
|
|
|
|
|
2018-03-28 01:52:07 +00:00
|
|
|
class AclMixin(object):
|
2018-03-29 12:55:57 +00:00
|
|
|
"""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
|
2018-04-13 23:42:22 +00:00
|
|
|
: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"""
|
2018-03-29 12:55:57 +00:00
|
|
|
|
2018-03-28 01:52:07 +00:00
|
|
|
@classmethod
|
|
|
|
def get_classname(cls):
|
2018-04-14 19:29:16 +00:00
|
|
|
""" Returns the name of the class where this mixin is used """
|
2018-03-28 01:52:07 +00:00
|
|
|
return str(cls.__name__).lower()
|
|
|
|
|
|
|
|
@classmethod
|
|
|
|
def get_modulename(cls):
|
2018-04-14 19:29:16 +00:00
|
|
|
""" Returns the name of the module where this mixin is used """
|
2018-03-28 01:52:07 +00:00
|
|
|
return str(cls.__module__).split('.')[0].lower()
|
|
|
|
|
2018-03-28 15:15:29 +00:00
|
|
|
@classmethod
|
2018-04-14 19:29:16 +00:00
|
|
|
def get_instance(cls, *_args, **kwargs):
|
2018-03-28 15:15:29 +00:00
|
|
|
"""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)
|
|
|
|
|
2018-03-28 01:52:07 +00:00
|
|
|
@classmethod
|
2018-04-14 19:29:16 +00:00
|
|
|
def can_create(cls, user_request, *_args, **_kwargs):
|
2018-03-29 02:35:23 +00:00
|
|
|
"""Verifie que l'user a les bons droits pour créer
|
|
|
|
un object
|
2018-03-28 01:52:07 +00:00
|
|
|
:param user_request: instance utilisateur qui fait la requête
|
|
|
|
:return: soit True, soit False avec la raison de l'échec"""
|
2018-04-13 23:42:22 +00:00
|
|
|
return (
|
|
|
|
user_request.has_perm(
|
|
|
|
cls.get_modulename() + '.add_' + cls.get_classname()
|
|
|
|
),
|
2018-06-23 17:53:10 +00:00
|
|
|
(_("You don't have the right to create a %s object.")
|
|
|
|
% cls.get_classname())
|
2018-04-13 23:42:22 +00:00
|
|
|
)
|
2018-03-28 01:52:07 +00:00
|
|
|
|
2018-04-14 19:29:16 +00:00
|
|
|
def can_edit(self, user_request, *_args, **_kwargs):
|
2018-03-29 02:35:23 +00:00
|
|
|
"""Verifie que l'user a les bons droits pour editer
|
|
|
|
cette instance
|
|
|
|
:param self: Instance à editer
|
2018-03-28 01:52:07 +00:00
|
|
|
:param user_request: Utilisateur qui fait la requête
|
|
|
|
:return: soit True, soit False avec la raison de l'échec"""
|
2018-04-13 23:42:22 +00:00
|
|
|
return (
|
|
|
|
user_request.has_perm(
|
|
|
|
self.get_modulename() + '.change_' + self.get_classname()
|
|
|
|
),
|
2018-06-23 17:53:10 +00:00
|
|
|
(_("You don't have the right to edit a %s object.")
|
|
|
|
% self.get_classname())
|
2018-04-13 23:42:22 +00:00
|
|
|
)
|
2018-03-28 01:52:07 +00:00
|
|
|
|
2018-04-14 19:29:16 +00:00
|
|
|
def can_delete(self, user_request, *_args, **_kwargs):
|
2018-03-29 02:35:23 +00:00
|
|
|
"""Verifie que l'user a les bons droits pour delete
|
|
|
|
cette instance
|
|
|
|
:param self: Instance à delete
|
2018-03-28 01:52:07 +00:00
|
|
|
:param user_request: Utilisateur qui fait la requête
|
|
|
|
:return: soit True, soit False avec la raison de l'échec"""
|
2018-04-13 23:42:22 +00:00
|
|
|
return (
|
|
|
|
user_request.has_perm(
|
|
|
|
self.get_modulename() + '.delete_' + self.get_classname()
|
|
|
|
),
|
2018-06-23 17:53:10 +00:00
|
|
|
(_("You don't have the right to delete a %s object.")
|
|
|
|
% self.get_classname())
|
2018-04-13 23:42:22 +00:00
|
|
|
)
|
2018-03-28 01:52:07 +00:00
|
|
|
|
|
|
|
@classmethod
|
2018-04-14 19:29:16 +00:00
|
|
|
def can_view_all(cls, user_request, *_args, **_kwargs):
|
2018-03-29 02:35:23 +00:00
|
|
|
"""Vérifie qu'on peut bien afficher l'ensemble des objets,
|
2018-03-28 01:52:07 +00:00
|
|
|
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"""
|
2018-04-13 23:42:22 +00:00
|
|
|
return (
|
|
|
|
user_request.has_perm(
|
|
|
|
cls.get_modulename() + '.view_' + cls.get_classname()
|
|
|
|
),
|
2018-06-23 17:53:10 +00:00
|
|
|
(_("You don't have the right to view every %s object.")
|
|
|
|
% cls.get_classname())
|
2018-04-13 23:42:22 +00:00
|
|
|
)
|
2018-03-28 01:52:07 +00:00
|
|
|
|
2018-04-14 19:29:16 +00:00
|
|
|
def can_view(self, user_request, *_args, **_kwargs):
|
2018-03-28 01:52:07 +00:00
|
|
|
"""Vérifie qu'on peut bien voir cette instance particulière avec
|
|
|
|
droit view objet
|
2018-03-29 02:35:23 +00:00
|
|
|
:param self: instance à voir
|
2018-03-28 01:52:07 +00:00
|
|
|
:param user_request: instance user qui fait l'edition
|
|
|
|
:return: True ou False avec la raison de l'échec le cas échéant"""
|
2018-04-13 23:42:22 +00:00
|
|
|
return (
|
|
|
|
user_request.has_perm(
|
|
|
|
self.get_modulename() + '.view_' + self.get_classname()
|
|
|
|
),
|
2018-06-23 17:53:10 +00:00
|
|
|
(_("You don't have the right to view a %s object.")
|
|
|
|
% self.get_classname())
|
2018-04-13 23:42:22 +00:00
|
|
|
)
|
2018-06-23 17:53:10 +00:00
|
|
|
|