mirror of
https://gitlab2.federez.net/re2o/re2o
synced 2024-11-05 01:16:27 +00:00
Allow ACL to be used for two instances of the same model.
This commit is contained in:
parent
d4df4b242f
commit
02a83a77ce
2 changed files with 12 additions and 12 deletions
17
re2o/acl.py
17
re2o/acl.py
|
@ -47,8 +47,7 @@ def acl_error_message(msg, permissions):
|
|||
message = msg or _("You don't have the right to edit this option.")
|
||||
if groups:
|
||||
return (
|
||||
message
|
||||
+ _("You need to be a member of one of these groups: %s.") % groups
|
||||
message + _("You need to be a member of one of these groups: %s.") % groups
|
||||
)
|
||||
else:
|
||||
return message + _("No group has the %s permission(s)!") % " or ".join(
|
||||
|
@ -124,7 +123,7 @@ ModelC)
|
|||
```
|
||||
The view will be called like this:
|
||||
```
|
||||
view(request, instance_of_A, instance_of_b, *args, **kwargs)
|
||||
view(request, instance_of_A, instance_of_b, *args, **kwargs)
|
||||
```
|
||||
where `*args` and `**kwargs` are the original view arguments.
|
||||
"""
|
||||
|
@ -153,7 +152,7 @@ ModelC)
|
|||
"""The wrapper used for a specific request"""
|
||||
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
|
||||
the can_change_`field` method with the given fields. It also
|
||||
stores the instances of models in order to avoid duplicate DB
|
||||
|
@ -161,7 +160,7 @@ ModelC)
|
|||
"""
|
||||
if on_instance:
|
||||
try:
|
||||
target = target.get_instance(*args, **kwargs)
|
||||
target = target.get_instance(target_id, *args, **kwargs)
|
||||
instances.append(target)
|
||||
except target.DoesNotExist:
|
||||
yield False, _("Nonexistent entry."), []
|
||||
|
@ -175,8 +174,12 @@ ModelC)
|
|||
|
||||
error_messages = []
|
||||
warning_messages = []
|
||||
for target, fields in group_targets():
|
||||
for can, msg, permissions in process_target(target, fields):
|
||||
for arg_key, (target, fields) in zip(kwargs.keys(), group_targets()):
|
||||
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:
|
||||
error_messages.append(acl_error_message(msg, permissions))
|
||||
elif msg:
|
||||
|
|
|
@ -61,8 +61,7 @@ class FormRevMixin(object):
|
|||
)
|
||||
elif self.changed_data:
|
||||
reversion.set_comment(
|
||||
"Field(s) edited: %s"
|
||||
% ", ".join(field for field in self.changed_data)
|
||||
"Field(s) edited: %s" % ", ".join(field for field in self.changed_data)
|
||||
)
|
||||
return super(FormRevMixin, self).save(*args, **kwargs)
|
||||
|
||||
|
@ -93,11 +92,9 @@ class AclMixin(object):
|
|||
return str(cls.__module__).split(".")[0].lower()
|
||||
|
||||
@classmethod
|
||||
def get_instance(cls, *_args, **kwargs):
|
||||
def get_instance(cls, object_id, *_args, **kwargs):
|
||||
"""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)
|
||||
|
||||
@classmethod
|
||||
|
|
Loading…
Reference in a new issue