mirror of
https://github.com/daniviga/django-ram.git
synced 2026-02-28 05:32:28 +01:00
Code refactoring to simplify template data contexts (#55)
* Fix a search filter when no catalogs are returned * Code refactoring to simplify templates * Remove duplicated code * Remove dead code * More improvements, clean up and add featured items in homepage * Fix a type and better page navigation
This commit is contained in:
@@ -44,7 +44,9 @@ class RollingClass(admin.ModelAdmin):
|
||||
@admin.display(description="Country")
|
||||
def country_flag(self, obj):
|
||||
return format_html(
|
||||
'<img src="{}" /> {}', obj.country.flag, obj.country.name
|
||||
'<img src="{}" title="{}" />',
|
||||
obj.company.country.flag,
|
||||
obj.company.country.name,
|
||||
)
|
||||
|
||||
|
||||
@@ -128,9 +130,12 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
"item_number",
|
||||
"company",
|
||||
"country_flag",
|
||||
"featured",
|
||||
"published",
|
||||
)
|
||||
list_filter = (
|
||||
"featured",
|
||||
"published",
|
||||
"rolling_class__type__category",
|
||||
"rolling_class__type",
|
||||
"rolling_class__company__name",
|
||||
@@ -152,7 +157,7 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
@admin.display(description="Country")
|
||||
def country_flag(self, obj):
|
||||
return format_html(
|
||||
'<img src="{}" /> {}', obj.country.flag, obj.country.name
|
||||
'<img src="{}" title="{}" />', obj.country.flag, obj.country.name
|
||||
)
|
||||
|
||||
fieldsets = (
|
||||
@@ -162,6 +167,7 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
"fields": (
|
||||
"preview",
|
||||
"published",
|
||||
"featured",
|
||||
"rolling_class",
|
||||
"road_number",
|
||||
"scale",
|
||||
@@ -224,8 +230,8 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
if obj.invoice.exists():
|
||||
html = format_html_join(
|
||||
"<br>",
|
||||
"<a href=\"{}\" target=\"_blank\">{}</a>",
|
||||
((i.file.url, i) for i in obj.invoice.all())
|
||||
'<a href="{}" target="_blank">{}</a>',
|
||||
((i.file.url, i) for i in obj.invoice.all()),
|
||||
)
|
||||
else:
|
||||
html = "-"
|
||||
@@ -296,4 +302,38 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
return generate_csv(header, data, "rolling_stock.csv")
|
||||
|
||||
download_csv.short_description = "Download selected items as CSV"
|
||||
actions = [publish, unpublish, download_csv]
|
||||
|
||||
def set_featured(modeladmin, request, queryset):
|
||||
count = queryset.count()
|
||||
if count > settings.FEATURED_ITEMS_MAX:
|
||||
modeladmin.message_user(
|
||||
request,
|
||||
"You can only mark up to {} items as featured.".format(
|
||||
settings.FEATURED_ITEMS_MAX
|
||||
),
|
||||
level="error",
|
||||
)
|
||||
return
|
||||
featured = RollingStock.objects.filter(featured=True).count()
|
||||
if featured + count > settings.FEATURED_ITEMS_MAX:
|
||||
modeladmin.message_user(
|
||||
request,
|
||||
"There are already {} featured items. You can only mark {} more items as featured.".format( # noqa: E501
|
||||
featured,
|
||||
settings.FEATURED_ITEMS_MAX - featured,
|
||||
),
|
||||
level="error",
|
||||
)
|
||||
return
|
||||
queryset.update(featured=True)
|
||||
|
||||
set_featured.short_description = "Mark selected rolling stock as featured"
|
||||
|
||||
def unset_featured(modeladmin, request, queryset):
|
||||
queryset.update(featured=False)
|
||||
|
||||
unset_featured.short_description = (
|
||||
"Unmark selected rolling stock as featured"
|
||||
)
|
||||
|
||||
actions = [publish, unpublish, set_featured, unset_featured, download_csv]
|
||||
|
||||
21
ram/roster/migrations/0039_rollingstock_featured.py
Normal file
21
ram/roster/migrations/0039_rollingstock_featured.py
Normal file
@@ -0,0 +1,21 @@
|
||||
# Generated by Django 6.0 on 2025-12-24 13:39
|
||||
|
||||
from django.db import migrations, models
|
||||
|
||||
|
||||
class Migration(migrations.Migration):
|
||||
|
||||
dependencies = [
|
||||
("roster", "0038_alter_rollingstock_rolling_class"),
|
||||
]
|
||||
|
||||
operations = [
|
||||
migrations.AddField(
|
||||
model_name="rollingstock",
|
||||
name="featured",
|
||||
field=models.BooleanField(
|
||||
default=False,
|
||||
help_text="Featured rolling stock will appear on the homepage",
|
||||
),
|
||||
),
|
||||
]
|
||||
@@ -5,6 +5,7 @@ from django.db import models
|
||||
from django.urls import reverse
|
||||
from django.conf import settings
|
||||
from django.dispatch import receiver
|
||||
from django.core.exceptions import ValidationError
|
||||
|
||||
from tinymce import models as tinymce
|
||||
|
||||
@@ -82,9 +83,7 @@ class RollingStock(BaseModel):
|
||||
help_text="Catalog item number or code",
|
||||
)
|
||||
item_number_slug = models.CharField(
|
||||
max_length=32,
|
||||
blank=True,
|
||||
editable=False
|
||||
max_length=32, blank=True, editable=False
|
||||
)
|
||||
set = models.BooleanField(
|
||||
default=False,
|
||||
@@ -113,6 +112,10 @@ class RollingStock(BaseModel):
|
||||
null=True,
|
||||
blank=True,
|
||||
)
|
||||
featured = models.BooleanField(
|
||||
default=False,
|
||||
help_text="Featured rolling stock will appear on the homepage",
|
||||
)
|
||||
tags = models.ManyToManyField(
|
||||
Tag, related_name="rolling_stock", blank=True
|
||||
)
|
||||
@@ -165,10 +168,18 @@ class RollingStock(BaseModel):
|
||||
os.path.join(
|
||||
settings.MEDIA_ROOT, "images", "rollingstock", str(self.uuid)
|
||||
),
|
||||
ignore_errors=True
|
||||
ignore_errors=True,
|
||||
)
|
||||
super(RollingStock, self).delete(*args, **kwargs)
|
||||
|
||||
def clean(self, *args, **kwargs):
|
||||
if self.featured:
|
||||
MAX = settings.FEATURED_ITEMS_MAX
|
||||
if RollingStock.objects.filter(featured=True).count() > MAX - 1:
|
||||
raise ValidationError(
|
||||
"There are already {} featured items".format(MAX)
|
||||
)
|
||||
|
||||
|
||||
@receiver(models.signals.pre_save, sender=RollingStock)
|
||||
def pre_save_internal_fields(sender, instance, *args, **kwargs):
|
||||
@@ -185,10 +196,7 @@ def pre_save_internal_fields(sender, instance, *args, **kwargs):
|
||||
|
||||
def rolling_stock_image_upload(instance, filename):
|
||||
return os.path.join(
|
||||
"images",
|
||||
"rollingstock",
|
||||
str(instance.rolling_stock.uuid),
|
||||
filename
|
||||
"images", "rollingstock", str(instance.rolling_stock.uuid), filename
|
||||
)
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user