2018-07-26 15:53:42 +02:00
|
|
|
from django.contrib import admin
|
2018-08-02 17:55:27 +02:00
|
|
|
from django import forms
|
|
|
|
|
2018-08-07 20:29:37 +02:00
|
|
|
from .models import Equipment, EquipmentDefault, EquipmentRevision, EquipmentPole, EquipmentCategory, EquipmentLost
|
2018-08-02 17:55:27 +02:00
|
|
|
from .fields import IdField, IdWidget
|
|
|
|
|
2018-08-04 05:38:03 +02:00
|
|
|
from shared.admin import admin_site
|
|
|
|
|
2018-08-02 17:55:27 +02:00
|
|
|
class IdForm(forms.ModelForm):
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
if 'min_value' in kwargs:
|
|
|
|
kwargs.pop('min_value')
|
2018-08-04 05:38:03 +02:00
|
|
|
if 'num_choices' in kwargs:
|
|
|
|
num_choices = kwargs.pop('num_choices')
|
2018-08-02 17:55:27 +02:00
|
|
|
super(IdForm, self).__init__(*args, **kwargs)
|
|
|
|
|
|
|
|
for field in self.instance._meta.fields:
|
|
|
|
if isinstance(field, IdField):
|
2018-08-04 05:38:03 +02:00
|
|
|
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
|
2018-08-06 17:06:38 +02:00
|
|
|
form = IdForm
|
|
|
|
formset = IdFormset
|
2018-08-07 15:58:38 +02:00
|
|
|
classes = ['collapse']
|
2018-07-26 15:53:42 +02:00
|
|
|
|
|
|
|
|
2018-08-07 19:08:20 +02:00
|
|
|
class EquipmentDefaultExtraInline(admin.TabularInline):
|
|
|
|
model = EquipmentDefault
|
2018-07-26 15:53:42 +02:00
|
|
|
extra = 0
|
2018-08-06 17:06:38 +02:00
|
|
|
form = IdForm
|
|
|
|
formset = IdFormset
|
2018-08-07 15:58:38 +02:00
|
|
|
classes = ['collapse']
|
2018-07-26 15:53:42 +02:00
|
|
|
|
2018-08-07 19:08:20 +02:00
|
|
|
|
|
|
|
class EquipmentLostExtraInline(admin.TabularInline):
|
|
|
|
model = EquipmentLost
|
|
|
|
extra = 0
|
|
|
|
form = IdForm
|
|
|
|
formset = IdFormset
|
|
|
|
classes = ['collapse']
|
|
|
|
|
|
|
|
|
2018-08-06 17:06:38 +02:00
|
|
|
class CharFieldModelAdmin(admin.ModelAdmin):
|
|
|
|
list_display = ['name']
|
|
|
|
ordering = ['name']
|
2018-07-26 15:53:42 +02:00
|
|
|
|
2018-08-07 19:08:20 +02:00
|
|
|
|
2018-07-26 15:53:42 +02:00
|
|
|
class EquipmentAdmin(admin.ModelAdmin):
|
2018-08-07 16:26:33 +02:00
|
|
|
list_display = ['name', 'stock', 'owner', 'pole', 'category']
|
|
|
|
ordering = ['name', 'owner', 'pole', 'category']
|
2018-08-07 19:08:20 +02:00
|
|
|
inlines = [EquipmentDefaultExtraInline,
|
|
|
|
EquipmentLostExtraInline,
|
|
|
|
EquipmentRevisionExtraInline]
|
2018-07-26 15:53:42 +02:00
|
|
|
|
|
|
|
|
2018-08-04 05:38:03 +02:00
|
|
|
admin_site.register(Equipment, EquipmentAdmin)
|
2018-08-06 17:06:38 +02:00
|
|
|
admin_site.register(EquipmentPole, CharFieldModelAdmin)
|
|
|
|
admin_site.register(EquipmentCategory, CharFieldModelAdmin)
|
2018-07-26 15:53:42 +02:00
|
|
|
admin.site.register(Equipment, EquipmentAdmin)
|