Skip to content

Commit

Permalink
[admin] Added MultitenantAdminMixin.multitenant_parent
Browse files Browse the repository at this point in the history
Allows specifying parent models which have the
organization attribute (when the current model
doesn't have it because it would be redundant).
  • Loading branch information
nemesifier committed Oct 26, 2018
1 parent e87075d commit babbd74
Showing 1 changed file with 16 additions and 4 deletions.
20 changes: 16 additions & 4 deletions openwisp_utils/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,14 @@ class MultitenantAdminMixin(object):
they are associated with.
"""
multitenant_shared_relations = []
multitenant_parent = None

def __init__(self, *args, **kwargs):
super(MultitenantAdminMixin, self).__init__(*args, **kwargs)
parent = self.multitenant_parent
shared_relations = self.multitenant_shared_relations
if parent and parent not in shared_relations:
self.multitenant_shared_relations.append(parent)

def get_repr(self, obj):
return str(obj)
Expand All @@ -22,12 +30,16 @@ def get_queryset(self, request):
objects associated to organizations he/she is associated with
"""
qs = super(MultitenantAdminMixin, self).get_queryset(request)
if not hasattr(self.model, 'organization'):
user = request.user
if user.is_superuser:
return qs
if request.user.is_superuser:
if hasattr(self.model, 'organization'):
return qs.filter(organization__in=user.organizations_pk)
elif not self.multitenant_parent:
return qs
organizations = request.user.organizations_pk
return qs.filter(organization__in=organizations)
else:
qsarg = '{0}__organization__in'.format(self.multitenant_parent)
return qs.filter(**{qsarg: user.organizations_pk})

def _edit_form(self, request, form):
"""
Expand Down

0 comments on commit babbd74

Please sign in to comment.