Skip to content

Commit 8dc2154

Browse files
authored
Merge pull request #18704 from alehaa/18095-inherit-contacts
Fixes 18095: inherit contacts
2 parents ae5314f + effc23f commit 8dc2154

File tree

2 files changed

+23
-14
lines changed

2 files changed

+23
-14
lines changed

netbox/netbox/models/features.py

+21
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
from django.contrib.contenttypes.fields import GenericRelation
66
from django.core.validators import ValidationError
77
from django.db import models
8+
from django.db.models import Q
89
from django.utils import timezone
910
from django.utils.translation import gettext_lazy as _
1011
from taggit.managers import TaggableManager
@@ -363,6 +364,26 @@ class ContactsMixin(models.Model):
363364
class Meta:
364365
abstract = True
365366

367+
def get_contacts(self, inherited=True):
368+
"""
369+
Return a `QuerySet` matching all contacts assigned to this object.
370+
371+
:param inherited: If `True`, inherited contacts from parent objects are included.
372+
"""
373+
from tenancy.models import ContactAssignment
374+
from . import NestedGroupModel
375+
376+
filter = Q(
377+
object_type=ObjectType.objects.get_for_model(self),
378+
object_id__in=(
379+
self.get_ancestors(include_self=True)
380+
if (isinstance(self, NestedGroupModel) and inherited)
381+
else [self.pk]
382+
),
383+
)
384+
385+
return ContactAssignment.objects.filter(filter)
386+
366387

367388
class BookmarksMixin(models.Model):
368389
"""

netbox/tenancy/views.py

+2-14
Original file line numberDiff line numberDiff line change
@@ -17,25 +17,13 @@ class ObjectContactsView(generic.ObjectChildrenView):
1717
template_name = 'tenancy/object_contacts.html'
1818
tab = ViewTab(
1919
label=_('Contacts'),
20-
badge=lambda obj: obj.contacts.count(),
20+
badge=lambda obj: obj.get_contacts().count(),
2121
permission='tenancy.view_contactassignment',
2222
weight=5000
2323
)
2424

2525
def get_children(self, request, parent):
26-
return ContactAssignment.objects.restrict(request.user, 'view').filter(
27-
object_type=ContentType.objects.get_for_model(parent),
28-
object_id=parent.pk
29-
).order_by('priority', 'contact', 'role')
30-
31-
def get_table(self, *args, **kwargs):
32-
table = super().get_table(*args, **kwargs)
33-
34-
# Hide object columns
35-
table.columns.hide('object_type')
36-
table.columns.hide('object')
37-
38-
return table
26+
return parent.get_contacts().restrict(request.user, 'view').order_by('priority', 'contact', 'role')
3927

4028

4129
#

0 commit comments

Comments
 (0)