From 2e06e94fdeef396af75820577f29341ef6ee6a1b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Daniele=20Vigan=C3=B2?= Date: Wed, 30 Apr 2025 22:50:43 +0200 Subject: [PATCH] Add counters to cards --- ram/portal/templates/cards/company.html | 4 +- ram/portal/templates/cards/manufacturer.html | 4 +- .../templates/cards/rolling_stock_type.html | 6 +- ram/portal/templates/cards/scale.html | 4 +- ram/portal/templates/consist.html | 6 +- ram/portal/views.py | 59 ++++++++++++++++--- ram/ram/__init__.py | 2 +- 7 files changed, 71 insertions(+), 14 deletions(-) diff --git a/ram/portal/templates/cards/company.html b/ram/portal/templates/cards/company.html index 9467c79..fc0b5af 100644 --- a/ram/portal/templates/cards/company.html +++ b/ram/portal/templates/cards/company.html @@ -39,8 +39,10 @@
- Show all rolling stock + {% with items=d.item.num_items %} + Show {{ items }} item{{ items | pluralize}} {% if request.user.is_staff %}Edit{% endif %} + {% endwith %}
diff --git a/ram/portal/templates/cards/manufacturer.html b/ram/portal/templates/cards/manufacturer.html index e809b57..c7e41e0 100644 --- a/ram/portal/templates/cards/manufacturer.html +++ b/ram/portal/templates/cards/manufacturer.html @@ -30,8 +30,10 @@
- Show all rolling stock + {% with items=d.item.num_items %} + Show {{ items }} item{{ items | pluralize}} {% if request.user.is_staff %}Edit{% endif %} + {% endwith %}
diff --git a/ram/portal/templates/cards/rolling_stock_type.html b/ram/portal/templates/cards/rolling_stock_type.html index 52a45f8..30665c9 100644 --- a/ram/portal/templates/cards/rolling_stock_type.html +++ b/ram/portal/templates/cards/rolling_stock_type.html @@ -20,8 +20,10 @@
- Show all rolling stock - {% if request.user.is_staff %}Edit{% endif %} + {% with items=d.item.num_items %} + Show {{ items }} item{{ items | pluralize}} + {% if request.user.is_staff %}Edit{% endif %} + {% endwith %}
diff --git a/ram/portal/templates/cards/scale.html b/ram/portal/templates/cards/scale.html index b80f1f2..4c1118a 100644 --- a/ram/portal/templates/cards/scale.html +++ b/ram/portal/templates/cards/scale.html @@ -28,8 +28,10 @@
- Show all rolling stock + {% with items=d.item.num_items %} + Show {{ items }} item{{ items | pluralize}} {% if request.user.is_staff %}Edit{% endif %} + {% endwith %}
diff --git a/ram/portal/templates/consist.html b/ram/portal/templates/consist.html index 3c50c15..25d1e3b 100644 --- a/ram/portal/templates/consist.html +++ b/ram/portal/templates/consist.html @@ -109,7 +109,11 @@ {% endif %} Length - {{ consist.length }} | {% for t in consist.get_type_count %}{{ t.count }}x {{ t.type }} {{t.category }}{% if not forloop.last %} + {% endif %}{% endfor %} + {{ consist.length }} + + + Composition + {% for t in consist.get_type_count %}{{ t.count }}x {{ t.type }} {{t.category }}{% if not forloop.last %} » {% endif %}{% endfor %} diff --git a/ram/portal/views.py b/ram/portal/views.py index 221de45..22d4776 100644 --- a/ram/portal/views.py +++ b/ram/portal/views.py @@ -7,7 +7,7 @@ from urllib.parse import unquote from django.views import View from django.http import Http404, HttpResponseBadRequest from django.db.utils import OperationalError, ProgrammingError -from django.db.models import Q, Count +from django.db.models import F, Q, Count from django.shortcuts import render, get_object_or_404, get_list_or_404 from django.core.exceptions import ObjectDoesNotExist from django.core.paginator import Paginator @@ -490,7 +490,11 @@ class Manufacturers(GetData): item_type = "manufacturer" def get_data(self, request): - return Manufacturer.objects.filter(self.filter) + return Manufacturer.objects.filter( + self.filter + ).annotate( + num_items=Count("rollingstock") + Count("rollingclass"), + ).order_by("name") # overload get method to filter by category def get(self, request, category, page=1): @@ -506,18 +510,50 @@ class Companies(GetData): item_type = "company" def get_data(self, request): - return Company.objects.all() + return Company.objects.annotate( + num_rollingstock=( + Count( + "rollingclass__rolling_class", + filter=Q( + rollingclass__rolling_class__in=( + RollingStock.objects.get_published(request.user) + ) + ), + distinct=True + ) + ) + ).annotate( + num_consists=( + Count( + "consist", + filter=Q( + consist__in=( + Consist.objects.get_published(request.user) + ), + ), + distinct=True + ) + ) + ).annotate( + num_items=F("num_rollingstock") + F("num_consists") + ).order_by("name") class Scales(GetData): title = "Scales" item_type = "scale" - queryset = Scale.objects.all() def get_data(self, request): return Scale.objects.annotate( - num_items=Count("rollingstock") - ) # .filter(num_items__gt=0) to filter data with no items + num_items=Count( + "rollingstock", + filter=Q( + rollingstock__in=RollingStock.objects.get_published( + request.user + ) + ), + ), + ).order_by("-ratio_int", "-tracks", "scale") class Types(GetData): @@ -525,7 +561,16 @@ class Types(GetData): item_type = "rolling_stock_type" def get_data(self, request): - return RollingStockType.objects.all() + return RollingStockType.objects.annotate( + num_items=Count( + "rollingclass__rolling_class", + filter=Q( + rollingclass__rolling_class__in=( + RollingStock.objects.get_published(request.user) + ) + ), + ) + ).order_by("order") class Books(GetData): diff --git a/ram/ram/__init__.py b/ram/ram/__init__.py index da1f3ee..bbf4658 100644 --- a/ram/ram/__init__.py +++ b/ram/ram/__init__.py @@ -1,4 +1,4 @@ from ram.utils import git_suffix -__version__ = "0.17.5" +__version__ = "0.17.6" __version__ += git_suffix(__file__)