Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add deletion option to BrokenCableTerminations #108

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 25 additions & 5 deletions scripts/find_orphaned_cables.py
Original file line number Diff line number Diff line change
@@ -1,23 +1,43 @@
from django.db.models import Count, Q

from dcim.models import Cable
from extras.scripts import Script
from extras.scripts import Script, BooleanVar


class BrokenCableTerminations(Script):
name = f'Find (partially) orphaned cables'
description = f'Find cable terminations misising either the A or B termination'

delete_orphans = BooleanVar(
description="Delete orphaned cables (cable missing A and B terminations)"
)

delete_partials = BooleanVar(
description="Delete partially orphaned cables (cable either missing A or B termination)"
)

def run(self, data, commit):
cables = Cable.objects.annotate(
aterm=Count('terminations', filter=Q(terminations__cable_end="A")),
bterm=Count('terminations', filter=Q(terminations__cable_end="B")),
).filter(Q(bterm=0) | Q(aterm=0) )
).filter(Q(bterm=0) | Q(aterm=0))
self.log_info(f'Found {cables.count()} problematic cables in DB')
for cable in cables:
if cable.aterm == 0 and cable.bterm > 0:
self.log_warning(f'[{cable}](/dcim/cables/{cable.pk}/) is missing \'A\' side termination')
self.log_warning(
f'[{cable}](/dcim/cables/{cable.pk}/) is missing \'A\' side termination'
f'{" => deleting" if data["delete_partials"] else ""}')
if data["delete_partials"]:
cable.delete()
elif cable.aterm > 0 and cable.bterm == 0:
self.log_warning(f'[{cable}](/dcim/cables/{cable.pk}/) is missing \'B\' side termination')
self.log_warning(
f'[{cable}](/dcim/cables/{cable.pk}/) is missing \'B\' side termination'
f'{" => deleting" if data["delete_partials"] else ""}')
if data["delete_partials"]:
cable.delete()
else:
self.log_warning(f'[{cable}](/dcim/cables/{cable.pk}/) is orphaned')
self.log_warning(
f'[{cable}](/dcim/cables/{cable.pk}/) is orphaned'
f'{" => deleting" if data["delete_orphans"] else ""}')
if data["delete_orphans"]:
cable.delete()