Fixes #15739: Account for parallel cables when calculating total path length (#19356)

This commit is contained in:
Jeremy Stretch 2025-04-29 12:32:43 -04:00 committed by GitHub
parent 48a367c409
commit 5342552054
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194
2 changed files with 22 additions and 4 deletions

View File

@ -3,7 +3,6 @@ import itertools
from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.fields import GenericForeignKey
from django.core.exceptions import ValidationError from django.core.exceptions import ValidationError
from django.db import models from django.db import models
from django.db.models import Sum
from django.dispatch import Signal from django.dispatch import Signal
from django.utils.translation import gettext_lazy as _ from django.utils.translation import gettext_lazy as _
@ -778,9 +777,28 @@ class CablePath(models.Model):
Return a tuple containing the sum of the length of each cable in the path Return a tuple containing the sum of the length of each cable in the path
and a flag indicating whether the length is definitive. and a flag indicating whether the length is definitive.
""" """
cable_ct = ObjectType.objects.get_for_model(Cable).pk
# Pre-cache cable lengths by ID
cable_ids = self.get_cable_ids() cable_ids = self.get_cable_ids()
cables = Cable.objects.filter(id__in=cable_ids, _abs_length__isnull=False) cables = {
total_length = cables.aggregate(total=Sum('_abs_length'))['total'] cable['pk']: cable['_abs_length']
for cable in Cable.objects.filter(id__in=cable_ids, _abs_length__isnull=False).values('pk', '_abs_length')
}
# Iterate through each set of nodes in the path. For cables, add the length of the longest cable to the total
# length of the path.
total_length = 0
for node_set in self.path:
hop_length = 0
for node in node_set:
ct, pk = decompile_path_node(node)
if ct != cable_ct:
break # Not a cable
if pk in cables and cables[pk] > hop_length:
hop_length = cables[pk]
total_length += hop_length
is_definitive = len(cables) == len(cable_ids) is_definitive = len(cables) == len(cable_ids)
return total_length, is_definitive return total_length, is_definitive

View File

@ -63,7 +63,7 @@
<td> <td>
{% if total_length %} {% if total_length %}
{{ total_length|floatformat:"-2" }}{% if not is_definitive %}+{% endif %} {% trans "Meters" %} / {{ total_length|floatformat:"-2" }}{% if not is_definitive %}+{% endif %} {% trans "Meters" %} /
{{ total_length|meters_to_feet|floatformat:"-2" }} {% trans "Feet" %} {{ total_length|meters_to_feet|floatformat:"-2" }}{% if not is_definitive %}+{% endif %} {% trans "Feet" %}
{% else %} {% else %}
{{ ''|placeholder }} {{ ''|placeholder }}
{% endif %} {% endif %}