mirror of
https://github.com/daniviga/django-ram.git
synced 2026-02-04 18:10:38 +01:00
Further optimizations, improve counting to rely on backend DB
This commit is contained in:
@@ -142,7 +142,7 @@
|
||||
</tr>
|
||||
<tr>
|
||||
<th scope="row">Composition</th>
|
||||
<td>{% for t in consist.get_type_count %}{{ t.count }}x {{ t.type }} {{t.category }}{% if not forloop.last %} » {% endif %}{% endfor %}{% if loads %} | <i class="bi bi-download"></i> {{ loads|length }}x Load{{ loads|pluralize }}{% endif %}</td>
|
||||
<td>{% for t in consist.get_type_count %}{{ t.count }}x {{ t.type }} {{t.category }}{% if not forloop.last %} » {% endif %}{% endfor %}{% if loads %} | <i class="bi bi-download"></i> {{ loads_count }}x Load{{ loads|pluralize }}{% endif %}</td>
|
||||
</tr>
|
||||
</tbody>
|
||||
</table>
|
||||
|
||||
@@ -579,6 +579,12 @@ class GetConsist(View):
|
||||
except ObjectDoesNotExist:
|
||||
raise Http404
|
||||
|
||||
# Get all published rolling stock IDs for efficient filtering
|
||||
published_ids = set(
|
||||
RollingStock.objects.get_published(request.user)
|
||||
.values_list('uuid', flat=True)
|
||||
)
|
||||
|
||||
# Fetch consist items with related rolling stock in one query
|
||||
consist_items = consist.consist_item.select_related(
|
||||
'rolling_stock',
|
||||
@@ -589,21 +595,17 @@ class GetConsist(View):
|
||||
'rolling_stock__scale',
|
||||
).prefetch_related('rolling_stock__image')
|
||||
|
||||
# Filter items and loads
|
||||
data = list(
|
||||
# Filter items and loads efficiently
|
||||
data = [
|
||||
item.rolling_stock
|
||||
for item in consist_items.filter(load=False)
|
||||
if RollingStock.objects.get_published(request.user)
|
||||
.filter(uuid=item.rolling_stock_id)
|
||||
.exists()
|
||||
)
|
||||
loads = list(
|
||||
if item.rolling_stock.uuid in published_ids
|
||||
]
|
||||
loads = [
|
||||
item.rolling_stock
|
||||
for item in consist_items.filter(load=True)
|
||||
if RollingStock.objects.get_published(request.user)
|
||||
.filter(uuid=item.rolling_stock_id)
|
||||
.exists()
|
||||
)
|
||||
if item.rolling_stock.uuid in published_ids
|
||||
]
|
||||
|
||||
paginator = Paginator(data, get_items_per_page())
|
||||
data = paginator.get_page(page)
|
||||
@@ -619,6 +621,7 @@ class GetConsist(View):
|
||||
"consist": consist,
|
||||
"data": data,
|
||||
"loads": loads,
|
||||
"loads_count": len(loads),
|
||||
"page_range": page_range,
|
||||
},
|
||||
)
|
||||
|
||||
Reference in New Issue
Block a user