Skip to content

Commit

Permalink
Fixes: #21 - Unnamed Devices should show Device Type rather than None
Browse files Browse the repository at this point in the history
Fixes: #20 - Child Devices should not show as unracked devices
Fixes: #16 - Only Save Devices which have changed face or U position
  • Loading branch information
cruse1977 committed Jun 27, 2024
1 parent b8200f3 commit c58daba
Show file tree
Hide file tree
Showing 3 changed files with 39 additions and 15 deletions.
33 changes: 22 additions & 11 deletions netbox_reorder_rack/api/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -29,25 +29,36 @@ def update(self, request, pk):
try:
serializer = ReorderRackSerializer(request.data)
with transaction.atomic():
for device in rack.devices.all():
device.position = None
device.clean()
device.save()
#for device in rack.devices.all():
# device.position = None
# device.clean()
# device.save()

for new in request.data["front"]:
device = rack.devices.filter(pk=new["id"]).first()
device.position = decimal.Decimal(new["y"])
device.face = new["face"]
device.clean()
device.save()
current_device = get_object_or_404(Device, pk=new["id"])

if current_device.face != new["face"] or device.position != decimal.Decimal(new["y"]):
device.position = decimal.Decimal(new["y"])
device.face = new["face"]
device.clean()
device.save()

for new in request.data["rear"]:
device = rack.devices.filter(pk=new["id"]).first()
device.position = decimal.Decimal(new["y"])
device.face = new["face"]
current_device = get_object_or_404(Device, pk=new["id"])
if current_device.face != new["face"] or device.position != decimal.Decimal(new["y"]):
device.position = decimal.Decimal(new["y"])
device.face = new["face"]
device.clean()
device.save()
# other = unracked
for new in request.data["other"]:
device = rack.devices.filter(pk=new["id"]).first()
device.position = None
device.clean()
device.save()

return Response(
{"message": "POST request received", "data": serializer.data},
status=status.HTTP_201_CREATED,
Expand Down
6 changes: 5 additions & 1 deletion netbox_reorder_rack/templates/netbox_reorder_rack/rack.html
Original file line number Diff line number Diff line change
Expand Up @@ -84,7 +84,11 @@ <h4>Non-Racked Devices</h4>
{% for device in non_racked %}
<div class='grid-stack-item' gs-w='2' gs-h='{{ device.device_type.u_height|mul:2 }}' gs-x='0' gs-id='{{device.id}}' gs-locked='false' data-item-color="{{device.role.color}}" data-item-text-color="{% if device.role.color %}{{ device.role.color|text_color }}{% else %}000000{% endif %}" data-full-depth="{{device.device_type.is_full_depth}}">
<div class='grid-stack-item-content' style="background-color: #{{device.role.color}}; color: #{% if device.role.color %}{{ device.role.color|text_color }}{% else %}000000{% endif %};">
{{device.name}}
{% if not device.name %}
{{ device.device_type }}
{% else %}
{{device.name}}
{% endif %}
</div>
</div>
{% endfor %}
Expand Down
15 changes: 12 additions & 3 deletions netbox_reorder_rack/views.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
from dcim.models import Device
from dcim.models import Rack
from dcim.models import Device, Rack, DeviceType
from django.conf import settings
from django.contrib.auth.mixins import LoginRequiredMixin
from django.contrib.auth.mixins import PermissionRequiredMixin
Expand All @@ -23,14 +22,24 @@ def get(self, request, pk):
non_racked = Device.objects.filter(
rack=rack, position__isnull=True, parent_bay__isnull=True
)

exclude_list = []
# fix - exclude all child devices:
for device in non_racked:
device_type = DeviceType.objects.get(id=device.device_type.id)
if device_type.subdevice_role == "child":
exclude_list.append(device.id)

non_racked_devices = non_racked.exclude(pk__in = exclude_list)

return render(
request,
self.template_name,
{
"object": rack,
"front_units": rack.get_rack_units(expand_devices=False, face="front"),
"rear_units": rack.get_rack_units(expand_devices=False, face="rear"),
"non_racked": non_racked,
"non_racked": non_racked_devices,
"basepath": settings.BASE_PATH,
},
)

0 comments on commit c58daba

Please sign in to comment.