From 27c599f0a916d4faff60c816d7e1298e18604bb2 Mon Sep 17 00:00:00 2001 From: Gabriel Augendre Date: Sat, 4 Jun 2016 01:55:27 +0200 Subject: [PATCH] Add latest refunds view --- refunding/admin.py | 2 +- refunding/models.py | 4 ++-- .../templates/refunding/refunds_list.html | 19 +++++++++++++++++++ refunding/urls.py | 3 ++- refunding/views.py | 12 +++++++++++- 5 files changed, 35 insertions(+), 5 deletions(-) create mode 100644 refunding/templates/refunding/refunds_list.html diff --git a/refunding/admin.py b/refunding/admin.py index 906bdf9..44c5ab4 100644 --- a/refunding/admin.py +++ b/refunding/admin.py @@ -10,7 +10,7 @@ class RefundAdmin(admin.ModelAdmin): search_fields = ('title',) date_hierarchy = 'date' form = RefundForm - readonly_fields = ('amount',) + readonly_fields = ('eur_value',) @admin.register(Payment) diff --git a/refunding/models.py b/refunding/models.py index 717815a..f6f4bd8 100644 --- a/refunding/models.py +++ b/refunding/models.py @@ -15,11 +15,11 @@ class Refund(models.Model): on_delete=models.PROTECT ) - def amount(self) -> float: + def eur_value(self) -> float: return self.payment_set.all().aggregate(Sum('value')).get('value__sum') / 100 def __str__(self) -> str: - return "{0} on {1} for {2}".format(self.title, self.date, self.amount()) + return "{0} on {1} for {2}".format(self.title, self.date, self.eur_value()) class Payment(models.Model): diff --git a/refunding/templates/refunding/refunds_list.html b/refunding/templates/refunding/refunds_list.html new file mode 100644 index 0000000..af0ecdd --- /dev/null +++ b/refunding/templates/refunding/refunds_list.html @@ -0,0 +1,19 @@ +{% extends 'base.html' %} +{% load l10n %} + +{% block content %} +

{% block title %}Latest refunds{% endblock %}

+ {% if refunds %} +
+ {% for refund in refunds %} +
+ {{ refund.title }} le {{ refund.date|date:"SHORT_DATE_FORMAT" }} : {{ payment.amount }}€ +
+ {% endfor %} +
+ {% else %} +

+ {% firstof default_nothing "Nothing here..." %} +

+ {% endif %} +{% endblock %} \ No newline at end of file diff --git a/refunding/urls.py b/refunding/urls.py index 4bf37d9..d70d6d5 100644 --- a/refunding/urls.py +++ b/refunding/urls.py @@ -1,6 +1,7 @@ from django.conf.urls import url -from refunding.views import not_refunded_payments +from refunding.views import not_refunded_payments, latest_refunds urlpatterns = [ url(r'^payments/$', not_refunded_payments, name='not_refunded_payments'), + url(r'^refunds/$', latest_refunds, name='latest_refunds'), ] diff --git a/refunding/views.py b/refunding/views.py index c5e7b63..0bbf631 100644 --- a/refunding/views.py +++ b/refunding/views.py @@ -1,7 +1,7 @@ from django.contrib.auth.decorators import login_required from django.db.models import Sum from django.shortcuts import render -from refunding.models import Payment +from refunding.models import Payment, Refund @login_required @@ -14,3 +14,13 @@ def not_refunded_payments(request): 'default_nothing': 'No payment to be refunded.' } return render(request, "refunding/payments_list.html", context) + + +@login_required +def latest_refunds(request): + refunds = Refund.objects.all().order_by('date')[:20] + context = { + 'refunds': refunds, + 'default_nothing': 'No refund to show.' + } + return render(request, "refunding/refunds_list.html", context)