network_inventory/computers/forms.py

84 lines
3.0 KiB
Python
Raw Normal View History

import floppyforms.__future__ as forms
2020-02-29 15:18:29 +01:00
from core import utils
2020-02-29 15:18:29 +01:00
from computers.models import Computer
2020-03-17 22:00:28 +01:00
from computers.models import ComputerCpuRelation
2020-03-17 23:03:24 +01:00
from computers.models import ComputerRamRelation
2020-03-17 22:00:28 +01:00
from devices.models import Warranty
class ComputerCreateForm(forms.ModelForm):
class Meta:
model = Computer
fields = (
'name',
'customer',
)
2020-02-29 15:18:29 +01:00
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.
"""
super(ComputerCreateForm, self).__init__(*args, **kwargs)
if not user.is_superuser:
2020-02-29 15:18:29 +01:00
self.fields['customer'].queryset = utils.get_customers(user)
2020-02-27 22:01:07 +01:00
class ComputerUpdateForm(forms.ModelForm):
class Meta:
model = Computer
fields = (
'name',
'description',
'serialnumber',
'category',
'owner',
'customer',
'manufacturer',
'model',
'location',
'user',
'installation_date',
)
2020-03-17 22:00:28 +01:00
ComputerFormSet = forms.inlineformset_factory(Computer,
Warranty,
fields=(
'valid_from',
'valid_until',
'warranty_type',
),
exclude=[],
can_delete=False,
form=ComputerUpdateForm,
max_num=1)
CpuFormSet = forms.inlineformset_factory(Computer,
ComputerCpuRelation,
fields=(
'cpu',
'computer',
'amount',
),
exclude=[],
can_delete=False,
form=ComputerUpdateForm,
max_num=1)
2020-03-17 23:03:24 +01:00
RamFormSet = forms.inlineformset_factory(Computer,
ComputerRamRelation,
fields=(
'ram',
'computer',
'amount',
),
exclude=[],
can_delete=False,
form=ComputerUpdateForm,
max_num=1)