fix image save for old Python versions (#390)
This commit is contained in:
@@ -1,4 +1,5 @@
|
|||||||
from typing import Dict
|
from typing import Dict
|
||||||
|
import sys
|
||||||
|
|
||||||
from circuits.models import Circuit
|
from circuits.models import Circuit
|
||||||
from dcim.models import Device, DeviceRole, PowerFeed, PowerPanel
|
from dcim.models import Device, DeviceRole, PowerFeed, PowerPanel
|
||||||
@@ -149,6 +150,7 @@ class SaveRoleImageViewSet(PermissionRequiredMixin, ReadOnlyModelViewSet):
|
|||||||
{"status": "Missing or malformed request body"}, status=400
|
{"status": "Missing or malformed request body"}, status=400
|
||||||
)
|
)
|
||||||
|
|
||||||
|
if sys.version_info >= (3,9,0):
|
||||||
device_roles = {
|
device_roles = {
|
||||||
k: v.removeprefix(settings.STATIC_URL)
|
k: v.removeprefix(settings.STATIC_URL)
|
||||||
for k, v in request.data.items()
|
for k, v in request.data.items()
|
||||||
@@ -158,6 +160,16 @@ class SaveRoleImageViewSet(PermissionRequiredMixin, ReadOnlyModelViewSet):
|
|||||||
for k, v in request.data.items()
|
for k, v in request.data.items()
|
||||||
if k.startswith("ct") and k[2:].isnumeric()
|
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())
|
roles: Dict[int, DeviceRole] = DeviceRole.objects.in_bulk(device_roles.keys())
|
||||||
content_types: Dict[int, ContentType] = ContentType.objects.in_bulk(
|
content_types: Dict[int, ContentType] = ContentType.objects.in_bulk(
|
||||||
|
|||||||
Reference in New Issue
Block a user