Skip to content

Commit

Permalink
Speedup inlines using autocomplete field and add more previews
Browse files Browse the repository at this point in the history
  • Loading branch information
daniviga committed Nov 4, 2024
1 parent 456272b commit 4d9e462
Show file tree
Hide file tree
Showing 8 changed files with 21 additions and 8 deletions.
1 change: 1 addition & 0 deletions ram/bookshelf/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,7 @@ class BookPropertyInline(admin.TabularInline):
model = BookProperty
min_num = 0
extra = 0
autocomplete_fields = ("property",)


@admin.register(Book)
Expand Down
3 changes: 2 additions & 1 deletion ram/consist/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down
3 changes: 3 additions & 0 deletions ram/consist/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -60,6 +60,9 @@ class Meta(object):
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

Expand Down
2 changes: 1 addition & 1 deletion ram/ram/__init__.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from ram.utils import git_suffix

__version__ = "0.12.5"
__version__ = "0.13.0"
__version__ += git_suffix(__file__)
4 changes: 2 additions & 2 deletions ram/ram/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -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"

Expand Down
6 changes: 3 additions & 3 deletions ram/ram/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
)


Expand Down
7 changes: 6 additions & 1 deletion ram/roster/admin.py
Original file line number Diff line number Diff line change
Expand Up @@ -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 = (
Expand Down Expand Up @@ -50,6 +52,7 @@ class RollingStockPropertyInline(admin.TabularInline):
model = RollingStockProperty
min_num = 0
extra = 0
autocomplete_fields = ("property",)


class RollingStockJournalInline(admin.TabularInline):
Expand Down Expand Up @@ -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",
Expand Down Expand Up @@ -136,6 +140,7 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
None,
{
"fields": (
"preview",
"rolling_class",
"road_number",
"scale",
Expand Down
3 changes: 3 additions & 0 deletions ram/roster/models.py
Original file line number Diff line number Diff line change
Expand Up @@ -120,6 +120,9 @@ def __str__(self):
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)

Expand Down

0 comments on commit 4d9e462

Please sign in to comment.