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.contrib import admin
|
||||||
from django.utils.html import format_html
|
# from django.forms import BaseInlineFormSet # for future reference
|
||||||
from adminsortable2.admin import SortableAdminBase, SortableInlineAdminMixin
|
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.admin import publish, unpublish
|
||||||
|
from ram.utils import generate_csv
|
||||||
from consist.models import Consist, ConsistItem
|
from consist.models import Consist, ConsistItem
|
||||||
|
|
||||||
|
|
||||||
|
# for future reference
|
||||||
|
# class ConsistItemInlineFormSet(CustomInlineFormSetMixin, BaseInlineFormSet):
|
||||||
|
# def clean(self):
|
||||||
|
# super().clean()
|
||||||
|
|
||||||
|
|
||||||
class ConsistItemInline(SortableInlineAdminMixin, admin.TabularInline):
|
class ConsistItemInline(SortableInlineAdminMixin, admin.TabularInline):
|
||||||
model = ConsistItem
|
model = ConsistItem
|
||||||
min_num = 1
|
min_num = 1
|
||||||
@@ -14,10 +29,11 @@ class ConsistItemInline(SortableInlineAdminMixin, admin.TabularInline):
|
|||||||
readonly_fields = (
|
readonly_fields = (
|
||||||
"preview",
|
"preview",
|
||||||
"published",
|
"published",
|
||||||
"address",
|
"scale",
|
||||||
"type",
|
|
||||||
"company",
|
"company",
|
||||||
|
"type",
|
||||||
"era",
|
"era",
|
||||||
|
"address",
|
||||||
)
|
)
|
||||||
|
|
||||||
|
|
||||||
@@ -46,10 +62,10 @@ class ConsistAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
"fields": (
|
"fields": (
|
||||||
"published",
|
"published",
|
||||||
"identifier",
|
"identifier",
|
||||||
"consist_address",
|
|
||||||
"company",
|
"company",
|
||||||
"era",
|
|
||||||
"scale",
|
"scale",
|
||||||
|
"era",
|
||||||
|
"consist_address",
|
||||||
"description",
|
"description",
|
||||||
"image",
|
"image",
|
||||||
"tags",
|
"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]
|
||||||
|
@@ -107,6 +107,10 @@ class ConsistItem(models.Model):
|
|||||||
def preview(self):
|
def preview(self):
|
||||||
return self.rolling_stock.image.first().image_thumbnail(100)
|
return self.rolling_stock.image.first().image_thumbnail(100)
|
||||||
|
|
||||||
|
@property
|
||||||
|
def scale(self):
|
||||||
|
return self.rolling_stock.scale
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def type(self):
|
def type(self):
|
||||||
return self.rolling_stock.rolling_class.type
|
return self.rolling_stock.rolling_class.type
|
||||||
|
@@ -140,6 +140,7 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
"rolling_class__identifier",
|
"rolling_class__identifier",
|
||||||
"rolling_class__company__name",
|
"rolling_class__company__name",
|
||||||
"manufacturer__name",
|
"manufacturer__name",
|
||||||
|
"scale",
|
||||||
"road_number",
|
"road_number",
|
||||||
"address",
|
"address",
|
||||||
"item_number",
|
"item_number",
|
||||||
@@ -229,9 +230,12 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
|
|
||||||
def download_csv(modeladmin, request, queryset):
|
def download_csv(modeladmin, request, queryset):
|
||||||
header = [
|
header = [
|
||||||
|
"ID",
|
||||||
"Name",
|
"Name",
|
||||||
|
"Class",
|
||||||
|
"Type",
|
||||||
"Company",
|
"Company",
|
||||||
"Identifier",
|
"Country",
|
||||||
"Road Number",
|
"Road Number",
|
||||||
"Manufacturer",
|
"Manufacturer",
|
||||||
"Scale",
|
"Scale",
|
||||||
@@ -258,9 +262,12 @@ class RollingStockAdmin(SortableAdminBase, admin.ModelAdmin):
|
|||||||
)
|
)
|
||||||
data.append(
|
data.append(
|
||||||
[
|
[
|
||||||
|
obj.uuid,
|
||||||
obj.__str__(),
|
obj.__str__(),
|
||||||
obj.rolling_class.company.name,
|
|
||||||
obj.rolling_class.identifier,
|
obj.rolling_class.identifier,
|
||||||
|
obj.rolling_class.type,
|
||||||
|
obj.rolling_class.company.name,
|
||||||
|
obj.rolling_class.company.country,
|
||||||
obj.road_number,
|
obj.road_number,
|
||||||
obj.manufacturer.name,
|
obj.manufacturer.name,
|
||||||
obj.scale.scale,
|
obj.scale.scale,
|
||||||
|
Reference in New Issue
Block a user