api.py 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177
  1. from django.contrib.contenttypes.fields import GenericForeignKey
  2. from django.core.exceptions import (
  3. FieldDoesNotExist, FieldError, MultipleObjectsReturned, ObjectDoesNotExist, ValidationError,
  4. )
  5. from django.db.models.fields.related import ManyToOneRel, RelatedField
  6. from django.urls import reverse
  7. from django.utils.module_loading import import_string
  8. from django.utils.translation import gettext_lazy as _
  9. from rest_framework.serializers import Serializer
  10. from rest_framework.views import get_view_name as drf_get_view_name
  11. from extras.constants import HTTP_CONTENT_TYPE_JSON
  12. from netbox.api.exceptions import GraphQLTypeNotFound, SerializerNotFound
  13. from netbox.api.fields import RelatedObjectCountField
  14. from .query import count_related, dict_to_filter_params
  15. from .string import title
  16. __all__ = (
  17. 'get_annotations_for_serializer',
  18. 'get_graphql_type_for_model',
  19. 'get_prefetches_for_serializer',
  20. 'get_related_object_by_attrs',
  21. 'get_serializer_for_model',
  22. 'get_view_name',
  23. 'is_api_request',
  24. )
  25. def get_serializer_for_model(model, prefix=''):
  26. """
  27. Return the appropriate REST API serializer for the given model.
  28. """
  29. app_label, model_name = model._meta.label.split('.')
  30. serializer_name = f'{app_label}.api.serializers.{prefix}{model_name}Serializer'
  31. try:
  32. return import_string(serializer_name)
  33. except ImportError:
  34. raise SerializerNotFound(
  35. f"Could not determine serializer for {app_label}.{model_name} with prefix '{prefix}'"
  36. )
  37. def get_graphql_type_for_model(model):
  38. """
  39. Return the GraphQL type class for the given model.
  40. """
  41. app_label, model_name = model._meta.label.split('.')
  42. class_name = f'{app_label}.graphql.types.{model_name}Type'
  43. try:
  44. return import_string(class_name)
  45. except ImportError:
  46. raise GraphQLTypeNotFound(f"Could not find GraphQL type for {app_label}.{model_name}")
  47. def is_api_request(request):
  48. """
  49. Return True of the request is being made via the REST API.
  50. """
  51. api_path = reverse('api-root')
  52. return request.path_info.startswith(api_path) and request.content_type == HTTP_CONTENT_TYPE_JSON
  53. def get_view_name(view):
  54. """
  55. Derive the view name from its associated model, if it has one. Fall back to DRF's built-in `get_view_name()`.
  56. This function is provided to DRF as its VIEW_NAME_FUNCTION.
  57. """
  58. if hasattr(view, 'queryset'):
  59. # Derive the model name from the queryset.
  60. name = title(view.queryset.model._meta.verbose_name)
  61. if suffix := getattr(view, 'suffix', None):
  62. name = f'{name} {suffix}'
  63. return name
  64. # Fall back to DRF's default behavior
  65. return drf_get_view_name(view)
  66. def get_prefetches_for_serializer(serializer_class, fields_to_include=None):
  67. """
  68. Compile and return a list of fields which should be prefetched on the queryset for a serializer.
  69. """
  70. model = serializer_class.Meta.model
  71. # If fields are not specified, default to all
  72. if not fields_to_include:
  73. fields_to_include = serializer_class.Meta.fields
  74. prefetch_fields = []
  75. for field_name in fields_to_include:
  76. serializer_field = serializer_class._declared_fields.get(field_name)
  77. # Determine the name of the model field referenced by the serializer field
  78. model_field_name = field_name
  79. if serializer_field and serializer_field.source:
  80. model_field_name = serializer_field.source
  81. # If the serializer field does not map to a discrete model field, skip it.
  82. try:
  83. field = model._meta.get_field(model_field_name)
  84. if isinstance(field, (RelatedField, ManyToOneRel, GenericForeignKey)):
  85. prefetch_fields.append(field.name)
  86. except FieldDoesNotExist:
  87. continue
  88. # If this field is represented by a nested serializer, recurse to resolve prefetches
  89. # for the related object.
  90. if serializer_field:
  91. if issubclass(type(serializer_field), Serializer):
  92. # Determine which fields to prefetch for the nested object
  93. subfields = serializer_field.Meta.brief_fields if serializer_field.nested else None
  94. for subfield in get_prefetches_for_serializer(type(serializer_field), subfields):
  95. prefetch_fields.append(f'{field_name}__{subfield}')
  96. return prefetch_fields
  97. def get_annotations_for_serializer(serializer_class, fields_to_include=None):
  98. """
  99. Return a mapping of field names to annotations to be applied to the queryset for a serializer.
  100. """
  101. annotations = {}
  102. # If specific fields are not specified, default to all
  103. if not fields_to_include:
  104. fields_to_include = serializer_class.Meta.fields
  105. model = serializer_class.Meta.model
  106. for field_name, field in serializer_class._declared_fields.items():
  107. if field_name in fields_to_include and type(field) is RelatedObjectCountField:
  108. related_field = model._meta.get_field(field.relation).field
  109. annotations[field_name] = count_related(related_field.model, related_field.name)
  110. return annotations
  111. def get_related_object_by_attrs(queryset, attrs):
  112. """
  113. Return an object identified by either a dictionary of attributes or its numeric primary key (ID). This is used
  114. for referencing related objects when creating/updating objects via the REST API.
  115. """
  116. if attrs is None:
  117. return None
  118. # Dictionary of related object attributes
  119. if isinstance(attrs, dict):
  120. params = dict_to_filter_params(attrs)
  121. try:
  122. return queryset.get(**params)
  123. except ObjectDoesNotExist:
  124. raise ValidationError(
  125. _("Related object not found using the provided attributes: {params}").format(params=params))
  126. except MultipleObjectsReturned:
  127. raise ValidationError(
  128. _("Multiple objects match the provided attributes: {params}").format(params=params)
  129. )
  130. except FieldError as e:
  131. raise ValidationError(e)
  132. # Integer PK of related object
  133. try:
  134. # Cast as integer in case a PK was mistakenly sent as a string
  135. pk = int(attrs)
  136. except (TypeError, ValueError):
  137. raise ValidationError(
  138. _(
  139. "Related objects must be referenced by numeric ID or by dictionary of attributes. Received an "
  140. "unrecognized value: {value}"
  141. ).format(value=attrs)
  142. )
  143. # Look up object by PK
  144. try:
  145. return queryset.get(pk=pk)
  146. except ObjectDoesNotExist:
  147. raise ValidationError(_("Related object not found using the provided numeric ID: {id}").format(id=pk))