querysets.py 6.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. from django.contrib.postgres.aggregates import JSONBAgg
  2. from django.db.models import OuterRef, Subquery, Q
  3. from extras.models.tags import TaggedItem
  4. from utilities.query_functions import EmptyGroupByJSONBAgg
  5. from utilities.querysets import RestrictedQuerySet
  6. __all__ = (
  7. 'ConfigContextModelQuerySet',
  8. 'ConfigContextQuerySet',
  9. 'NotificationQuerySet',
  10. )
  11. class ConfigContextQuerySet(RestrictedQuerySet):
  12. def get_for_object(self, obj, aggregate_data=False):
  13. """
  14. Return all applicable ConfigContexts for a given object. Only active ConfigContexts will be included.
  15. Args:
  16. aggregate_data: If True, use the JSONBAgg aggregate function to return only the list of JSON data objects
  17. """
  18. # Device type and location assignment is relevant only for Devices
  19. device_type = getattr(obj, 'device_type', None)
  20. location = getattr(obj, 'location', None)
  21. # Get assigned cluster, group, and type (if any)
  22. cluster = getattr(obj, 'cluster', None)
  23. cluster_type = getattr(cluster, 'type', None)
  24. cluster_group = getattr(cluster, 'group', None)
  25. # Get the group of the assigned tenant, if any
  26. tenant_group = obj.tenant.group if obj.tenant else None
  27. # Match against the directly assigned region as well as any parent regions.
  28. region = getattr(obj.site, 'region', None)
  29. regions = region.get_ancestors(include_self=True) if region else []
  30. # Match against the directly assigned site group as well as any parent site groups.
  31. sitegroup = getattr(obj.site, 'group', None)
  32. sitegroups = sitegroup.get_ancestors(include_self=True) if sitegroup else []
  33. # Match against the directly assigned role as well as any parent roles.
  34. device_roles = obj.role.get_ancestors(include_self=True) if obj.role else []
  35. queryset = self.filter(
  36. Q(regions__in=regions) | Q(regions=None),
  37. Q(site_groups__in=sitegroups) | Q(site_groups=None),
  38. Q(sites=obj.site) | Q(sites=None),
  39. Q(locations=location) | Q(locations=None),
  40. Q(device_types=device_type) | Q(device_types=None),
  41. Q(roles__in=device_roles) | Q(roles=None),
  42. Q(platforms=obj.platform) | Q(platforms=None),
  43. Q(cluster_types=cluster_type) | Q(cluster_types=None),
  44. Q(cluster_groups=cluster_group) | Q(cluster_groups=None),
  45. Q(clusters=cluster) | Q(clusters=None),
  46. Q(tenant_groups=tenant_group) | Q(tenant_groups=None),
  47. Q(tenants=obj.tenant) | Q(tenants=None),
  48. Q(tags__slug__in=obj.tags.slugs()) | Q(tags=None),
  49. is_active=True,
  50. ).order_by('weight', 'name').distinct()
  51. if aggregate_data:
  52. return queryset.aggregate(
  53. config_context_data=JSONBAgg('data', ordering=['weight', 'name'])
  54. )['config_context_data']
  55. return queryset
  56. class ConfigContextModelQuerySet(RestrictedQuerySet):
  57. """
  58. QuerySet manager used by models which support ConfigContext (device and virtual machine).
  59. Includes a method which appends an annotation of aggregated config context JSON data objects. This is
  60. implemented as a subquery which performs all the joins necessary to filter relevant config context objects.
  61. This offers a substantial performance gain over ConfigContextQuerySet.get_for_object() when dealing with
  62. multiple objects. This allows the annotation to be entirely optional.
  63. """
  64. def annotate_config_context_data(self):
  65. """
  66. Attach the subquery annotation to the base queryset
  67. """
  68. from extras.models import ConfigContext
  69. return self.annotate(
  70. config_context_data=Subquery(
  71. ConfigContext.objects.filter(
  72. self._get_config_context_filters()
  73. ).annotate(
  74. _data=EmptyGroupByJSONBAgg('data', ordering=['weight', 'name'])
  75. ).values("_data").order_by()
  76. )
  77. ).distinct()
  78. def _get_config_context_filters(self):
  79. # Construct the set of Q objects for the specific object types
  80. tag_query_filters = {
  81. "object_id": OuterRef(OuterRef('pk')),
  82. "content_type__app_label": self.model._meta.app_label,
  83. "content_type__model": self.model._meta.model_name
  84. }
  85. base_query = Q(
  86. Q(platforms=OuterRef('platform')) | Q(platforms=None),
  87. Q(cluster_types=OuterRef('cluster__type')) | Q(cluster_types=None),
  88. Q(cluster_groups=OuterRef('cluster__group')) | Q(cluster_groups=None),
  89. Q(clusters=OuterRef('cluster')) | Q(clusters=None),
  90. Q(tenant_groups=OuterRef('tenant__group')) | Q(tenant_groups=None),
  91. Q(tenants=OuterRef('tenant')) | Q(tenants=None),
  92. Q(sites=OuterRef('site')) | Q(sites=None),
  93. Q(
  94. tags__pk__in=Subquery(
  95. TaggedItem.objects.filter(
  96. **tag_query_filters
  97. ).values_list(
  98. 'tag_id',
  99. flat=True
  100. )
  101. )
  102. ) | Q(tags=None),
  103. is_active=True,
  104. )
  105. # Apply Location & DeviceType filters only for VirtualMachines
  106. if self.model._meta.model_name == 'device':
  107. base_query.add((Q(locations=OuterRef('location')) | Q(locations=None)), Q.AND)
  108. base_query.add((Q(device_types=OuterRef('device_type')) | Q(device_types=None)), Q.AND)
  109. elif self.model._meta.model_name == 'virtualmachine':
  110. base_query.add(Q(locations=None), Q.AND)
  111. base_query.add(Q(device_types=None), Q.AND)
  112. # MPTT-based filters
  113. base_query.add(
  114. (Q(
  115. regions__tree_id=OuterRef('site__region__tree_id'),
  116. regions__level__lte=OuterRef('site__region__level'),
  117. regions__lft__lte=OuterRef('site__region__lft'),
  118. regions__rght__gte=OuterRef('site__region__rght'),
  119. ) | Q(regions=None)),
  120. Q.AND
  121. )
  122. base_query.add(
  123. (Q(
  124. site_groups__tree_id=OuterRef('site__group__tree_id'),
  125. site_groups__level__lte=OuterRef('site__group__level'),
  126. site_groups__lft__lte=OuterRef('site__group__lft'),
  127. site_groups__rght__gte=OuterRef('site__group__rght'),
  128. ) | Q(site_groups=None)),
  129. Q.AND
  130. )
  131. base_query.add(
  132. (Q(
  133. roles__tree_id=OuterRef('role__tree_id'),
  134. roles__level__lte=OuterRef('role__level'),
  135. roles__lft__lte=OuterRef('role__lft'),
  136. roles__rght__gte=OuterRef('role__rght'),
  137. ) | Q(roles=None)),
  138. Q.AND
  139. )
  140. return base_query
  141. class NotificationQuerySet(RestrictedQuerySet):
  142. def unread(self):
  143. """
  144. Return only unread notifications.
  145. """
  146. return self.filter(read__isnull=True)