Improve consist counter, fix a bug with unpublished stock

This commit is contained in:
2025-04-27 22:12:15 +02:00
parent 85741f090c
commit 1222116874
3 changed files with 29 additions and 14 deletions

View File

@@ -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(),
),
]

View File

@@ -50,19 +50,14 @@ class Consist(BaseModel):
"type" "type"
).annotate( ).annotate(
count=models.Count("rolling_stock"), count=models.Count("rolling_stock"),
category=models.F("rolling_stock__rolling_class__type__category") category=models.F("rolling_stock__rolling_class__type__category"),
).order_by("rolling_stock__rolling_class__type__order") order=models.Max("order"),
).order_by("order")
@property @property
def country(self): def country(self):
return self.company.country 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: class Meta:
ordering = ["company", "-creation_time"] ordering = ["company", "-creation_time"]
@@ -72,11 +67,7 @@ class ConsistItem(models.Model):
Consist, on_delete=models.CASCADE, related_name="consist_item" Consist, on_delete=models.CASCADE, related_name="consist_item"
) )
rolling_stock = models.ForeignKey(RollingStock, on_delete=models.CASCADE) rolling_stock = models.ForeignKey(RollingStock, on_delete=models.CASCADE)
order = models.PositiveIntegerField( order = models.PositiveIntegerField(blank=False, null=False)
default=1000, # make sure it is always added at the end
blank=False,
null=False
)
class Meta: class Meta:
ordering = ["order"] ordering = ["order"]
@@ -90,6 +81,12 @@ class ConsistItem(models.Model):
def __str__(self): def __str__(self):
return "{0}".format(self.rolling_stock) 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): def published(self):
return self.rolling_stock.published return self.rolling_stock.published
published.boolean = True published.boolean = True

View File

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