Implement support for rolling stock load in consists

This commit is contained in:
2026-01-03 14:18:46 +01:00
parent 2ab2d00585
commit 49c8d804d6
5 changed files with 65 additions and 7 deletions

View File

@@ -0,0 +1,18 @@
# Generated by Django 6.0 on 2026-01-03 12:31
from django.db import migrations, models
class Migration(migrations.Migration):
dependencies = [
("consist", "0018_alter_consist_scale"),
]
operations = [
migrations.AddField(
model_name="consistitem",
name="load",
field=models.BooleanField(default=False),
),
]

View File

@@ -43,10 +43,10 @@ class Consist(BaseModel):
@property
def length(self):
return self.consist_item.count()
return self.consist_item.filter(load=False).count()
def get_type_count(self):
return self.consist_item.annotate(
return self.consist_item.filter(load=False).annotate(
type=models.F("rolling_stock__rolling_class__type__type")
).values(
"type"
@@ -69,6 +69,7 @@ class ConsistItem(models.Model):
Consist, on_delete=models.CASCADE, related_name="consist_item"
)
rolling_stock = models.ForeignKey(RollingStock, on_delete=models.CASCADE)
load = models.BooleanField(default=False)
order = models.PositiveIntegerField(blank=False, null=False)
class Meta:
@@ -92,10 +93,15 @@ class ConsistItem(models.Model):
# because the consist is not saved yet and it must be moved
# to the admin form validation via InlineFormSet.clean()
consist = self.consist
if rolling_stock.scale != consist.scale:
# Scale must match, but allow loads of any scale
if rolling_stock.scale != consist.scale and not self.load:
raise ValidationError(
"The rolling stock and consist must be of the same scale."
)
if self.load and rolling_stock.scale.ratio != consist.scale.ratio:
raise ValidationError(
"The load and consist must be of the same scale ratio."
)
if self.consist.published and not rolling_stock.published:
raise ValidationError(
"You must unpublish the the consist before using this item."

View File

@@ -7,11 +7,11 @@
{{ t.name }}</a>{# new line is required #}
{% endfor %}
</p>
{% endif %}
{% if not consist.published %}
<span class="badge text-bg-warning">Unpublished</span> |
{% endif %}
<small class="text-body-secondary">Updated {{ consist.updated_time | date:"M d, Y H:i" }}</small>
{% endif %}
{% endblock %}
{% block carousel %}
{% if consist.image %}
@@ -26,6 +26,33 @@
</div>
{% endif %}
{% endblock %}
{% block cards_layout %}
<div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-3">
{% block cards %}
{% for d in data %}
{% include "cards/roster.html" %}
{% endfor %}
{% endblock %}
</div>
<div class="accordion shadow-sm mt-4" id="accordionLoads">
<div class="accordion-item">
<h2 class="accordion-header">
<button class="accordion-button collapsed" type="button" data-bs-toggle="collapse" data-bs-target="#collapseLoads" aria-expanded="false" aria-controls="collapseLoads">
<i class="bi bi-download"></i>&nbsp;Rolling Stock loaded on freight cars
</button>
</h2>
<div id="collapseLoads" class="accordion-collapse collapse" data-bs-parent="#accordionLoads">
<div class="accordion-body">
<div class="row row-cols-1 row-cols-md-2 row-cols-lg-3 g-3">
{% for l in loads %}
{% include "cards/roster.html" with d=l %}
{% endfor %}
</div>
</div>
</div>
</div>
</div>
{% endblock %}
{% block pagination %}
{% if data.has_other_pages %}
<nav aria-label="Page navigation">
@@ -113,7 +140,7 @@
</tr>
<tr>
<th scope="row">Composition</th>
<td>{% for t in consist.get_type_count %}{{ t.count }}x {{ t.type }} {{t.category }}{% if not forloop.last %} &raquo; {% endif %}{% endfor %}</td>
<td>{% for t in consist.get_type_count %}{{ t.count }}x {{ t.type }} {{t.category }}{% if not forloop.last %} &raquo; {% endif %}{% endfor %}{% if loads %} | <i class="bi bi-download"></i> {{ loads|length }}x Load{{ loads|pluralize }}{% endif %}</td>
</tr>
</tbody>
</table>

View File

@@ -526,7 +526,13 @@ class GetConsist(View):
RollingStock.objects.get_published(request.user).get(
uuid=r.rolling_stock_id
)
for r in consist.consist_item.all()
for r in consist.consist_item.filter(load=False)
)
loads = list(
RollingStock.objects.get_published(request.user).get(
uuid=r.rolling_stock_id
)
for r in consist.consist_item.filter(load=True)
)
paginator = Paginator(data, get_items_per_page())
data = paginator.get_page(page)
@@ -541,6 +547,7 @@ class GetConsist(View):
"title": consist,
"consist": consist,
"data": data,
"loads": loads,
"page_range": page_range,
},
)

View File

@@ -1,4 +1,4 @@
from ram.utils import git_suffix
__version__ = "0.19.5"
__version__ = "0.19.6"
__version__ += git_suffix(__file__)