55 lines
1.7 KiB
Python
55 lines
1.7 KiB
Python
from django.contrib import admin
|
|
from django import forms
|
|
|
|
from .models import Equipment, EquipmentRemark, EquipmentRevision
|
|
from .fields import IdField, IdWidget
|
|
|
|
from shared.admin import admin_site
|
|
|
|
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')
|
|
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
|
|
|
|
|
|
class EquipmentRemarkExtraInline(admin.TabularInline):
|
|
model = EquipmentRemark
|
|
extra = 0
|
|
form = IdForm
|
|
formset = IdFormset
|
|
|
|
|
|
class EquipmentAdmin(admin.ModelAdmin):
|
|
list_display = ['name', 'stock']
|
|
ordering = ['name']
|
|
inlines = [EquipmentRemarkExtraInline, EquipmentRevisionExtraInline]
|
|
|
|
|
|
admin_site.register(Equipment, EquipmentAdmin)
|
|
admin.site.register(Equipment, EquipmentAdmin)
|