Skip to content

Commit

Permalink
Closes #189, Closes #158: Add draw.io XML export (#277)
Browse files Browse the repository at this point in the history
* Added export to draw.io XML

* Corrected missing newline at end of file

* init

* moved to api call

---------

Co-authored-by: mattieserver <3049868+mattieserver@users.noreply.github.com>
  • Loading branch information
dreng and mattieserver authored Apr 30, 2023
1 parent 7d23cfc commit 5e9aaec
Show file tree
Hide file tree
Showing 11 changed files with 1,414 additions and 295 deletions.
1 change: 1 addition & 0 deletions netbox_topology_views/api/urls.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,5 +6,6 @@

router.register("save-coords", views.SaveCoordsViewSet)
router.register("images", views.SaveRoleImageViewSet)
router.register("xml-export", views.ExportTopoToXML)

urlpatterns = router.urls
43 changes: 39 additions & 4 deletions netbox_topology_views/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@
from django.conf import settings
from django.contrib.auth.mixins import PermissionRequiredMixin
from django.contrib.contenttypes.models import ContentType
from django.http import JsonResponse
from django.http import HttpResponse, JsonResponse
from rest_framework.decorators import action
from rest_framework.response import Response
from rest_framework.viewsets import ReadOnlyModelViewSet, ViewSet
Expand All @@ -14,9 +14,10 @@
RoleImageSerializer,
TopologyDummySerializer,
)
from netbox_topology_views.models import RoleImage
from netbox_topology_views.utils import get_image_from_url

from netbox_topology_views.models import RoleImage, IndividualOptions
from netbox_topology_views.views import get_topology_data
from netbox_topology_views.utils import get_image_from_url, export_data_to_xml, get_query_settings
from netbox_topology_views.filters import DeviceFilterSet

class SaveCoordsViewSet(ReadOnlyModelViewSet):
queryset = Device.objects.none()
Expand Down Expand Up @@ -62,6 +63,40 @@ def save_coords(self, request):

return Response({"status": "saved coords"})

class ExportTopoToXML(PermissionRequiredMixin, ViewSet):
queryset = Device.objects.none()
permission_required = ("dcim.view_site", "dcim.view_device")
serializer_class = TopologyDummySerializer

def list(self, request):

self.filterset = DeviceFilterSet
self.queryset = Device.objects.all().select_related(
"device_type", "device_role"
)
self.queryset = self.filterset(request.GET, self.queryset).qs

individualOptions, created = IndividualOptions.objects.get_or_create(
user_id=request.user.id,
)

save_coords, show_unconnected, show_power, show_circuit, show_logical_connections, show_single_cable_logical_conns, show_cables, show_wireless = get_query_settings(request)
topo_data = get_topology_data(
self.queryset,
individualOptions,
show_unconnected,
save_coords,
show_cables,
show_circuit,
show_logical_connections,
show_single_cable_logical_conns,
show_power,
show_wireless,
)
xml_data = export_data_to_xml(topo_data).decode('utf-8')

return HttpResponse(xml_data, content_type="application/xml; charset=utf-8")


class SaveRoleImageViewSet(PermissionRequiredMixin, ViewSet):
queryset = DeviceRole.objects.none()
Expand Down
7 changes: 3 additions & 4 deletions netbox_topology_views/static/netbox_topology_views/js/app.js

Large diffs are not rendered by default.

23 changes: 23 additions & 0 deletions netbox_topology_views/static_dev/js/home.js
Original file line number Diff line number Diff line change
Expand Up @@ -133,6 +133,29 @@ function performGraphDownload() {
document.body.removeChild(tempDownloadLink)
}

// Download XML
const downloadXmlButton = document.querySelector('#btnDownloadXml')
downloadXmlButton.addEventListener('click', (e) => {
performXmlDownload()
})

function performXmlDownload() {
const tempDownloadLink = document.createElement('a');

fetch('/api/plugins/netbox_topology_views/xml-export/?' + new URLSearchParams(window.location.search)).then(response => response.text())
.then(data => {
var blob = new Blob([data ], { type: "text/plain" });

tempDownloadLink.setAttribute("href", window.URL.createObjectURL(blob));
tempDownloadLink.setAttribute("download", 'topology.xml');

tempDownloadLink.dataset.downloadurl = ["text/plain", tempDownloadLink.download, tempDownloadLink.href].join(":");

tempDownloadLink.click();

});
}

// Theme switching
const observer = new MutationObserver((mutations) =>
mutations.forEach((mutation) => {
Expand Down
Loading

0 comments on commit 5e9aaec

Please sign in to comment.