from django.shortcuts import render, redirect from django.contrib.auth import get_user_model from app.models import Artikal, Klient, Lagerlist, Stavka # Create your views here. def home_v(request): return render(request, 'home.html') def fakturi_v(request): fakturi = Stavka.objects.using('db').all() return render(request, 'fakturi.html', {'fakturi':fakturi}) def fakturi_edit_v(request, id): if (request.method != "POST"): try: stavka = Stavka.objects.using('db').get(stavkaid=id) except Stavka.DoesNotExist: stavka = None artikli = Lagerlist.objects.using('db').filter(stavkaid=id) items = Artikal.objects.using('db').all() klienti = Klient.objects.using('db').all() return render(request, 'fakturi_edit_form.html', {'artikli':artikli, 'stavka':stavka, 'items':items, 'klienti':klienti}) else: lagerlist_dict = [] print(request.POST) for i, element in enumerate(request.POST.getlist("id")): lagerlist_dict.append([request.POST.getlist("lagerlist_id")[i],request.POST.getlist("id")[i],request.POST.getlist("kol")[i]]) stavka_post = request.POST['stavka'] stavka_edit = Stavka.objects.using('db').get(stavkaid=stavka_post) stavka_edit.broj = request.POST['broj'] stavka_edit.klientid = Klient(request.POST['klient']) stavka_edit.datum = request.POST['datum'] stavka_edit.save() data_in_lager = Lagerlist.objects.using('db').filter(stavkaid=id) for item in lagerlist_dict: print(item) try: lagerlist_edit = Lagerlist.objects.using('db').get(lagerlist_id=item[0]) lagerlist_edit.artikal = Artikal(item[1]) lagerlist_edit.kolicina = item[2] lagerlist_edit.save() except ValueError: avg_cena = Artikal.objects.using('db').get(artikal_id=item[1]).cena lagerlist_edit = Lagerlist(stavkaid=Stavka(id), artikal=Artikal(item[1]), kolicina=item[2], avg_cena=avg_cena) lagerlist_edit.save(using="db") return redirect('fakturi') def fakturi_new_v(request): items = Artikal.objects.using('db').all() klienti = Klient.objects.using('db').all() return render(request, 'fakturi_new_form.html', {'klienti':klienti, 'items':items})