diff --git a/app/access/mixin.py b/app/access/mixin.py index 1b9820de..a7241303 100644 --- a/app/access/mixin.py +++ b/app/access/mixin.py @@ -16,6 +16,21 @@ class OrganizationMixin(): user_groups = [] + + def get_parent_obj(self): + """ Get the Parent Model Object + + Use in views where the the model has no organization and the organization should be fetched from the parent model. + + Requires attribute `parent_model` within the view with the value of the parent's model class + + Returns: + parent_model: with PK from kwargs['pk'] + """ + + return self.parent_model.objects.get(pk=self.kwargs['pk']) + + def object_organization(self) -> int: id = None @@ -26,7 +41,17 @@ class OrganizationMixin(): self.get_queryset() - if hasattr(self, 'get_object'): + if hasattr(self, 'parent_model'): + obj = self.get_parent_obj() + + id = obj.get_organization().id + + if obj.is_global: + + id = 0 + + + if hasattr(self, 'get_object') and id is None: obj = self.get_object() diff --git a/app/access/views/user.py b/app/access/views/user.py index f4c15467..34238d3c 100644 --- a/app/access/views/user.py +++ b/app/access/views/user.py @@ -10,6 +10,7 @@ from access.models import Team, TeamUsers class Add(OrganizationPermission, generic.CreateView): model = TeamUsers + parent_model = Team permission_required = [ 'access.view_team', 'access.add_teamusers'