From 26be22c0bdae75042b08be4ad285624861c0d00c Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniele=20Vigan=C3=B2?= Date: Wed, 8 Jan 2025 23:28:04 +0100 Subject: [PATCH] Minor admin improvements and remove unique_together deprecated Meta Also make rolling stock unique per consist --- ...sebookdocument_unique_together_and_more.py | 23 +++++++++++++ ram/bookshelf/models.py | 7 +++- ram/consist/admin.py | 15 ++++++--- .../0013_consistitem_one_stock_per_consist.py | 20 +++++++++++ .../0014_alter_consistitem_order.py | 18 ++++++++++ ram/consist/models.py | 14 ++++++-- ...ecoderdocument_unique_together_and_more.py | 33 +++++++++++++++++++ ram/metadata/models.py | 20 ++++++++--- ram/ram/__init__.py | 2 +- ...gstockdocument_unique_together_and_more.py | 23 +++++++++++++ ram/roster/models.py | 9 +++-- 11 files changed, 169 insertions(+), 15 deletions(-) create mode 100644 ram/bookshelf/migrations/0020_alter_basebookdocument_unique_together_and_more.py create mode 100644 ram/consist/migrations/0013_consistitem_one_stock_per_consist.py create mode 100644 ram/consist/migrations/0014_alter_consistitem_order.py create mode 100644 ram/metadata/migrations/0020_alter_decoderdocument_unique_together_and_more.py create mode 100644 ram/roster/migrations/0031_alter_rollingstockdocument_unique_together_and_more.py diff --git a/ram/bookshelf/migrations/0020_alter_basebookdocument_unique_together_and_more.py b/ram/bookshelf/migrations/0020_alter_basebookdocument_unique_together_and_more.py new file mode 100644 index 0000000..dd12521 --- /dev/null +++ b/ram/bookshelf/migrations/0020_alter_basebookdocument_unique_together_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 5.1.4 on 2025-01-08 22:25 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("bookshelf", "0019_basebook_price"), + ] + + operations = [ + migrations.AlterUniqueTogether( + name="basebookdocument", + unique_together=set(), + ), + migrations.AddConstraint( + model_name="basebookdocument", + constraint=models.UniqueConstraint( + fields=("book", "file"), name="unique_book_file" + ), + ), + ] diff --git a/ram/bookshelf/models.py b/ram/bookshelf/models.py index e89780d..59a879c 100644 --- a/ram/bookshelf/models.py +++ b/ram/bookshelf/models.py @@ -96,7 +96,12 @@ class BaseBookDocument(Document): class Meta: verbose_name_plural = "Documents" - unique_together = ("book", "file") + constraints = [ + models.UniqueConstraint( + fields=["book", "file"], + name="unique_book_file" + ) + ] class BaseBookProperty(PropertyInstance): diff --git a/ram/consist/admin.py b/ram/consist/admin.py index 53201f8..3d61b01 100644 --- a/ram/consist/admin.py +++ b/ram/consist/admin.py @@ -9,7 +9,14 @@ class ConsistItemInline(SortableInlineAdminMixin, admin.TabularInline): min_num = 1 extra = 0 autocomplete_fields = ("rolling_stock",) - readonly_fields = ("preview", "published", "address", "type", "company", "era") + readonly_fields = ( + "preview", + "published", + "address", + "type", + "company", + "era", + ) @admin.register(Consist) @@ -19,9 +26,9 @@ class ConsistAdmin(SortableAdminBase, admin.ModelAdmin): "creation_time", "updated_time", ) - list_display = ("identifier", "published", "company", "era") - list_filter = list_display - search_fields = list_display + list_filter = ("company", "era", "published") + list_display = ("__str__",) + list_filter + search_fields = ("identifier",) + list_filter save_as = True fieldsets = ( diff --git a/ram/consist/migrations/0013_consistitem_one_stock_per_consist.py b/ram/consist/migrations/0013_consistitem_one_stock_per_consist.py new file mode 100644 index 0000000..115a5da --- /dev/null +++ b/ram/consist/migrations/0013_consistitem_one_stock_per_consist.py @@ -0,0 +1,20 @@ +# Generated by Django 5.1.4 on 2025-01-08 21:50 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("consist", "0012_consist_published"), + ("roster", "0030_rollingstock_price"), + ] + + operations = [ + migrations.AddConstraint( + model_name="consistitem", + constraint=models.UniqueConstraint( + fields=("consist", "rolling_stock"), name="one_stock_per_consist" + ), + ), + ] diff --git a/ram/consist/migrations/0014_alter_consistitem_order.py b/ram/consist/migrations/0014_alter_consistitem_order.py new file mode 100644 index 0000000..30d53cb --- /dev/null +++ b/ram/consist/migrations/0014_alter_consistitem_order.py @@ -0,0 +1,18 @@ +# Generated by Django 5.1.4 on 2025-01-08 22:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("consist", "0013_consistitem_one_stock_per_consist"), + ] + + operations = [ + migrations.AlterField( + model_name="consistitem", + name="order", + field=models.PositiveIntegerField(default=1000), + ), + ] diff --git a/ram/consist/models.py b/ram/consist/models.py index 4cc12cc..79a9d75 100644 --- a/ram/consist/models.py +++ b/ram/consist/models.py @@ -54,10 +54,20 @@ 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=0, blank=False, null=False) + order = models.PositiveIntegerField( + default=1000, # make sure it is always added at the end + blank=False, + null=False + ) - class Meta(object): + class Meta: ordering = ["order"] + constraints = [ + models.UniqueConstraint( + fields=["consist", "rolling_stock"], + name="one_stock_per_consist" + ) + ] def __str__(self): return "{0}".format(self.rolling_stock) diff --git a/ram/metadata/migrations/0020_alter_decoderdocument_unique_together_and_more.py b/ram/metadata/migrations/0020_alter_decoderdocument_unique_together_and_more.py new file mode 100644 index 0000000..9a059af --- /dev/null +++ b/ram/metadata/migrations/0020_alter_decoderdocument_unique_together_and_more.py @@ -0,0 +1,33 @@ +# Generated by Django 5.1.4 on 2025-01-08 22:25 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("metadata", "0019_alter_scale_gauge"), + ] + + operations = [ + migrations.AlterUniqueTogether( + name="decoderdocument", + unique_together=set(), + ), + migrations.AlterUniqueTogether( + name="rollingstocktype", + unique_together=set(), + ), + migrations.AddConstraint( + model_name="decoderdocument", + constraint=models.UniqueConstraint( + fields=("decoder", "file"), name="unique_decoder_file" + ), + ), + migrations.AddConstraint( + model_name="rollingstocktype", + constraint=models.UniqueConstraint( + fields=("category", "type"), name="unique_category_type" + ), + ), + ] diff --git a/ram/metadata/models.py b/ram/metadata/models.py index 01c9a35..cde8761 100644 --- a/ram/metadata/models.py +++ b/ram/metadata/models.py @@ -117,7 +117,7 @@ class Decoder(models.Model): blank=True, ) - class Meta(object): + class Meta: ordering = ["manufacturer__name", "name"] def __str__(self): @@ -135,7 +135,12 @@ class DecoderDocument(Document): ) class Meta: - unique_together = ("decoder", "file") + constraints = [ + models.UniqueConstraint( + fields=["decoder", "file"], + name="unique_decoder_file" + ) + ] def calculate_ratio(ratio): @@ -189,8 +194,13 @@ class RollingStockType(models.Model): ) slug = models.CharField(max_length=128, unique=True, editable=False) - class Meta(object): - unique_together = ("category", "type") + class Meta: + constraints = [ + models.UniqueConstraint( + fields=["category", "type"], + name="unique_category_type" + ) + ] ordering = ["order"] def get_absolute_url(self): @@ -210,7 +220,7 @@ class Tag(models.Model): name = models.CharField(max_length=128, unique=True) slug = models.CharField(max_length=128, unique=True) - class Meta(object): + class Meta: ordering = ["name"] def __str__(self): diff --git a/ram/ram/__init__.py b/ram/ram/__init__.py index df8978a..e89914c 100644 --- a/ram/ram/__init__.py +++ b/ram/ram/__init__.py @@ -1,4 +1,4 @@ from ram.utils import git_suffix -__version__ = "0.15.3" +__version__ = "0.15.4" __version__ += git_suffix(__file__) diff --git a/ram/roster/migrations/0031_alter_rollingstockdocument_unique_together_and_more.py b/ram/roster/migrations/0031_alter_rollingstockdocument_unique_together_and_more.py new file mode 100644 index 0000000..4998772 --- /dev/null +++ b/ram/roster/migrations/0031_alter_rollingstockdocument_unique_together_and_more.py @@ -0,0 +1,23 @@ +# Generated by Django 5.1.4 on 2025-01-08 22:22 + +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("roster", "0030_rollingstock_price"), + ] + + operations = [ + migrations.AlterUniqueTogether( + name="rollingstockdocument", + unique_together=set(), + ), + migrations.AddConstraint( + model_name="rollingstockdocument", + constraint=models.UniqueConstraint( + fields=("rolling_stock", "file"), name="unique_stock_file" + ), + ), + ] diff --git a/ram/roster/models.py b/ram/roster/models.py index 803c5b7..fc6804d 100644 --- a/ram/roster/models.py +++ b/ram/roster/models.py @@ -156,8 +156,13 @@ class RollingStockDocument(Document): RollingStock, on_delete=models.CASCADE, related_name="document" ) - class Meta(object): - unique_together = ("rolling_stock", "file") + class Meta: + constraints = [ + models.UniqueConstraint( + fields=["rolling_stock", "file"], + name="unique_stock_file" + ) + ] def rolling_stock_image_upload(instance, filename):