From 122211687412b1577efd9d0ff40727ded030a599 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniele=20Vigan=C3=B2?= Date: Sun, 27 Apr 2025 22:12:15 +0200 Subject: [PATCH] Improve consist counter, fix a bug with unpublished stock --- .../0016_alter_consistitem_order.py | 18 +++++++++++++++ ram/consist/models.py | 23 ++++++++----------- ram/ram/__init__.py | 2 +- 3 files changed, 29 insertions(+), 14 deletions(-) create mode 100644 ram/consist/migrations/0016_alter_consistitem_order.py diff --git a/ram/consist/migrations/0016_alter_consistitem_order.py b/ram/consist/migrations/0016_alter_consistitem_order.py new file mode 100644 index 0000000..68cc677 --- /dev/null +++ b/ram/consist/migrations/0016_alter_consistitem_order.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1.4 on 2025-04-27 19:53 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("consist", "0015_consist_description"), + ] + + operations = [ + migrations.AlterField( + model_name="consistitem", + name="order", + field=models.PositiveIntegerField(), + ), + ] diff --git a/ram/consist/models.py b/ram/consist/models.py index f9d5ac8..e6467aa 100644 --- a/ram/consist/models.py +++ b/ram/consist/models.py @@ -50,19 +50,14 @@ class Consist(BaseModel): "type" ).annotate( count=models.Count("rolling_stock"), - category=models.F("rolling_stock__rolling_class__type__category") - ).order_by("rolling_stock__rolling_class__type__order") + category=models.F("rolling_stock__rolling_class__type__category"), + order=models.Max("order"), + ).order_by("order") @property def country(self): return self.company.country - def clean(self): - if self.consist_item.filter(rolling_stock__published=False).exists(): - raise ValidationError( - "You must publish all items in the consist before publishing the consist." # noqa: E501 - ) - class Meta: ordering = ["company", "-creation_time"] @@ -72,11 +67,7 @@ class ConsistItem(models.Model): Consist, on_delete=models.CASCADE, related_name="consist_item" ) rolling_stock = models.ForeignKey(RollingStock, on_delete=models.CASCADE) - order = models.PositiveIntegerField( - default=1000, # make sure it is always added at the end - blank=False, - null=False - ) + order = models.PositiveIntegerField(blank=False, null=False) class Meta: ordering = ["order"] @@ -90,6 +81,12 @@ class ConsistItem(models.Model): def __str__(self): return "{0}".format(self.rolling_stock) + def clean(self): + if self.consist.published and not self.rolling_stock.published: + raise ValidationError( + "You must unpublish the the consist before using this item." + ) + def published(self): return self.rolling_stock.published published.boolean = True diff --git a/ram/ram/__init__.py b/ram/ram/__init__.py index 21933ee..da1f3ee 100644 --- a/ram/ram/__init__.py +++ b/ram/ram/__init__.py @@ -1,4 +1,4 @@ from ram.utils import git_suffix -__version__ = "0.17.4" +__version__ = "0.17.5" __version__ += git_suffix(__file__)