Improve purchase view
This commit is contained in:
parent
2f4e16dd54
commit
d213a3d35d
|
@ -3,7 +3,7 @@ from datetime import timedelta
|
||||||
from django.core.exceptions import ValidationError
|
from django.core.exceptions import ValidationError
|
||||||
from django.db import models
|
from django.db import models
|
||||||
from django.db.models import F, Sum
|
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 django.utils import timezone
|
||||||
|
|
||||||
from common.time import format_duration
|
from common.time import format_duration
|
||||||
|
@ -148,14 +148,27 @@ class Purchase(models.Model):
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def standardized_name(self):
|
def standardized_name(self):
|
||||||
return self.name if self.name else self.first_game.name
|
return self.name or self.first_game.name
|
||||||
|
|
||||||
@property
|
@property
|
||||||
def first_game(self):
|
def first_game(self):
|
||||||
return self.games.first()
|
return self.games.first()
|
||||||
|
|
||||||
def __str__(self):
|
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):
|
def is_game(self):
|
||||||
return self.type == self.GAME
|
return self.type == self.GAME
|
||||||
|
|
|
@ -2,8 +2,17 @@
|
||||||
<div class="dark:text-white max-w-sm sm:max-w-xl lg:max-w-3xl mx-auto">
|
<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="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="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>
|
</span>
|
||||||
<div class="inline-flex rounded-md shadow-sm mb-3" role="group">
|
<div class="inline-flex rounded-md shadow-sm mb-3" role="group">
|
||||||
<a href="{% url 'edit_purchase' purchase.id %}">
|
<a href="{% url 'edit_purchase' purchase.id %}">
|
||||||
|
|
|
@ -200,7 +200,11 @@ def delete_purchase(request: HttpRequest, purchase_id: int) -> HttpResponse:
|
||||||
@login_required
|
@login_required
|
||||||
def view_purchase(request: HttpRequest, purchase_id: int) -> HttpResponse:
|
def view_purchase(request: HttpRequest, purchase_id: int) -> HttpResponse:
|
||||||
purchase = get_object_or_404(Purchase, id=purchase_id)
|
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
|
@login_required
|
||||||
|
|
Loading…
Reference in New Issue