network_inventory/devices/forms.py

44 lines
1.4 KiB
Python
Raw Normal View History

2020-05-01 15:22:53 +02:00
import floppyforms.__future__ as forms
from core import utils
from devices.models import Device
from devices.models import Warranty
class DeviceCreateForm(forms.ModelForm):
class Meta:
model = Device
fields = (
'name',
'customer',
)
def __init__(self, user=None, *args, **kwargs):
2020-05-01 16:11:47 +02:00
"""
If the user is not a superuser it's always assigned to a customer which
we can use to assign to the field.
"""
2020-05-01 15:22:53 +02:00
super(DeviceCreateForm, self).__init__(*args, **kwargs)
if not user.is_superuser:
self.fields['customer'].queryset = utils.get_customers(user)
class DeviceUpdateForm(forms.ModelForm):
class Meta:
model = Device
fields = '__all__'
WarrantyFormSet = forms.inlineformset_factory(Device,
Warranty,
fields=(
'valid_from',
'valid_until',
'warranty_type',
),
exclude=[],
can_delete=False,
form=DeviceUpdateForm,
max_num=1)