fix image save for old Python versions (#390)

This commit is contained in:
Mario authored and GitHub committed 2023-09-28 15:49:05 +02:00
1 parent 16837047be
commit f28623b378
1 file changed
+21 -9
+21 -9
View File
@@ -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,15 +150,26 @@ class SaveRoleImageViewSet(PermissionRequiredMixin, ReadOnlyModelViewSet):
{"status": "Missing or malformed request body"}, status=400 {"status": "Missing or malformed request body"}, status=400
) )
device_roles = { if sys.version_info >= (3,9,0):
k: v.removeprefix(settings.STATIC_URL) device_roles = {
for k, v in request.data.items() k: v.removeprefix(settings.STATIC_URL)
if k.isnumeric()} for k, v in request.data.items()
content_type_ids = { if k.isnumeric()}
k[2:]: v.removeprefix(settings.STATIC_URL) content_type_ids = {
for k, v in request.data.items() k[2:]: v.removeprefix(settings.STATIC_URL)
if k.startswith("ct") and k[2:].isnumeric() 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()) 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(