# This file is part of FNP-Redakcja, licensed under GNU Affero GPLv3 or later.
# Copyright © Fundacja Nowoczesna Polska. See NOTICE for more information.
#
+from collections import Counter
from django.contrib import admin
from django.utils.translation import gettext_lazy as _
from fnpdjango.actions import export_as_csv_action
qs = qs.prefetch_related("authors", "translators")
return qs
+ def estimated_costs(self, obj):
+ return "\n".join(
+ "{}: {} zł".format(
+ work_type.name,
+ cost or '—'
+ )
+ for work_type, cost in obj.get_estimated_costs().items()
+ )
+
admin.site.register(models.Book, BookAdmin)
autocomplete_fields = []
prepopulated_fields = {"slug": ("name",)}
search_fields = ["name"]
+ fields = ['name', 'slug', 'estimated_costs']
+ readonly_fields = ['estimated_costs']
inlines = [AuthorInline, BookInline]
+ def estimated_costs(self, obj):
+ costs = Counter()
+ for book in obj.book_set.all():
+ for k, v in book.get_estimated_costs().items():
+ costs[k] += v or 0
+
+ for author in obj.author_set.all():
+ for book in author.book_set.all():
+ for k, v in book.get_estimated_costs().items():
+ costs[k] += v or 0
+ for book in author.translated_book_set.all():
+ for k, v in book.get_estimated_costs().items():
+ costs[k] += v or 0
+
+ return "\n".join(
+ "{}: {} zł".format(
+ work_type.name,
+ cost or '—'
+ )
+ for work_type, cost in costs.items()
+ )
+
admin.site.register(models.Collection, CollectionAdmin)
DBook = apps.get_model("documents", "Book")
return DBook.objects.filter(dc_slug=self.slug)
- def estimated_costs(self):
- return "\n".join(
- "{}: {} zł".format(
- work_type.name,
- work_type.calculate(self) or '—'
- )
+ def get_estimated_costs(self):
+ return {
+ work_type: work_type.calculate(self)
for work_type in WorkType.objects.all()
- )
+ }
class Collection(models.Model):