108 lines
4 KiB
Python
108 lines
4 KiB
Python
from django.utils.translation import ugettext_lazy as _
|
|
from django.contrib.auth.models import Group
|
|
from django.db.models import Q
|
|
from django.http.request import QueryDict
|
|
|
|
import django_filters
|
|
from django_filters.widgets import LinkWidget
|
|
from django_tables2.utils import A
|
|
import django_tables2 as tables
|
|
|
|
from .models import Equipment, EquipmentCategory
|
|
|
|
|
|
class EquipmentFilter(django_filters.FilterSet):
|
|
owner = django_filters.ModelChoiceFilter(
|
|
field_name='owner',
|
|
queryset=Group.objects.all(),
|
|
widget=LinkWidget(),
|
|
)
|
|
category = django_filters.ModelChoiceFilter(
|
|
field_name='category',
|
|
queryset=EquipmentCategory.objects.all(),
|
|
widget=LinkWidget(),
|
|
method='filter_category',
|
|
)
|
|
|
|
def filter_category(self, queryset, name, value):
|
|
return queryset.in_category(value)
|
|
|
|
def get_categories(self, qs):
|
|
"""
|
|
rend les catégories qui servent à filtrer les Equipments de qs
|
|
ie les catégories des equipments et tous leurs parents
|
|
"""
|
|
filtre = Q(id__lt=0)
|
|
for eq in qs:
|
|
current = eq.category
|
|
for k in range(100):
|
|
if current is None:
|
|
break
|
|
filtre |= Q(id=current.id)
|
|
current = current.parent
|
|
return EquipmentCategory.objects.filter(filtre)
|
|
|
|
def __init__(self, data=None, queryset=None, *, request=None, prefix=None):
|
|
# On que les requêtes vides rendent quelque chose
|
|
if data is None:
|
|
data = QueryDict('category&owner')
|
|
super().__init__(data=data, queryset=queryset,
|
|
request=request, prefix=prefix)
|
|
if self.queryset is not None:
|
|
for filter_ in self.filters.values():
|
|
if filter_.queryset.model == Group:
|
|
own_ids = [eq.owner.id for eq in self.queryset]
|
|
filtre = Q(id__lt=0)
|
|
for own_id in own_ids:
|
|
filtre |= Q(id=own_id)
|
|
filter_.queryset = Group.objects.filter(filtre)
|
|
if filter_.queryset.model == EquipmentCategory:
|
|
filter_.queryset = self.get_categories(self.queryset)
|
|
|
|
class Meta:
|
|
model = Equipment
|
|
fields = ['category', 'owner']
|
|
|
|
|
|
class AbstractEquipmentTable(tables.Table):
|
|
stock_aviable_p = tables.Column(
|
|
accessor=A('stock_aviable_p'),
|
|
orderable=False, # TODO le rendre ordorable
|
|
verbose_name=_("Quantité disponible"),
|
|
)
|
|
full_category_p = tables.Column(
|
|
accessor=A('full_category_p'),
|
|
order_by=('category'),
|
|
verbose_name=_("Catégorie"),
|
|
)
|
|
name = tables.LinkColumn(
|
|
'equipment:detail',
|
|
args=[A('pk')],
|
|
verbose_name=_("Matériel"),
|
|
)
|
|
admin = tables.LinkColumn(
|
|
'admin:equipment_equipment_change',
|
|
attrs={
|
|
'a': {'class': 'glyphicon glyphicon-cog'}
|
|
},
|
|
text="",
|
|
orderable=False,
|
|
args=[A('pk')],
|
|
verbose_name=_(""),
|
|
)
|
|
|
|
def before_render(self, request):
|
|
if (request.user.is_staff and
|
|
request.user.has_perm('equipment_change_equipment')):
|
|
self.columns.show('admin')
|
|
else:
|
|
self.columns.hide('admin')
|
|
|
|
|
|
class EquipmentTable(AbstractEquipmentTable):
|
|
class Meta:
|
|
model = Equipment
|
|
template_name = 'equipment/tables/bootstrap-responsive.html'
|
|
fields = ['name', 'stock', 'owner', ]
|
|
sequence = ['admin', 'name', 'stock', 'stock_aviable_p',
|
|
'full_category_p', 'owner', ]
|