97 lines
3.2 KiB
Python
97 lines
3.2 KiB
Python
from .models import Equipment, EquipmentCategory
|
|
from django.contrib.auth.models import Group
|
|
from django.db.models import Sum
|
|
from django.views.generic import DetailView, ListView
|
|
from django.contrib.auth.mixins import LoginRequiredMixin
|
|
|
|
from django_filters.views import FilterView
|
|
from django_tables2.views import SingleTableMixin
|
|
|
|
from .tables import EquipmentTable, EquipmentFilter
|
|
|
|
|
|
class EquipmentHome(LoginRequiredMixin, ListView):
|
|
template_name = 'equipment/home.html'
|
|
context_object_name = 'categories'
|
|
model = EquipmentCategory
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
# TODO remplacer par les vrais owners
|
|
context['owners'] = Group.objects.all()
|
|
categories = (EquipmentCategory.objects.order_by('name')
|
|
.prefetch_related('children'))
|
|
context['root_cat'] = categories.filter(parent=None)
|
|
queryset = Equipment.objects.all()
|
|
context['stock'] = queryset.aggregate(Sum('stock'))['stock__sum']
|
|
context['nb_type'] = queryset.count()
|
|
return context
|
|
|
|
|
|
class EquipmentListAbstract(LoginRequiredMixin, SingleTableMixin,FilterView):
|
|
table_class = EquipmentTable
|
|
filterset_class = EquipmentFilter
|
|
template_name = 'equipment/list.html'
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
context['stock'] = self.queryset.aggregate(Sum('stock'))['stock__sum']
|
|
context['nb_type'] = self.queryset.count()
|
|
return context
|
|
|
|
|
|
class EquipmentList(EquipmentListAbstract):
|
|
def get_queryset(self):
|
|
self.queryset = Equipment.objects.all()
|
|
return self.queryset
|
|
|
|
|
|
class EquipmentListByCategory(EquipmentListAbstract):
|
|
def get_category(self):
|
|
try:
|
|
pk = self.kwargs.get('pk')
|
|
except KeyError:
|
|
raise AttributeError(
|
|
"View %s must be called with an object "
|
|
"pk in the URLconf." % self.__class__.__name__
|
|
)
|
|
return EquipmentCategory.objects.get(id=pk)
|
|
|
|
def get_queryset(self):
|
|
cat = self.get_category()
|
|
self.queryset = Equipment.objects.all().in_category(cat)
|
|
return self.queryset
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
cat = self.get_category()
|
|
context['subtitle'] = "Dans {cat}".format(cat=cat.full_name())
|
|
return context
|
|
|
|
|
|
class EquipmentListByOwner(EquipmentListAbstract):
|
|
def get_owner(self):
|
|
try:
|
|
pk = self.kwargs.get('pk')
|
|
except KeyError:
|
|
raise AttributeError(
|
|
"View %s must be called with an object "
|
|
"pk in the URLconf." % self.__class__.__name__
|
|
)
|
|
return Group.objects.get(id=pk)
|
|
|
|
def get_queryset(self):
|
|
owner = self.get_owner()
|
|
self.queryset = Equipment.objects.filter(owner=owner)
|
|
return self.queryset
|
|
|
|
def get_context_data(self, **kwargs):
|
|
context = super().get_context_data(**kwargs)
|
|
owner = self.get_owner()
|
|
context['subtitle'] = "Matériel de {owner}".format(owner=owner)
|
|
return context
|
|
|
|
|
|
class EquipmentView(DetailView):
|
|
model = Equipment
|
|
template_name = 'equipment/detail.html'
|