127 lines
3.7 KiB
Python
127 lines
3.7 KiB
Python
from django.contrib import admin
|
|
from django import forms
|
|
|
|
from .models import Equipment, EquipmentDefault, EquipmentRevision, EquipmentCategory, EquipmentLost, EquipmentAttributeValue, EquipmentAttribute
|
|
from .fields import IdField, IdWidget
|
|
|
|
|
|
class IdForm(forms.ModelForm):
|
|
def __init__(self, *args, **kwargs):
|
|
if 'min_value' in kwargs:
|
|
kwargs.pop('min_value')
|
|
if 'num_choices' in kwargs:
|
|
num_choices = kwargs.pop('num_choices')
|
|
else:
|
|
num_choices = None
|
|
super(IdForm, self).__init__(*args, **kwargs)
|
|
|
|
for field in self.instance._meta.fields:
|
|
if isinstance(field, IdField):
|
|
if num_choices is None:
|
|
choices = []
|
|
else:
|
|
choices = [(k, str(k)) for k in range(1, num_choices+1)]
|
|
self.fields[field.name].choices = choices
|
|
self.fields[field.name].widget = IdWidget(choices=self.fields[field.name].choices)
|
|
|
|
|
|
class IdFormset(forms.models.BaseInlineFormSet):
|
|
def get_form_kwargs(self, index):
|
|
kwargs = super().get_form_kwargs(index)
|
|
if self.instance:
|
|
kwargs["num_choices"] = self.instance.stock
|
|
return kwargs
|
|
|
|
|
|
class EquipmentRevisionExtraInline(admin.TabularInline):
|
|
model = EquipmentRevision
|
|
extra = 0
|
|
form = IdForm
|
|
formset = IdFormset
|
|
classes = ['collapse']
|
|
|
|
|
|
class EquipmentDefaultExtraInline(admin.TabularInline):
|
|
model = EquipmentDefault
|
|
extra = 0
|
|
form = IdForm
|
|
formset = IdFormset
|
|
classes = ['collapse']
|
|
|
|
|
|
class EquipmentLostExtraInline(admin.TabularInline):
|
|
model = EquipmentLost
|
|
extra = 0
|
|
form = IdForm
|
|
formset = IdFormset
|
|
classes = ['collapse']
|
|
|
|
|
|
class EquipmentAttributeValueInline(admin.TabularInline):
|
|
model = EquipmentAttributeValue
|
|
extra = 0
|
|
|
|
|
|
class CategoryAdmin(admin.ModelAdmin):
|
|
readonly_fields = ['full_name_p']
|
|
list_display = ['name', 'parent', "full_name_p"]
|
|
ordering = ['name', 'parent']
|
|
search_fields = ['name',]
|
|
autocomplete_fields = ['parent', ]
|
|
|
|
|
|
class EquipmentAttributeAdmin(admin.ModelAdmin):
|
|
list_display = ['name']
|
|
ordering = ['name']
|
|
search_fields = ['name',]
|
|
|
|
|
|
class EquipmentAdmin(admin.ModelAdmin):
|
|
save_as_continue = True
|
|
save_on_top = True
|
|
autocomplete_fields = ['category', ]
|
|
readonly_fields = ['full_category_p',
|
|
'added_at',
|
|
'modified_at',
|
|
'stock_aviable_p',
|
|
'ids_aviable_p',
|
|
'stock_lost_p',
|
|
'ids_lost_p',
|
|
]
|
|
list_display = ['name', 'stock', 'owner', 'category', 'modified_at']
|
|
fieldsets = (
|
|
('Général', {
|
|
'fields': ('name', 'owner', 'stock', )
|
|
}),
|
|
('Info stock',
|
|
{
|
|
'fields': (
|
|
'stock_aviable_p',
|
|
'ids_aviable_p',
|
|
'stock_lost_p',
|
|
'ids_lost_p',
|
|
),
|
|
}),
|
|
('Catégorie', {
|
|
'fields': ('category', 'full_category_p'),
|
|
}),
|
|
('Description', {
|
|
'fields': ('description', 'added_at', 'modified_at',),
|
|
}),
|
|
)
|
|
|
|
|
|
|
|
|
|
ordering = ['name', 'owner', 'category']
|
|
inlines = [EquipmentAttributeValueInline,
|
|
EquipmentDefaultExtraInline,
|
|
EquipmentLostExtraInline,
|
|
EquipmentRevisionExtraInline]
|
|
search_fields = ['name', 'description',]
|
|
list_filter = ['owner', 'category' ]
|
|
|
|
|
|
admin.site.register(Equipment, EquipmentAdmin)
|
|
admin.site.register(EquipmentCategory, CategoryAdmin)
|
|
admin.site.register(EquipmentAttribute, EquipmentAttributeAdmin)
|