network_inventory/devices/forms.py

115 lines
3.2 KiB
Python
Raw Normal View History

2020-05-01 15:22:53 +02:00
import floppyforms.__future__ as forms
from core import utils
from customers.models import Customer
from customers.models import Location
2020-05-01 15:22:53 +02:00
from devices.models import Device
2020-05-03 21:10:53 +02:00
from devices.models import DeviceInNet
2020-05-01 15:22:53 +02:00
from devices.models import Warranty
from users.models import User
2020-05-01 15:22:53 +02:00
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)
self.fields['customer'].queryset = (
utils.objects_for_allowed_customers(
Customer, user=user))
2020-05-01 15:22:53 +02:00
class DeviceUpdateForm(forms.ModelForm):
2020-05-03 20:08:57 +02:00
"""
Basic form class to use crispies HTML5 forms.
"""
def __init__(self, request, *args, **kwargs):
super(DeviceUpdateForm, self).__init__(*args, **kwargs)
customers = utils.objects_for_allowed_customers(Customer,
user=request.user)
locations = utils.objects_for_allowed_customers(Location,
user=request.user)
users = utils.objects_for_allowed_customers(User,
user=request.user)
self.fields['customer'].queryset = customers
self.fields['location'].queryset = locations
self.fields['user'].queryset = users
2020-05-01 15:22:53 +02:00
class Meta:
model = Device
fields = '__all__'
2020-05-03 21:47:45 +02:00
exclude = ('net',)
2020-05-01 15:22:53 +02:00
2020-05-01 16:57:38 +02:00
class WarrantyCreateForm(forms.ModelForm):
2020-05-03 20:08:57 +02:00
"""
Basic form class to use crispies HTML5 forms.
"""
2021-12-29 14:57:12 +01:00
def clean(self):
cleaned_data = super().clean()
valid_from = cleaned_data.get("valid_from")
valid_until = cleaned_data.get("valid_until")
if valid_from and valid_until:
if valid_from > valid_until:
raise forms.ValidationError(
"Valid from date must be before valid until date")
return cleaned_data
2020-05-01 16:57:38 +02:00
class Meta:
model = Warranty
fields = '__all__'
2020-05-03 20:07:22 +02:00
class WarrantyUpdateForm(forms.ModelForm):
"""
Basic form class to use crispies HTML5 forms.
"""
2021-12-29 14:57:12 +01:00
def clean(self):
cleaned_data = super().clean()
valid_from = cleaned_data.get("valid_from")
valid_until = cleaned_data.get("valid_until")
if valid_from and valid_until:
if valid_from > valid_until:
raise forms.ValidationError(
"Valid from date must be before valid until date")
return cleaned_data
2020-05-03 20:07:22 +02:00
class Meta:
model = Warranty
fields = '__all__'
2020-05-03 21:10:53 +02:00
class DeviceInNetCreateForm(forms.ModelForm):
"""
Basic form class to use crispies HTML5 forms.
"""
class Meta:
model = DeviceInNet
fields = '__all__'
2020-05-03 22:13:47 +02:00
class DeviceInNetUpdateForm(forms.ModelForm):
"""
Basic form class to use crispies HTML5 forms.
"""
class Meta:
model = DeviceInNet
fields = '__all__'