182 lines
5.6 KiB
Python
182 lines
5.6 KiB
Python
import json
|
|
from collections import defaultdict
|
|
from django.shortcuts import get_object_or_404, render
|
|
from django.http import HttpResponseRedirect, HttpResponse
|
|
from django.urls import reverse
|
|
from django.db import transaction
|
|
from django.db.models import Sum
|
|
|
|
from .models import *
|
|
|
|
ADD_SKETCH_REQUIRES = ['sketch_name', 'sketch_number', '']
|
|
|
|
|
|
def index(request):
|
|
product_list = Product.objects.all()
|
|
context = {'product_list': product_list}
|
|
return render(request, 'parts/products.html', context)
|
|
|
|
|
|
def product(request, product_id):
|
|
product = Product.objects.get(id=product_id)
|
|
sketches = Sketch.objects.filter(usage__productusage__product=product)
|
|
context = {'product': product, 'sketches': sketches}
|
|
return render(request, 'parts/product.html', context)
|
|
|
|
|
|
def sketch(request, sketch_id):
|
|
sketch = Sketch.objects.get(id=sketch_id)
|
|
context = {'sketch': sketch}
|
|
return render(request, 'parts/sketch.html', context)
|
|
|
|
def __part_view(request, part):
|
|
sketches = part.usage_set.values('sketch', 'sketch_number', 'sketch__name', 'sketch__product__name', 'sketch__product__id').annotate(qty=Sum('productusage__quantity'))
|
|
context = {'part': part, 'sketches': sketches}
|
|
return render(request, 'parts/part.html', context)
|
|
|
|
def part(request, part_id):
|
|
part = BrandedPart.objects.get(id=part_id)
|
|
return __part_view(request, part)
|
|
|
|
def part_by_number(request, part_number):
|
|
part = BrandedPart.objects.get(number=part_number)
|
|
return __part_view(request, part)
|
|
|
|
def parts(request):
|
|
products = Product.objects.order_by('name')
|
|
parts = BrandedPart.objects.order_by('number')
|
|
quantities = []
|
|
product_sums = defaultdict(lambda: 0)
|
|
for part in parts: # TODO: replace by sophisticated query
|
|
qtys = defaultdict(lambda: 0)
|
|
for result in part.usage_set.values('productusage__product__name').annotate(Sum('productusage__quantity')).order_by('productusage__product__name'):
|
|
prod_name = result['productusage__product__name']
|
|
prod_sum = result['productusage__quantity__sum']
|
|
if prod_sum:
|
|
qtys[prod_name] = prod_sum
|
|
product_sums[prod_name] += prod_sum
|
|
prod_qtys = [qtys[prod.name] for prod in products]
|
|
quantities.append({
|
|
"name": part.get_name(),
|
|
"number": part.number,
|
|
"quantities": prod_qtys,
|
|
"sum": sum(prod_qtys),
|
|
"id": part.id,
|
|
})
|
|
|
|
product_sum = [product_sums[prod.name] for prod in products]
|
|
context = {
|
|
'products': products,
|
|
'parts': parts,
|
|
'quantities': quantities,
|
|
'len': len(quantities),
|
|
'product_sums': product_sum,
|
|
'total_sum': sum(product_sum),
|
|
}
|
|
return render(request, 'parts/parts.html', context)
|
|
|
|
|
|
def part_search(request):
|
|
if "term" in request.GET:
|
|
term = request.GET.get("term", '')
|
|
parts = BrandedPart.objects.filter(number__startswith=term)
|
|
return HttpResponse(
|
|
json.dumps(
|
|
[
|
|
{
|
|
'id': part.id,
|
|
'label': part.number + " - " + part.get_name(full=True),
|
|
'value': part.number,
|
|
'name': part.get_name(first=True)
|
|
}
|
|
for part in parts
|
|
],
|
|
indent=1)
|
|
)
|
|
return HttpResponse("404")
|
|
|
|
|
|
@transaction.atomic
|
|
def sketch_add(request, product_id, sketch_id=None):
|
|
product = get_object_or_404(Product, pk=product_id)
|
|
print(sketch_id)
|
|
if sketch_id:
|
|
sketch = Sketch.objects.get(id=sketch_id)
|
|
else:
|
|
sketch = Sketch()
|
|
if not "sketch_name" in request.POST:
|
|
return render(request, 'parts/add.html', {'product': product, "sketch": sketch})
|
|
sketch.name = request.POST["sketch_name"]
|
|
sketch.product = product
|
|
sketch.brand = product.brand
|
|
sketch.save()
|
|
for i in range(len(request.POST.getlist('name'))):
|
|
print(i)
|
|
usage = Usage()
|
|
usage.sketch = sketch
|
|
usage.sketch_number = request.POST.getlist('fignumber')[i]
|
|
usage.exact_sketch = is_exact_sketchnumber(usage.sketch_number)
|
|
|
|
part_number = request.POST.getlist('partnumber')[i]
|
|
if len(part_number) < 1:
|
|
continue
|
|
branded_part, created = BrandedPart.objects.get_or_create(brand=product.brand, number=part_number)
|
|
_part_name = request.POST.getlist('name')[i].strip()
|
|
if created:
|
|
try:
|
|
part, _ = Part.objects.get_or_create(name=_part_name)
|
|
except Part.MultipleObjectsReturned:
|
|
part = Part.objects.filter(name=_part_name).first()
|
|
branded_part.parts.add(part)
|
|
branded_part.brand = product.brand
|
|
branded_part.save()
|
|
else:
|
|
if len(_part_name):
|
|
part = Part.objects.filter(name=_part_name)
|
|
if not (part and part.first() in branded_part.parts.all()):
|
|
if part:
|
|
branded_part.parts.add(part.first())
|
|
else:
|
|
part, _ = Part.objects.get_or_create(name=_part_name)
|
|
branded_part.parts.add(part)
|
|
usage.part = branded_part
|
|
usage.save()
|
|
|
|
prod = ProductUsage()
|
|
prod.usage = usage
|
|
prod.product = product
|
|
prod.quantity = get_quantity(request.POST.getlist('quantity')[i].strip())
|
|
last_version = request.POST.getlist('last_version')[i]
|
|
if len(last_version):
|
|
version, _ = Version.objects.get_or_create(product=product, name=last_version)
|
|
prod.used_until = version
|
|
first_version = request.POST.getlist('first_version')[i]
|
|
if len(first_version):
|
|
version, _ = Version.objects.get_or_create(product=product, name=first_version)
|
|
prod.used_since = version
|
|
replaced = request.POST.getlist('replaced_by')[i]
|
|
note = request.POST.getlist('notes')[i]
|
|
if len(note):
|
|
prod.note = note
|
|
if len(replaced):
|
|
prod.replaced_by = replaced
|
|
prod.note += "{REP:" + str(replaced) + "}" # TODO
|
|
internal = request.POST.getlist('internal')[i]
|
|
if len(internal):
|
|
prod.internal = internal
|
|
prod.save()
|
|
|
|
return HttpResponseRedirect(reverse('parts:sketch', args=(sketch.id,)))
|
|
|
|
|
|
def get_quantity(qnty_str):
|
|
qnty = 0
|
|
if len(qnty_str):
|
|
qnty = int(qnty_str)
|
|
return qnty
|
|
|
|
|
|
def is_exact_sketchnumber(sketch_number):
|
|
approximate = ("ähn", "vgl", "~", "zu")
|
|
return not any([i in sketch_number for i in approximate])
|