im/inventory/views.py

57 lines
1.6 KiB
Python

from django.shortcuts import render
from django.views import generic
from django.db.models import F, Sum, Q
from .models import PantryItem, PantryItemLine
# Create your views here.
def index(request):
return render(request, "inventory/index.html")
class Shoppinglist(generic.ListView):
template_name = "inventory/shoppinglist.html"
context_object_name = 'pis'
def get_queryset(self):
"""
Return pantryitems for which we have None itemlines or Itemlines whith a total quantitly less than required
"""
return PantryItem.objects.annotate(
total_quantity=Sum(F('pantryitemline__quantity') *
F('pantryitemline__size'))
).filter(Q(min_quantity__gt=F('total_quantity')) | Q(pantryitemline=None, min_quantity__gt=0))
class Expirations(generic.ListView):
template_name = "inventory/expirations.html"
context_object_name = 'pis'
def get_queryset(self):
return PantryItemLine.objects.exclude(expiry_date__isnull=True).exclude(quantity=0).order_by('expiry_date')
class ConsumeList(generic.ListView):
template_name = "inventory/consumelist.html"
context_object_name = 'pis'
def get_queryset(self):
return PantryItemLine.objects.exclude(quantity=0).order_by('expiry_date')
class Consume(generic.UpdateView):
template_name = "inventory/consume.html"
fields = ['quantity']
context_object_name = 'pis'
def get_queryset(self):
return PantryItemLine.objects.exclude(quantity=0).order_by('expiry_date')
class PantryItemLineView(generic.DetailView):
model = PantryItemLine