From c589bdc8926f9fb7d4b34a2c6569fa4feae833f0 Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Wed, 28 Oct 2020 12:41:11 +0100 Subject: [PATCH] Estimated costs for collections. --- src/catalogue/admin.py | 34 ++++++++++++++++++++++++++++++++++ src/catalogue/models.py | 11 ++++------- 2 files changed, 38 insertions(+), 7 deletions(-) diff --git a/src/catalogue/admin.py b/src/catalogue/admin.py index 35345216..a8fc82a5 100644 --- a/src/catalogue/admin.py +++ b/src/catalogue/admin.py @@ -1,6 +1,7 @@ # 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 @@ -93,6 +94,15 @@ class BookAdmin(WikidataAdminMixin, admin.ModelAdmin): 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) @@ -112,8 +122,32 @@ class CollectionAdmin(admin.ModelAdmin): 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) diff --git a/src/catalogue/models.py b/src/catalogue/models.py index bb738a26..ce7a5722 100644 --- a/src/catalogue/models.py +++ b/src/catalogue/models.py @@ -168,14 +168,11 @@ class Book(WikidataMixin, models.Model): 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): -- 2.20.1