partlist: fix product alignment, add product sum
parent
8304f30cb6
commit
1d91c5da38
|
|
@ -24,5 +24,8 @@
|
||||||
<tr>
|
<tr>
|
||||||
<td>Summe:</td>
|
<td>Summe:</td>
|
||||||
<td>{{len}}</td>
|
<td>{{len}}</td>
|
||||||
|
{% for product in product_sums %}
|
||||||
|
<td>{{product}}</td>
|
||||||
|
{% endfor %}
|
||||||
<td>Teile</td>
|
<td>Teile</td>
|
||||||
</table>
|
</table>
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,5 @@
|
||||||
import json
|
import json
|
||||||
|
from collections import defaultdict
|
||||||
from django.shortcuts import get_object_or_404, render
|
from django.shortcuts import get_object_or_404, render
|
||||||
from django.http import HttpResponseRedirect, HttpResponse
|
from django.http import HttpResponseRedirect, HttpResponse
|
||||||
from django.urls import reverse
|
from django.urls import reverse
|
||||||
|
|
@ -45,17 +46,16 @@ def parts(request):
|
||||||
products = Product.objects.order_by('name')
|
products = Product.objects.order_by('name')
|
||||||
parts = BrandedPart.objects.order_by('number')
|
parts = BrandedPart.objects.order_by('number')
|
||||||
quantities = []
|
quantities = []
|
||||||
for part in parts: # FIXME!
|
product_sums = defaultdict(lambda: 0)
|
||||||
qtys = part.usage_set.values('productusage__product__name').annotate(Sum('productusage__quantity')).order_by('productusage__product__name')
|
for part in parts: # TODO: replace by sophisticated query
|
||||||
#prod_qtys = {}
|
qtys = defaultdict(lambda: 0)
|
||||||
prod_qtys = []
|
for result in part.usage_set.values('productusage__product__name').annotate(Sum('productusage__quantity')).order_by('productusage__product__name'):
|
||||||
for obj in qtys:
|
prod_name = result['productusage__product__name']
|
||||||
# FIXME: make sure there are n(==products) entries
|
prod_sum = result['productusage__quantity__sum']
|
||||||
#prod_qtys['productusage__product__name'] = obj['productusage__quantity__sum']
|
if prod_sum:
|
||||||
qty = obj['productusage__quantity__sum']
|
qtys[prod_name] = prod_sum
|
||||||
if not qty:
|
product_sums[prod_name] += prod_sum
|
||||||
qty = 0
|
prod_qtys = [qtys[prod.name] for prod in products]
|
||||||
prod_qtys.append(qty)
|
|
||||||
quantities.append({
|
quantities.append({
|
||||||
"name": part.get_name(),
|
"name": part.get_name(),
|
||||||
"number": part.number,
|
"number": part.number,
|
||||||
|
|
@ -64,7 +64,13 @@ def parts(request):
|
||||||
"id": part.id,
|
"id": part.id,
|
||||||
})
|
})
|
||||||
|
|
||||||
context = {'products': products, 'parts': parts, 'quantities': quantities, 'len': len(quantities)}
|
context = {
|
||||||
|
'products': products,
|
||||||
|
'parts': parts,
|
||||||
|
'quantities': quantities,
|
||||||
|
'len': len(quantities),
|
||||||
|
'product_sums': [product_sums[prod.name] for prod in products]
|
||||||
|
}
|
||||||
return render(request, 'parts/parts.html', context)
|
return render(request, 'parts/parts.html', context)
|
||||||
|
|
||||||
|
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue