Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fixes 18095: inherit contacts #18704

Merged
merged 5 commits into from
Feb 25, 2025
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
21 changes: 21 additions & 0 deletions netbox/netbox/models/features.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,6 +5,7 @@
from django.contrib.contenttypes.fields import GenericRelation
from django.core.validators import ValidationError
from django.db import models
from django.db.models import Q
from django.utils import timezone
from django.utils.translation import gettext_lazy as _
from taggit.managers import TaggableManager
Expand Down Expand Up @@ -363,6 +364,26 @@ class ContactsMixin(models.Model):
class Meta:
abstract = True

def get_contacts(self, inherited=True):
"""
Return a `QuerySet` matching all contacts assigned to this object.

:param inherited: If `True`, inherited contacts from parent objects are included.
"""
from tenancy.models import ContactAssignment
from . import NestedGroupModel

filter = Q(
object_type=ObjectType.objects.get_for_model(self),
object_id__in=(
self.get_ancestors(include_self=True).values_list('pk', flat=True)
if (isinstance(self, NestedGroupModel) and inherited)
else [self.pk]
),
)

return ContactAssignment.objects.filter(filter)


class BookmarksMixin(models.Model):
"""
Expand Down
16 changes: 2 additions & 14 deletions netbox/tenancy/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -17,25 +17,13 @@ class ObjectContactsView(generic.ObjectChildrenView):
template_name = 'tenancy/object_contacts.html'
tab = ViewTab(
label=_('Contacts'),
badge=lambda obj: obj.contacts.count(),
badge=lambda obj: obj.get_contacts().count(),
permission='tenancy.view_contactassignment',
weight=5000
)

def get_children(self, request, parent):
return ContactAssignment.objects.restrict(request.user, 'view').filter(
object_type=ContentType.objects.get_for_model(parent),
object_id=parent.pk
).order_by('priority', 'contact', 'role')

def get_table(self, *args, **kwargs):
table = super().get_table(*args, **kwargs)

# Hide object columns
table.columns.hide('object_type')
table.columns.hide('object')

return table
return parent.get_contacts().restrict(request.user, 'view').order_by('priority', 'contact', 'role')


#
Expand Down