8
0
Fork 0
mirror of https://gitlab2.federez.net/re2o/re2o synced 2024-11-22 11:23:10 +00:00

Allow ACL to be used for two instances of the same model.

This commit is contained in:
Hugo Levy-Falk 2020-04-30 19:59:42 +02:00
parent c33781b7e0
commit d8b6a7dab1
2 changed files with 12 additions and 12 deletions

View file

@ -47,8 +47,7 @@ def acl_error_message(msg, permissions):
message = msg or _("You don't have the right to edit this option.") message = msg or _("You don't have the right to edit this option.")
if groups: if groups:
return ( return (
message message + _("You need to be a member of one of these groups: %s.") % groups
+ _("You need to be a member of one of these groups: %s.") % groups
) )
else: else:
return message + _("No group has the %s permission(s)!") % " or ".join( return message + _("No group has the %s permission(s)!") % " or ".join(
@ -153,7 +152,7 @@ ModelC)
"""The wrapper used for a specific request""" """The wrapper used for a specific request"""
instances = [] instances = []
def process_target(target, fields): def process_target(target, fields, target_id=None):
"""This function calls the methods on the target and checks for """This function calls the methods on the target and checks for
the can_change_`field` method with the given fields. It also the can_change_`field` method with the given fields. It also
stores the instances of models in order to avoid duplicate DB stores the instances of models in order to avoid duplicate DB
@ -161,7 +160,7 @@ ModelC)
""" """
if on_instance: if on_instance:
try: try:
target = target.get_instance(*args, **kwargs) target = target.get_instance(target_id, *args, **kwargs)
instances.append(target) instances.append(target)
except target.DoesNotExist: except target.DoesNotExist:
yield False, _("Nonexistent entry."), [] yield False, _("Nonexistent entry."), []
@ -175,8 +174,12 @@ ModelC)
error_messages = [] error_messages = []
warning_messages = [] warning_messages = []
for target, fields in group_targets(): for arg_key, (target, fields) in zip(kwargs.keys(), group_targets()):
for can, msg, permissions in process_target(target, fields): if on_instance:
target_id = int(kwargs[arg_key])
else:
target_id = None
for can, msg, permissions in process_target(target, fields, target_id):
if not can: if not can:
error_messages.append(acl_error_message(msg, permissions)) error_messages.append(acl_error_message(msg, permissions))
elif msg: elif msg:

View file

@ -61,8 +61,7 @@ class FormRevMixin(object):
) )
elif self.changed_data: elif self.changed_data:
reversion.set_comment( reversion.set_comment(
"Field(s) edited: %s" "Field(s) edited: %s" % ", ".join(field for field in self.changed_data)
% ", ".join(field for field in self.changed_data)
) )
return super(FormRevMixin, self).save(*args, **kwargs) return super(FormRevMixin, self).save(*args, **kwargs)
@ -93,11 +92,9 @@ class AclMixin(object):
return str(cls.__module__).split(".")[0].lower() return str(cls.__module__).split(".")[0].lower()
@classmethod @classmethod
def get_instance(cls, *_args, **kwargs): def get_instance(cls, object_id, *_args, **kwargs):
"""Récupère une instance """Récupère une instance
:param objectid: Instance id à trouver
:return: Une instance de la classe évidemment""" :return: Une instance de la classe évidemment"""
object_id = kwargs.get(cls.get_classname() + "id")
return cls.objects.get(pk=object_id) return cls.objects.get(pk=object_id)
@classmethod @classmethod