From 7a8610e5869654bd2e8351b34b9e722ce6de73bd Mon Sep 17 00:00:00 2001 From: Radek Czajka Date: Wed, 21 Feb 2024 15:14:57 +0100 Subject: [PATCH] Funding balance sheet changed. --- ..._annotation_spent_link_alter_spent_book.py | 30 ++++++++++++++ src/funding/migrations/0013_missing_spent.py | 41 +++++++++++++++++++ src/funding/models.py | 4 +- src/funding/templates/funding/wlfund.html | 22 +++++++--- src/funding/views.py | 13 ++---- 5 files changed, 94 insertions(+), 16 deletions(-) create mode 100644 src/funding/migrations/0012_spent_annotation_spent_link_alter_spent_book.py create mode 100644 src/funding/migrations/0013_missing_spent.py diff --git a/src/funding/migrations/0012_spent_annotation_spent_link_alter_spent_book.py b/src/funding/migrations/0012_spent_annotation_spent_link_alter_spent_book.py new file mode 100644 index 000000000..4277fb757 --- /dev/null +++ b/src/funding/migrations/0012_spent_annotation_spent_link_alter_spent_book.py @@ -0,0 +1,30 @@ +# Generated by Django 4.0.8 on 2024-02-21 11:52 + +from django.db import migrations, models +import django.db.models.deletion + + +class Migration(migrations.Migration): + + dependencies = [ + ('catalogue', '0046_alter_book_options_alter_bookmedia_options_and_more'), + ('funding', '0011_alter_funding_customer_ip_alter_funding_notify_key_and_more'), + ] + + operations = [ + migrations.AddField( + model_name='spent', + name='annotation', + field=models.CharField(blank=True, help_text="np. 'audiobook'", max_length=255, verbose_name='adnotacja'), + ), + migrations.AddField( + model_name='spent', + name='link', + field=models.URLField(blank=True, help_text='zamiast książki, np. kolekcja'), + ), + migrations.AlterField( + model_name='spent', + name='book', + field=models.ForeignKey(blank=True, null=True, on_delete=django.db.models.deletion.PROTECT, to='catalogue.book'), + ), + ] diff --git a/src/funding/migrations/0013_missing_spent.py b/src/funding/migrations/0013_missing_spent.py new file mode 100644 index 000000000..d7da1bfe0 --- /dev/null +++ b/src/funding/migrations/0013_missing_spent.py @@ -0,0 +1,41 @@ +# Generated by Django 4.0.8 on 2024-02-21 11:55 + +from django.db import migrations, models + + +def populate_spent(apps, schema_editor): + Offer = apps.get_model('funding', 'Offer') + Spent = apps.get_model('funding', 'Spent') + Book = apps.get_model('catalogue', 'Book') + for o in Offer.objects.all(): + if Spent.objects.filter(book__slug=o.slug).exists(): + continue + s = o.funding_set.exclude(completed_at=None).aggregate(s=models.Sum('amount'))['s'] or 0 + if s >= o.target: + try: + book = Book.objects.get(slug=o.slug) + link = '' + except Book.DoesNotExist: + book = None + link = o.slug + Spent.objects.create( + book=book, + link=link, + amount=o.target, + timestamp=o.end, + annotation='auto' + ) + + +class Migration(migrations.Migration): + + dependencies = [ + ('funding', '0012_spent_annotation_spent_link_alter_spent_book'), + ] + + operations = [ + migrations.RunPython( + populate_spent, + migrations.RunPython.noop + ) + ] diff --git a/src/funding/models.py b/src/funding/models.py index 23afa3c04..12fa0ca3e 100644 --- a/src/funding/models.py +++ b/src/funding/models.py @@ -372,9 +372,11 @@ class PayUNotification(club.payu.models.Notification): class Spent(models.Model): """ Some of the remaining money spent on a book. """ - book = models.ForeignKey(Book, models.PROTECT) + book = models.ForeignKey(Book, models.PROTECT, null=True, blank=True) + link = models.URLField(blank=True, help_text='zamiast książki, np. kolekcja') amount = models.DecimalField('kwota', decimal_places=2, max_digits=10) timestamp = models.DateField('kiedy') + annotation = models.CharField('adnotacja', max_length=255, blank=True, help_text="np. 'audiobook'") class Meta: verbose_name = 'pieniądze wydane na książkę' diff --git a/src/funding/templates/funding/wlfund.html b/src/funding/templates/funding/wlfund.html index 5a1ba61b4..bff10921e 100644 --- a/src/funding/templates/funding/wlfund.html +++ b/src/funding/templates/funding/wlfund.html @@ -26,7 +26,8 @@ {% trans "Data" %}: {% trans "Rozdysponowanie środków" %}: - {% trans "Kwota" %}: + {% trans "Zebrane" %}: + {% trans "Wydane" %}: {% trans "Bilans" %}: @@ -35,20 +36,29 @@ {{ entry.timestamp }} - {% trans "Pieniądze przeznaczone na opublikowanie książki" %}: - {{ entry.book }} + {% trans "Pieniądze przeznaczone na publikację" %}: + {% if entry.book %} + {{ entry.book }} + {% if entry.annotation %} + ({{ entry.annotation }}) + {% endif %} + {% else %} + {{ entry.annotation }} + {% endif %} - -{{ entry.amount }} zł + + {{ entry.amount }} zł {{ entry.total }} zł {% else %} {{ entry.end }} - {% trans "Pieniądze pozostałe ze zbiórki na" %}: + {% trans "Zbiórka" %}: {{ entry }} - +{{ entry.wlfund }} zł + {{ entry.wlfund }} zł + {{ entry.total }} zł {% endif %} diff --git a/src/funding/views.py b/src/funding/views.py index 9b4378062..5be8c4b89 100644 --- a/src/funding/views.py +++ b/src/funding/views.py @@ -51,20 +51,15 @@ class WLFundView(TemplateView): offers = [] for o in Offer.past(): - if o.is_win(): - o.wlfund = o.sum() - o.target - if o.wlfund > 0: - offers.append(o) - else: - o.wlfund = o.sum() - if o.wlfund > 0: - offers.append(o) + o.wlfund = o.sum() + if o.wlfund > 0: + offers.append(o) amount = sum(o.wlfund for o in offers) - sum(o.amount for o in Spent.objects.all()) ctx['amount'] = amount ctx['log'] = add_total(amount, mix( - (offers, lambda x: x.end, 'offer'), (Spent.objects.all().select_related(), lambda x: x.timestamp, 'spent'), + (offers, lambda x: x.end, 'offer'), )) return ctx -- 2.20.1