network_inventory/src/devices/forms.py

190 lines
5.5 KiB
Python
Raw Permalink Normal View History

2022-02-02 23:23:38 +01:00
from django.urls import reverse_lazy
2020-05-01 15:22:53 +02:00
import floppyforms.__future__ as forms
from crispy_forms.helper import FormHelper
2022-03-23 15:43:08 +01:00
from crispy_forms.layout import Layout, Submit, HTML, Button, Div
2022-02-02 23:23:38 +01:00
from crispy_forms.bootstrap import FormActions
2020-05-01 15:22:53 +02:00
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
from devices.models import DeviceCategory
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 DeviceCategoryForm(forms.ModelForm):
class Meta:
model = DeviceCategory
2022-03-27 14:50:44 +02:00
fields = ("name",)
def __init__(self, *args, **kwargs):
super(DeviceCategoryForm, self).__init__(*args, **kwargs)
2022-02-11 08:38:51 +01:00
self.helper = FormHelper(self)
2022-02-02 23:23:38 +01:00
self.helper.attrs = {
2022-03-27 14:50:44 +02:00
"hx-post": reverse_lazy("device_category_create"),
"id": "device-category-form",
2022-02-02 23:23:38 +01:00
}
2022-02-11 08:38:51 +01:00
self.helper.layout.append(
FormActions(
2022-03-27 14:50:44 +02:00
Submit("save_category", "Save"),
Button(
"cancel",
"Cancel",
css_class="btn btn-secondary",
onclick="closeModal()",
),
)
)
2020-05-01 15:22:53 +02:00
class DeviceCreateForm(forms.ModelForm):
class Meta:
model = Device
fields = (
2022-03-27 14:50:44 +02:00
"name",
"customer",
2020-05-01 15:22:53 +02: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.
"""
2020-05-01 15:22:53 +02:00
super(DeviceCreateForm, self).__init__(*args, **kwargs)
2022-03-27 14:50:44 +02:00
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)
2023-07-10 16:47:01 +02:00
customers = utils.objects_for_allowed_customers(Customer, user=request.user)
locations = utils.objects_for_allowed_customers(Location, user=request.user)
2022-03-27 14:50:44 +02:00
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
self.helper = FormHelper()
2022-03-27 14:50:44 +02:00
self.helper.form_id = "htmx-device-form"
self.helper.layout = Layout(
2022-03-27 14:50:44 +02:00
"name",
"customer",
2022-03-23 14:11:11 +01:00
Div(
2022-03-27 14:50:44 +02:00
Div("location", id="htmx-location-target"),
HTML(
"""
2022-03-23 18:08:37 +01:00
<a hx-get="{% url 'htmx_create_location' %}"
hx-swap="innerHTML" hx-target="#htmx-modal-position"
href=""
class="add" title="Add" data-toggle="tooltip"><i
class="material-icons">add</i></a>
2022-03-27 14:50:44 +02:00
"""
),
css_class="input-group",
),
"user",
2022-03-23 14:11:11 +01:00
Div(
2022-03-27 14:50:44 +02:00
Div("category", id="htmx-category-target"),
HTML(
"""
2022-03-23 18:08:37 +01:00
<a hx-get="{% url 'device_category_create' %}"
hx-swap="innerHTML" hx-target="#htmx-modal-position"
href=""
class="add" title="Add" data-toggle="tooltip"><i
class="material-icons">add</i></a>
2022-03-27 14:50:44 +02:00
"""
),
css_class="input-group",
),
"serialnumber",
"description",
FormActions(
2022-03-27 14:50:44 +02:00
Submit("save_device", "Save"),
HTML(
2022-03-23 18:08:37 +01:00
"""<a href="{{ request.META.HTTP_REFERER }}"
2022-03-27 14:50:44 +02:00
class="btn btn-secondary">Cancel</a>"""
),
2022-02-03 22:24:30 +01:00
),
2022-03-27 14:50:44 +02:00
Div(css_id="htmx-modal-position", css_class="col"),
)
2020-05-01 15:22:53 +02:00
class Meta:
model = Device
2022-03-27 14:50:44 +02:00
fields = "__all__"
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(
2022-03-27 14:50:44 +02:00
"Valid from date must be before valid until date"
)
2021-12-29 14:57:12 +01:00
return cleaned_data
2020-05-01 16:57:38 +02:00
class Meta:
model = Warranty
2022-03-27 14:50:44 +02:00
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(
2022-03-27 14:50:44 +02:00
"Valid from date must be before valid until date"
)
2021-12-29 14:57:12 +01:00
return cleaned_data
2020-05-03 20:07:22 +02:00
class Meta:
model = Warranty
2022-03-27 14:50:44 +02:00
fields = "__all__"
2020-05-03 21:10:53 +02:00
class DeviceInNetCreateForm(forms.ModelForm):
"""
Basic form class to use crispies HTML5 forms.
"""
2022-03-27 14:50:44 +02:00
2020-05-03 21:10:53 +02:00
class Meta:
model = DeviceInNet
2022-03-27 14:50:44 +02:00
fields = "__all__"
2020-05-03 22:13:47 +02:00
class DeviceInNetUpdateForm(forms.ModelForm):
"""
Basic form class to use crispies HTML5 forms.
"""
2022-03-27 14:50:44 +02:00
2020-05-03 22:13:47 +02:00
class Meta:
model = DeviceInNet
2022-03-27 14:50:44 +02:00
fields = "__all__"