Просмотр исходного кода

Closes #3665: Enable rendering export templates via REST API

jeremystretch 4 лет назад
Родитель
Сommit
43a823d0ac

+ 10 - 0
docs/additional-features/export-templates.md

@@ -33,6 +33,16 @@ The `as_attachment` attribute of an export template controls its behavior when r
 
 A MIME type and file extension can optionally be defined for each export template. The default MIME type is `text/plain`.
 
+## REST API Integration
+
+When it is necessary to provide authentication credentials (such as when [`LOGIN_REQUIRED`](../configuration/optional-settings.md#login_required) has been enabled), it is recommended to render export templates via the REST API. This allows the client to specify an authentication token. To render an export template via the REST API, make a `GET` request to the model's list endpoint and append the `export` parameter specifying the export template name. For example:
+
+```
+GET /api/dcim/sites/?export=MyTemplateName
+```
+
+Note that the body of the response will contain only the rendered export template content, as opposed to a JSON object or list.
+
 ## Example
 
 Here's an example device export template that will generate a simple Nagios configuration from a list of devices.

+ 1 - 0
docs/release-notes/version-2.12.md

@@ -4,6 +4,7 @@
 
 ### Enhancements
 
+* [#3665](https://github.com/netbox-community/netbox/issues/3665) - Enable rendering export templates via REST API
 * [#4609](https://github.com/netbox-community/netbox/issues/4609) - Allow marking prefixes as fully utilized
 * [#5806](https://github.com/netbox-community/netbox/issues/5806) - Add kilometer and mile as choices for cable length unit
 * [#6154](https://github.com/netbox-community/netbox/issues/6154) - Allow decimal values for cable lengths

+ 4 - 5
netbox/extras/models/models.py

@@ -300,13 +300,12 @@ class ExportTemplate(BigIDModel):
 
         # Build the response
         response = HttpResponse(output, content_type=mime_type)
-        filename = 'netbox_{}{}'.format(
-            queryset.model._meta.verbose_name_plural,
-            '.{}'.format(self.file_extension) if self.file_extension else ''
-        )
 
         if self.as_attachment:
-            response['Content-Disposition'] = 'attachment; filename="{}"'.format(filename)
+            basename = queryset.model._meta.verbose_name_plural.replace(' ', '_')
+            extension = f'.{self.file_extension}' if self.file_extension else ''
+            filename = f'netbox_{basename}{extension}'
+            response['Content-Disposition'] = f'attachment; filename="{filename}"'
 
         return response
 

+ 15 - 0
netbox/netbox/api/views.py

@@ -5,9 +5,11 @@ from collections import OrderedDict
 from django import __version__ as DJANGO_VERSION
 from django.apps import apps
 from django.conf import settings
+from django.contrib.contenttypes.models import ContentType
 from django.core.exceptions import ObjectDoesNotExist, PermissionDenied
 from django.db import transaction
 from django.db.models import ProtectedError
+from django.shortcuts import get_object_or_404
 from django_rq.queues import get_connection
 from rest_framework import status
 from rest_framework.response import Response
@@ -16,6 +18,7 @@ from rest_framework.views import APIView
 from rest_framework.viewsets import ModelViewSet as ModelViewSet_
 from rq.worker import Worker
 
+from extras.models import ExportTemplate
 from netbox.api import BulkOperationSerializer
 from netbox.api.authentication import IsAuthenticatedOrLoginNotRequired
 from netbox.api.exceptions import SerializerNotFound
@@ -222,6 +225,18 @@ class ModelViewSet(BulkUpdateModelMixin, BulkDestroyModelMixin, ModelViewSet_):
             # Check that the instance is matched by the view's queryset
             self.queryset.get(pk=instance.pk)
 
+    def list(self, request, *args, **kwargs):
+        """
+        Overrides ListModelMixin to allow processing ExportTemplates.
+        """
+        if 'export' in request.GET:
+            content_type = ContentType.objects.get_for_model(self.serializer_class.Meta.model)
+            et = get_object_or_404(ExportTemplate, content_type=content_type, name=request.GET['export'])
+            queryset = self.filter_queryset(self.get_queryset())
+            return et.render_to_response(queryset)
+
+        return super().list(request, *args, **kwargs)
+
     def perform_create(self, serializer):
         model = self.queryset.model
         logger = logging.getLogger('netbox.api.views.ModelViewSet')