mirror of
https://github.com/netbox-community/netbox.git
synced 2025-07-16 12:12:53 -06:00
Move contact queryset into model
This commit is contained in:
parent
701f40e2a8
commit
d5316de9c8
@ -5,6 +5,7 @@ from functools import cached_property
|
|||||||
from django.contrib.contenttypes.fields import GenericRelation
|
from django.contrib.contenttypes.fields import GenericRelation
|
||||||
from django.core.validators import ValidationError
|
from django.core.validators import ValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
|
from django.db.models import Q
|
||||||
from django.utils import timezone
|
from django.utils import timezone
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
from taggit.managers import TaggableManager
|
from taggit.managers import TaggableManager
|
||||||
@ -363,6 +364,26 @@ class ContactsMixin(models.Model):
|
|||||||
class Meta:
|
class Meta:
|
||||||
abstract = True
|
abstract = True
|
||||||
|
|
||||||
|
def get_contacts(self):
|
||||||
|
"""
|
||||||
|
Return a `QuerySet` matching all contacts assigned to this object.
|
||||||
|
"""
|
||||||
|
from tenancy.models import ContactAssignment
|
||||||
|
from . import NestedGroupModel
|
||||||
|
|
||||||
|
filter = Q()
|
||||||
|
for obj in [self]:
|
||||||
|
filter |= Q(
|
||||||
|
object_type=ObjectType.objects.get_for_model(obj),
|
||||||
|
object_id__in=(
|
||||||
|
obj.get_ancestors(include_self=True).values_list('pk', flat=True)
|
||||||
|
if isinstance(obj, NestedGroupModel)
|
||||||
|
else [obj.pk]
|
||||||
|
),
|
||||||
|
)
|
||||||
|
|
||||||
|
return ContactAssignment.objects.filter(filter)
|
||||||
|
|
||||||
|
|
||||||
class BookmarksMixin(models.Model):
|
class BookmarksMixin(models.Model):
|
||||||
"""
|
"""
|
||||||
|
@ -2,7 +2,6 @@ from django.contrib.contenttypes.models import ContentType
|
|||||||
from django.shortcuts import get_object_or_404
|
from django.shortcuts import get_object_or_404
|
||||||
from django.utils.translation import gettext_lazy as _
|
from django.utils.translation import gettext_lazy as _
|
||||||
|
|
||||||
from netbox.models import NestedGroupModel
|
|
||||||
from netbox.views import generic
|
from netbox.views import generic
|
||||||
from utilities.query import count_related
|
from utilities.query import count_related
|
||||||
from utilities.views import GetRelatedModelsMixin, ViewTab, register_model_view
|
from utilities.views import GetRelatedModelsMixin, ViewTab, register_model_view
|
||||||
@ -18,24 +17,13 @@ class ObjectContactsView(generic.ObjectChildrenView):
|
|||||||
template_name = 'tenancy/object_contacts.html'
|
template_name = 'tenancy/object_contacts.html'
|
||||||
tab = ViewTab(
|
tab = ViewTab(
|
||||||
label=_('Contacts'),
|
label=_('Contacts'),
|
||||||
badge=lambda obj: obj.contacts.count(),
|
badge=lambda obj: obj.get_contacts().count(),
|
||||||
permission='tenancy.view_contactassignment',
|
permission='tenancy.view_contactassignment',
|
||||||
weight=5000
|
weight=5000
|
||||||
)
|
)
|
||||||
|
|
||||||
def get_children(self, request, parent):
|
def get_children(self, request, parent):
|
||||||
qs = ContactAssignment.objects.restrict(request.user, 'view')
|
return parent.get_contacts().restrict(request.user, 'view').order_by('priority', 'contact', 'role')
|
||||||
for obj in [parent]:
|
|
||||||
qs = qs.filter(
|
|
||||||
object_type=ContentType.objects.get_for_model(obj),
|
|
||||||
object_id__in=(
|
|
||||||
obj.get_ancestors(include_self=True).values_list('pk', flat=True)
|
|
||||||
if isinstance(obj, NestedGroupModel)
|
|
||||||
else [obj.pk]
|
|
||||||
),
|
|
||||||
)
|
|
||||||
|
|
||||||
return qs.order_by('priority', 'contact', 'role')
|
|
||||||
|
|
||||||
|
|
||||||
#
|
#
|
||||||
|
Loading…
Reference in New Issue
Block a user