Improve purchase view
Django CI/CD / test (push) Successful in 58s Details
Django CI/CD / build-and-push (push) Successful in 2m16s Details

This commit is contained in:
Lukáš Kucharczyk 2025-01-30 17:54:42 +01:00
parent 2f4e16dd54
commit d213a3d35d
Signed by: lukas
SSH Key Fingerprint: SHA256:vMuSwvwAvcT6htVAioMP7rzzwMQNi3roESyhv+nAxeg
3 changed files with 31 additions and 5 deletions

View File

@ -3,7 +3,7 @@ from datetime import timedelta
from django.core.exceptions import ValidationError
from django.db import models
from django.db.models import F, Sum
from django.template.defaultfilters import floatformat, slugify
from django.template.defaultfilters import floatformat, pluralize, slugify
from django.utils import timezone
from common.time import format_duration
@ -148,14 +148,27 @@ class Purchase(models.Model):
@property
def standardized_name(self):
return self.name if self.name else self.first_game.name
return self.name or self.first_game.name
@property
def first_game(self):
return self.games.first()
def __str__(self):
return f"{self.standardized_name} ({self.num_purchases}, {self.date_purchased}, {self.standardized_price})"
return self.standardized_name
@property
def full_name(self):
additional_info = [
str(item)
for item in [
f"{self.num_purchases} game{pluralize(self.num_purchases)}",
self.date_purchased,
self.standardized_price,
]
if item
]
return f"{self.standardized_name} ({', '.join(additional_info)})"
def is_game(self):
return self.type == self.GAME

View File

@ -2,8 +2,17 @@
<div class="dark:text-white max-w-sm sm:max-w-xl lg:max-w-3xl mx-auto">
<div class="flex flex-col gap-5 mb-3">
<div class="font-bold font-serif text-slate-500 text-2xl">
{% if not purchase.name %}
Unnamed purchase
{% else %}
{{ purchase.name }}
{% endif %}
</div>
<span class="text-balance max-w-[30rem] text-4xl">
<span class="font-bold font-serif">{% if purchase.name %}{{ purchase.name }}{% else %}Unnamed purchase{% endif %}</span> <span class="text-slate-500 text-2xl">({{ purchase.games.count }} games)</span>
<span class="font-bold font-serif">
{{ purchase.date_purchased }} ({{ purchase.num_purchases }} game{{ purchase.num_purchases|pluralize}})
</span>
</span>
<div class="inline-flex rounded-md shadow-sm mb-3" role="group">
<a href="{% url 'edit_purchase' purchase.id %}">

View File

@ -200,7 +200,11 @@ def delete_purchase(request: HttpRequest, purchase_id: int) -> HttpResponse:
@login_required
def view_purchase(request: HttpRequest, purchase_id: int) -> HttpResponse:
purchase = get_object_or_404(Purchase, id=purchase_id)
return render(request, "view_purchase.html", {"purchase": purchase})
return render(
request,
"view_purchase.html",
{"purchase": purchase, "title": f"Purchase: {purchase.full_name}"},
)
@login_required