mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-04 13:17:50 +02:00
Implement CSV export for cosists
This commit is contained in:
@@ -1,11 +1,26 @@
|
||||
import html
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib import admin
|
||||
from django.utils.html import format_html
|
||||
from adminsortable2.admin import SortableAdminBase, SortableInlineAdminMixin
|
||||
# from django.forms import BaseInlineFormSet # for future reference
|
||||
from django.utils.html import format_html, strip_tags
|
||||
from adminsortable2.admin import (
|
||||
SortableAdminBase,
|
||||
SortableInlineAdminMixin,
|
||||
# CustomInlineFormSetMixin, # for future reference
|
||||
)
|
||||
|
||||
from ram.admin import publish, unpublish
|
||||
from ram.utils import generate_csv
|
||||
from consist.models import Consist, ConsistItem
|
||||
|
||||
|
||||
# for future reference
|
||||
# class ConsistItemInlineFormSet(CustomInlineFormSetMixin, BaseInlineFormSet):
|
||||
# def clean(self):
|
||||
# super().clean()
|
||||
|
||||
|
||||
class ConsistItemInline(SortableInlineAdminMixin, admin.TabularInline):
|
||||
model = ConsistItem
|
||||
min_num = 1
|
||||
@@ -14,10 +29,11 @@ class ConsistItemInline(SortableInlineAdminMixin, admin.TabularInline):
|
||||
readonly_fields = (
|
||||
"preview",
|
||||
"published",
|
||||
"address",
|
||||
"type",
|
||||
"scale",
|
||||
"company",
|
||||
"type",
|
||||
"era",
|
||||
"address",
|
||||
)
|
||||
|
||||
|
||||
@@ -46,10 +62,10 @@ class ConsistAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
"fields": (
|
||||
"published",
|
||||
"identifier",
|
||||
"consist_address",
|
||||
"company",
|
||||
"era",
|
||||
"scale",
|
||||
"era",
|
||||
"consist_address",
|
||||
"description",
|
||||
"image",
|
||||
"tags",
|
||||
@@ -71,4 +87,55 @@ class ConsistAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
},
|
||||
),
|
||||
)
|
||||
actions = [publish, unpublish]
|
||||
|
||||
def download_csv(modeladmin, request, queryset):
|
||||
header = [
|
||||
"ID",
|
||||
"Name",
|
||||
"Published",
|
||||
"Company",
|
||||
"Country",
|
||||
"Address",
|
||||
"Scale",
|
||||
"Era",
|
||||
"Description",
|
||||
"Tags",
|
||||
"Length",
|
||||
"Composition",
|
||||
"Item name",
|
||||
"Item type",
|
||||
"Item ID",
|
||||
]
|
||||
data = []
|
||||
for obj in queryset:
|
||||
for item in obj.consist_item.all():
|
||||
types = " + ".join(
|
||||
"{}x {}".format(t["count"], t["type"])
|
||||
for t in obj.get_type_count()
|
||||
)
|
||||
data.append(
|
||||
[
|
||||
obj.uuid,
|
||||
obj.__str__(),
|
||||
"X" if obj.published else "",
|
||||
obj.company.name,
|
||||
obj.company.country,
|
||||
obj.consist_address,
|
||||
obj.scale.scale,
|
||||
obj.era,
|
||||
html.unescape(strip_tags(obj.description)),
|
||||
settings.CSV_SEPARATOR_ALT.join(
|
||||
t.name for t in obj.tags.all()
|
||||
),
|
||||
obj.length,
|
||||
types,
|
||||
item.rolling_stock.__str__(),
|
||||
item.type,
|
||||
item.rolling_stock.uuid,
|
||||
]
|
||||
)
|
||||
|
||||
return generate_csv(header, data, "consists.csv")
|
||||
download_csv.short_description = "Download selected items as CSV"
|
||||
|
||||
actions = [publish, unpublish, download_csv]
|
||||
|
Reference in New Issue
Block a user