From 0156952c1214ec0113b9a04a57603b192bacdc98 Mon Sep 17 00:00:00 2001 From: Hugo Levy-Falk Date: Fri, 1 May 2020 12:23:41 +0200 Subject: [PATCH] fix acl functions in topologie models inheriting from Machine --- machines/models.py | 1 + topologie/models.py | 69 +++++++++++++++++++----- topologie/templates/topologie/index.html | 2 +- 3 files changed, 59 insertions(+), 13 deletions(-) diff --git a/machines/models.py b/machines/models.py index dc8ba25c..be88fe96 100644 --- a/machines/models.py +++ b/machines/models.py @@ -130,6 +130,7 @@ class Machine(RevMixin, FieldPermissionModelMixin, AclMixin, models.Model): :param user_request: Utilisateur qui fait la requête :param userid: id de l'user dont on va créer une machine :return: soit True, soit False avec la raison de l'échec""" + raise ValueError("Now you see me.") try: user = users.models.User.objects.get(pk=userid) except users.models.User.DoesNotExist: diff --git a/topologie/models.py b/topologie/models.py index 445d70e5..a184bde4 100644 --- a/topologie/models.py +++ b/topologie/models.py @@ -135,11 +135,26 @@ class AccessPoint(Machine): def __str__(self): return str(self.interface_set.first()) + # We want to retrieve the default behaviour given by AclMixin rather + # than the one overwritten by Machine. If you are not familiar with + # the behaviour of `super`, please check https://docs.python.org/3/library/functions.html#super + @classmethod - def get_instance(cls, object_id, *_args, **kwargs): - """Récupère une instance - :return: Une instance de la classe évidemment""" - return cls.objects.get(pk=object_id) + def get_instance(cls, *args, **kwargs): + return super(Machine, cls).get_instance(*args, **kwargs) + + @classmethod + def can_create(cls, *args, **kwargs): + return super(Machine, cls).can_create(*args, **kwargs) + + def can_edit(self, *args, **kwargs): + return super(Machine, self).can_edit(*args, **kwargs) + + def can_delete(self, *args, **kwargs): + return super(Machine, self).can_delete(*args, **kwargs) + + def can_view(self, *args, **kwargs): + return super(Machine, self).can_view(*args, **kwargs) class Server(Machine): @@ -179,11 +194,26 @@ class Server(Machine): def __str__(self): return str(self.interface_set.first()) + # We want to retrieve the default behaviour given by AclMixin rather + # than the one overwritten by Machine. If you are not familiar with + # the behaviour of `super`, please check https://docs.python.org/3/library/functions.html#super + @classmethod - def get_instance(cls, object_id, *_args, **kwargs): - """Récupère une instance - :return: Une instance de la classe évidemment""" - return cls.objects.get(pk=object_id) + def get_instance(cls, *args, **kwargs): + return super(Machine, cls).get_instance(*args, **kwargs) + + @classmethod + def can_create(cls, *args, **kwargs): + return super(Machine, cls).can_create(*args, **kwargs) + + def can_edit(self, *args, **kwargs): + return super(Machine, self).can_edit(*args, **kwargs) + + def can_delete(self, *args, **kwargs): + return super(Machine, self).can_delete(*args, **kwargs) + + def can_view(self, *args, **kwargs): + return super(Machine, self).can_view(*args, **kwargs) class Switch(Machine): @@ -469,11 +499,26 @@ class Switch(Machine): def __str__(self): return str(self.get_name) + # We want to retrieve the default behaviour given by AclMixin rather + # than the one overwritten by Machine. If you are not familiar with + # the behaviour of `super`, please check https://docs.python.org/3/library/functions.html#super + @classmethod - def get_instance(cls, object_id, *_args, **kwargs): - """Récupère une instance - :return: Une instance de la classe évidemment""" - return cls.objects.get(pk=object_id) + def get_instance(cls, *args, **kwargs): + return super(Machine, cls).get_instance(*args, **kwargs) + + @classmethod + def can_create(cls, *args, **kwargs): + return super(Machine, cls).can_create(*args, **kwargs) + + def can_edit(self, *args, **kwargs): + return super(Machine, self).can_edit(*args, **kwargs) + + def can_delete(self, *args, **kwargs): + return super(Machine, self).can_delete(*args, **kwargs) + + def can_view(self, *args, **kwargs): + return super(Machine, self).can_view(*args, **kwargs) class ModelSwitch(AclMixin, RevMixin, models.Model): diff --git a/topologie/templates/topologie/index.html b/topologie/templates/topologie/index.html index 4d021779..85424b25 100644 --- a/topologie/templates/topologie/index.html +++ b/topologie/templates/topologie/index.html @@ -56,7 +56,7 @@ function toggle_graph() { - +