Speedup inlines using autocomplete field and add more previews (#38)

This commit is contained in:
2024-11-04 11:33:28 +01:00
committed by GitHub
parent 456272b93a
commit d0854a4cff
8 changed files with 21 additions and 8 deletions

View File

@@ -16,6 +16,7 @@ class BookPropertyInline(admin.TabularInline):
model = BookProperty
min_num = 0
extra = 0
autocomplete_fields = ("property",)
@admin.register(Book)

View File

@@ -8,7 +8,8 @@ class ConsistItemInline(SortableInlineAdminMixin, admin.TabularInline):
model = ConsistItem
min_num = 1
extra = 0
readonly_fields = ("address", "type", "company", "era")
autocomplete_fields = ("rolling_stock",)
readonly_fields = ("preview", "address", "type", "company", "era")
@admin.register(Consist)

View File

@@ -60,6 +60,9 @@ class ConsistItem(models.Model):
def __str__(self):
return "{0}".format(self.rolling_stock)
def preview(self):
return self.rolling_stock.image.first().image_thumbnail(100)
def type(self):
return self.rolling_stock.rolling_class.type

View File

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

View File

@@ -36,8 +36,8 @@ class Image(models.Model):
storage=DeduplicatedStorage,
)
def image_thumbnail(self):
return get_image_preview(self.image.url)
def image_thumbnail(self, max_size=150):
return get_image_preview(self.image.url, max_size)
image_thumbnail.short_description = "Preview"

View File

@@ -44,10 +44,10 @@ def git_suffix(fname):
return gh
def get_image_preview(url):
def get_image_preview(url, max_size=150):
return format_html(
'<img src="%s" style="max-width: 150px; max-height: 150px;'
'background-color: #eee;" />' % url
'<img src="{src}" style="max-width: {size}px; max-height: {size}px;'
'background-color: #eee;" />'.format(src=url, size=max_size)
)

View File

@@ -16,11 +16,13 @@ class RollingClassPropertyInline(admin.TabularInline):
model = RollingClassProperty
min_num = 0
extra = 0
autocomplete_fields = ("property",)
@admin.register(RollingClass)
class RollingClass(admin.ModelAdmin):
inlines = (RollingClassPropertyInline,)
autocomplete_fields = ("manufacturer",)
list_display = ("__str__", "type", "company")
list_filter = ("company", "type__category", "type")
search_fields = (
@@ -50,6 +52,7 @@ class RollingStockPropertyInline(admin.TabularInline):
model = RollingStockProperty
min_num = 0
extra = 0
autocomplete_fields = ("property",)
class RollingStockJournalInline(admin.TabularInline):
@@ -104,7 +107,8 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
RollingStockDocInline,
RollingStockJournalInline,
)
readonly_fields = ("creation_time", "updated_time")
autocomplete_fields = ("rolling_class",)
readonly_fields = ("preview", "creation_time", "updated_time")
list_display = (
"__str__",
"address",
@@ -136,6 +140,7 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
None,
{
"fields": (
"preview",
"rolling_class",
"road_number",
"scale",

View File

@@ -120,6 +120,9 @@ class RollingStock(models.Model):
def get_absolute_url(self):
return reverse("rolling_stock", kwargs={"uuid": self.uuid})
def preview(self):
return self.image.first().image_thumbnail(350)
def country(self):
return str(self.rolling_class.company.country)