mirror of
https://github.com/daniviga/django-ram.git
synced 2025-08-04 13:17:50 +02:00
Add a CSV export functionality in admin and add price fields (#41)
* Implement an action do download data in csv * Refactor CSV download * Move price to main models and add csv to bookshelf * Update template and API * Small refactoring
This commit is contained in:
@@ -1,6 +1,12 @@
|
||||
import html
|
||||
|
||||
from django.conf import settings
|
||||
from django.contrib import admin
|
||||
from django.utils.html import strip_tags
|
||||
from adminsortable2.admin import SortableAdminBase, SortableInlineAdminMixin
|
||||
|
||||
from ram.utils import generate_csv
|
||||
from portal.utils import get_site_conf
|
||||
from bookshelf.models import (
|
||||
BaseBookProperty,
|
||||
BaseBookImage,
|
||||
@@ -71,12 +77,28 @@ class BookAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
"number_of_pages",
|
||||
"publication_year",
|
||||
"description",
|
||||
"purchase_date",
|
||||
"notes",
|
||||
"tags",
|
||||
)
|
||||
},
|
||||
),
|
||||
(
|
||||
"Purchase data",
|
||||
{
|
||||
"fields": (
|
||||
"purchase_date",
|
||||
"price",
|
||||
)
|
||||
},
|
||||
),
|
||||
(
|
||||
"Notes",
|
||||
{
|
||||
"classes": ("collapse",),
|
||||
"fields": (
|
||||
"notes",
|
||||
)
|
||||
},
|
||||
),
|
||||
(
|
||||
"Audit",
|
||||
{
|
||||
@@ -89,13 +111,66 @@ class BookAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
),
|
||||
)
|
||||
|
||||
def get_form(self, request, obj=None, **kwargs):
|
||||
form = super().get_form(request, obj, **kwargs)
|
||||
form.base_fields["price"].label = "Price ({})".format(
|
||||
get_site_conf().currency
|
||||
)
|
||||
return form
|
||||
|
||||
@admin.display(description="Publisher")
|
||||
def get_publisher(self, obj):
|
||||
return obj.publisher.name
|
||||
|
||||
@admin.display(description="Authors")
|
||||
def get_authors(self, obj):
|
||||
return ", ".join(a.short_name() for a in obj.authors.all())
|
||||
return obj.authors_list
|
||||
|
||||
def download_csv(modeladmin, request, queryset):
|
||||
header = [
|
||||
"Title",
|
||||
"Authors",
|
||||
"Publisher",
|
||||
"ISBN",
|
||||
"Language",
|
||||
"Number of Pages",
|
||||
"Publication Year",
|
||||
"Description",
|
||||
"Tags",
|
||||
"Purchase Date",
|
||||
"Price ({})".format(get_site_conf().currency),
|
||||
"Notes",
|
||||
"Properties",
|
||||
]
|
||||
|
||||
data = []
|
||||
for obj in queryset:
|
||||
properties = settings.CSV_SEPARATOR_ALT.join(
|
||||
"{}:{}".format(property.property.name, property.value)
|
||||
for property in obj.property.all()
|
||||
)
|
||||
data.append([
|
||||
obj.title,
|
||||
obj.authors_list.replace(",", settings.CSV_SEPARATOR_ALT),
|
||||
obj.publisher.name,
|
||||
obj.ISBN,
|
||||
dict(settings.LANGUAGES)[obj.language],
|
||||
obj.number_of_pages,
|
||||
obj.publication_year,
|
||||
html.unescape(strip_tags(obj.description)),
|
||||
settings.CSV_SEPARATOR_ALT.join(
|
||||
t.name for t in obj.tags.all()
|
||||
),
|
||||
obj.purchase_date,
|
||||
obj.price,
|
||||
html.unescape(strip_tags(obj.notes)),
|
||||
properties,
|
||||
])
|
||||
|
||||
return generate_csv(header, data, "bookshelf_books.csv")
|
||||
|
||||
download_csv.short_description = "Download selected items as CSV"
|
||||
actions = [download_csv]
|
||||
|
||||
|
||||
@admin.register(Author)
|
||||
@@ -146,12 +221,28 @@ class CatalogAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
"number_of_pages",
|
||||
"publication_year",
|
||||
"description",
|
||||
"purchase_date",
|
||||
"notes",
|
||||
"tags",
|
||||
)
|
||||
},
|
||||
),
|
||||
(
|
||||
"Purchase data",
|
||||
{
|
||||
"fields": (
|
||||
"purchase_date",
|
||||
"price",
|
||||
)
|
||||
},
|
||||
),
|
||||
(
|
||||
"Notes",
|
||||
{
|
||||
"classes": ("collapse",),
|
||||
"fields": (
|
||||
"notes",
|
||||
)
|
||||
},
|
||||
),
|
||||
(
|
||||
"Audit",
|
||||
{
|
||||
@@ -164,6 +255,61 @@ class CatalogAdmin(SortableAdminBase, admin.ModelAdmin):
|
||||
),
|
||||
)
|
||||
|
||||
def get_form(self, request, obj=None, **kwargs):
|
||||
form = super().get_form(request, obj, **kwargs)
|
||||
form.base_fields["price"].label = "Price ({})".format(
|
||||
get_site_conf().currency
|
||||
)
|
||||
return form
|
||||
|
||||
@admin.display(description="Scales")
|
||||
def get_scales(self, obj):
|
||||
return "/".join(s.scale for s in obj.scales.all())
|
||||
|
||||
def download_csv(modeladmin, request, queryset):
|
||||
header = [
|
||||
"Catalog",
|
||||
"Manufacturer",
|
||||
"Years",
|
||||
"Scales",
|
||||
"ISBN",
|
||||
"Language",
|
||||
"Number of Pages",
|
||||
"Publication Year",
|
||||
"Description",
|
||||
"Tags",
|
||||
"Purchase Date",
|
||||
"Price ({})".format(get_site_conf().currency),
|
||||
"Notes",
|
||||
"Properties",
|
||||
]
|
||||
|
||||
data = []
|
||||
for obj in queryset:
|
||||
properties = settings.CSV_SEPARATOR_ALT.join(
|
||||
"{}:{}".format(property.property.name, property.value)
|
||||
for property in obj.property.all()
|
||||
)
|
||||
data.append([
|
||||
obj.__str__,
|
||||
obj.manufacturer.name,
|
||||
obj.years,
|
||||
obj.get_scales,
|
||||
obj.ISBN,
|
||||
dict(settings.LANGUAGES)[obj.language],
|
||||
obj.number_of_pages,
|
||||
obj.publication_year,
|
||||
html.unescape(strip_tags(obj.description)),
|
||||
settings.CSV_SEPARATOR_ALT.join(
|
||||
t.name for t in obj.tags.all()
|
||||
),
|
||||
obj.purchase_date,
|
||||
obj.price,
|
||||
html.unescape(strip_tags(obj.notes)),
|
||||
properties,
|
||||
])
|
||||
|
||||
return generate_csv(header, data, "bookshelf_catalogs.csv")
|
||||
|
||||
download_csv.short_description = "Download selected items as CSV"
|
||||
actions = [download_csv]
|
||||
|
Reference in New Issue
Block a user