Skip to content
Snippets Groups Projects
Commit 906b62ff authored by Valentin Rigal's avatar Valentin Rigal
Browse files

Allow custom order_by

parent b3d272de
No related branches found
No related tags found
No related merge requests found
...@@ -21,9 +21,11 @@ class ACLMixin(object): ...@@ -21,9 +21,11 @@ class ACLMixin(object):
Access control mixin using the generic Right table. Access control mixin using the generic Right table.
""" """
_user = None _user = None
mixin_order_by_fields = ()
def __init__(self, user=None): def __init__(self, user=None, order_by_fields=()):
self._user = user self._user = user
self.mixin_order_by_fields = order_by_fields
@property @property
def user(self): def user(self):
...@@ -72,15 +74,18 @@ class ACLMixin(object): ...@@ -72,15 +74,18 @@ class ACLMixin(object):
'memberships__group__memberships__level' 'memberships__group__memberships__level'
) )
) )
# Ensure one of the right has an adequate level
queryset = queryset.filter(max_level__gte=level) # Order by decreasing max_level to make sure we keep the max among all rights
queryset = queryset.filter(max_level__gte=level) \
.order_by(*self.mixin_order_by_fields, 'id', '-max_level') \
.distinct(*self.mixin_order_by_fields, 'id')
# Use a join to add public instances as this is the more elegant solution # Use a join to add public instances as this is the more elegant solution
if include_public: if include_public:
queryset = queryset.union(self.get_public_instances(model, Role.Guest.value)) queryset = queryset.union(self.get_public_instances(model, Role.Guest.value))
# Return distinct corpus with the max right level among matching rights # Return distinct corpus with the max right level among matching rights
return queryset.order_by('id', '-max_level').distinct('id') return queryset.order_by(*self.mixin_order_by_fields, 'id')
def has_access(self, instance, level): def has_access(self, instance, level):
self._check_level(level) self._check_level(level)
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment