Skip to content

Commit

Permalink
fix image save for old Python versions (#390)
Browse files Browse the repository at this point in the history
  • Loading branch information
dreng committed Sep 28, 2023
1 parent 1683704 commit f28623b
Showing 1 changed file with 21 additions and 9 deletions.
30 changes: 21 additions & 9 deletions netbox_topology_views/api/views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
from typing import Dict
import sys

from circuits.models import Circuit
from dcim.models import Device, DeviceRole, PowerFeed, PowerPanel
Expand Down Expand Up @@ -149,15 +150,26 @@ def save(self, request):
{"status": "Missing or malformed request body"}, status=400
)

device_roles = {
k: v.removeprefix(settings.STATIC_URL)
for k, v in request.data.items()
if k.isnumeric()}
content_type_ids = {
k[2:]: v.removeprefix(settings.STATIC_URL)
for k, v in request.data.items()
if k.startswith("ct") and k[2:].isnumeric()
}
if sys.version_info >= (3,9,0):
device_roles = {
k: v.removeprefix(settings.STATIC_URL)
for k, v in request.data.items()
if k.isnumeric()}
content_type_ids = {
k[2:]: v.removeprefix(settings.STATIC_URL)
for k, v in request.data.items()
if k.startswith("ct") and k[2:].isnumeric()
}
else:
device_roles = {}
for k, v in request.data.items():
if k.isdigit() and v.startswith(settings.STATIC_URL):
device_roles[k] = v[len(settings.STATIC_URL):]

content_type_ids = {}
for k, v in request.data.items():
if k.startswith("ct") and k[2:].isdigit() and v.startswith(settings.STATIC_URL):
content_type_ids[k[2:]] = v[len(settings.STATIC_URL):]

roles: Dict[int, DeviceRole] = DeviceRole.objects.in_bulk(device_roles.keys())
content_types: Dict[int, ContentType] = ContentType.objects.in_bulk(
Expand Down

0 comments on commit f28623b

Please sign in to comment.