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"
).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