mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-04 13:17:50 +02:00
Improve road number sorting and enforce company on consists
This commit is contained in:
20
ram/consist/migrations/0004_alter_consist_company.py
Normal file
20
ram/consist/migrations/0004_alter_consist_company.py
Normal file
@@ -0,0 +1,20 @@
|
|||||||
|
# Generated by Django 4.0.6 on 2022-07-15 16:08
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
import django.db.models.deletion
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('metadata', '0004_alter_rollingstocktype_options_and_more'),
|
||||||
|
('consist', '0003_consist_image'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AlterField(
|
||||||
|
model_name='consist',
|
||||||
|
name='company',
|
||||||
|
field=models.ForeignKey(on_delete=django.db.models.deletion.CASCADE, to='metadata.company'),
|
||||||
|
),
|
||||||
|
]
|
@@ -13,9 +13,7 @@ class Consist(models.Model):
|
|||||||
consist_address = models.SmallIntegerField(
|
consist_address = models.SmallIntegerField(
|
||||||
default=None, null=True, blank=True
|
default=None, null=True, blank=True
|
||||||
)
|
)
|
||||||
company = models.ForeignKey(
|
company = models.ForeignKey(Company, on_delete=models.CASCADE)
|
||||||
Company, on_delete=models.CASCADE, null=True, blank=True
|
|
||||||
)
|
|
||||||
era = models.CharField(max_length=32, blank=True)
|
era = models.CharField(max_length=32, blank=True)
|
||||||
image = models.ImageField(upload_to="images/", null=True, blank=True)
|
image = models.ImageField(upload_to="images/", null=True, blank=True)
|
||||||
notes = models.TextField(blank=True)
|
notes = models.TextField(blank=True)
|
||||||
|
@@ -18,7 +18,7 @@ def order_by_fields():
|
|||||||
fields = ["rolling_class__type",
|
fields = ["rolling_class__type",
|
||||||
"rolling_class__company",
|
"rolling_class__company",
|
||||||
"rolling_class__identifier",
|
"rolling_class__identifier",
|
||||||
"road_number"]
|
"road_number_cleaned"]
|
||||||
|
|
||||||
if order_by == "type":
|
if order_by == "type":
|
||||||
return (fields[0], fields[1], fields[2], fields[3])
|
return (fields[0], fields[1], fields[2], fields[3])
|
||||||
|
@@ -0,0 +1,34 @@
|
|||||||
|
# Generated by Django 4.0.6 on 2022-07-15 15:55
|
||||||
|
|
||||||
|
from django.db import migrations, models
|
||||||
|
|
||||||
|
|
||||||
|
def gen_road_number_cleaned(apps, schema_editor):
|
||||||
|
RollingStock = apps.get_model('roster', 'RollingStock')
|
||||||
|
for row in RollingStock.objects.all():
|
||||||
|
row.road_number_cleaned = row.road_number.lstrip('#').lstrip('0')
|
||||||
|
row.save(update_fields=['road_number_cleaned'])
|
||||||
|
|
||||||
|
|
||||||
|
class Migration(migrations.Migration):
|
||||||
|
|
||||||
|
dependencies = [
|
||||||
|
('roster', '0007_alter_rollingclass_company_alter_rollingclass_type'),
|
||||||
|
]
|
||||||
|
|
||||||
|
operations = [
|
||||||
|
migrations.AddField(
|
||||||
|
model_name='rollingstock',
|
||||||
|
name='road_number_cleaned',
|
||||||
|
field=models.CharField(default='', max_length=128),
|
||||||
|
preserve_default=False,
|
||||||
|
),
|
||||||
|
migrations.RunPython(
|
||||||
|
gen_road_number_cleaned,
|
||||||
|
reverse_code=migrations.RunPython.noop
|
||||||
|
),
|
||||||
|
migrations.AlterModelOptions(
|
||||||
|
name='rollingstock',
|
||||||
|
options={'ordering': ['rolling_class', 'road_number_cleaned'], 'verbose_name_plural': 'Rolling stock'},
|
||||||
|
),
|
||||||
|
]
|
@@ -2,9 +2,9 @@ import os
|
|||||||
from uuid import uuid4
|
from uuid import uuid4
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
from django.dispatch import receiver
|
||||||
|
|
||||||
# from django.core.files.storage import FileSystemStorage
|
# from django.core.files.storage import FileSystemStorage
|
||||||
# from django.dispatch import receiver
|
|
||||||
|
|
||||||
from ram.utils import get_image_preview
|
from ram.utils import get_image_preview
|
||||||
from metadata.models import (
|
from metadata.models import (
|
||||||
@@ -75,6 +75,7 @@ class RollingStock(models.Model):
|
|||||||
verbose_name="Class",
|
verbose_name="Class",
|
||||||
)
|
)
|
||||||
road_number = models.CharField(max_length=128, unique=False)
|
road_number = models.CharField(max_length=128, unique=False)
|
||||||
|
road_number_cleaned = models.CharField(max_length=128, unique=False)
|
||||||
manufacturer = models.ForeignKey(
|
manufacturer = models.ForeignKey(
|
||||||
Manufacturer,
|
Manufacturer,
|
||||||
on_delete=models.CASCADE,
|
on_delete=models.CASCADE,
|
||||||
@@ -99,7 +100,7 @@ class RollingStock(models.Model):
|
|||||||
updated_time = models.DateTimeField(auto_now=True)
|
updated_time = models.DateTimeField(auto_now=True)
|
||||||
|
|
||||||
class Meta:
|
class Meta:
|
||||||
ordering = ["rolling_class", "road_number"]
|
ordering = ["rolling_class", "road_number_cleaned"]
|
||||||
verbose_name_plural = "Rolling stock"
|
verbose_name_plural = "Rolling stock"
|
||||||
|
|
||||||
def __str__(self):
|
def __str__(self):
|
||||||
@@ -115,6 +116,11 @@ class RollingStock(models.Model):
|
|||||||
return str(self.rolling_class.company)
|
return str(self.rolling_class.company)
|
||||||
|
|
||||||
|
|
||||||
|
@receiver(models.signals.pre_save, sender=RollingStock)
|
||||||
|
def pre_save_running_number(sender, instance, *args, **kwargs):
|
||||||
|
instance.road_number_cleaned = instance.road_number.lstrip("#").lstrip("0")
|
||||||
|
|
||||||
|
|
||||||
class RollingStockDocument(models.Model):
|
class RollingStockDocument(models.Model):
|
||||||
rolling_stock = models.ForeignKey(
|
rolling_stock = models.ForeignKey(
|
||||||
RollingStock, on_delete=models.CASCADE, related_name="document"
|
RollingStock, on_delete=models.CASCADE, related_name="document"
|
||||||
|
Reference in New Issue
Block a user