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
|
2022-02-02 22:27:48 +01:00
|
|
|
from crispy_forms.helper import FormHelper
|
2022-02-07 12:20:54 +01:00
|
|
|
from crispy_forms.layout import Layout, Submit, HTML, Field, 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
|
|
|
|
|
2021-12-24 12:55:34 +01:00
|
|
|
from customers.models import Customer
|
2021-12-30 14:57:47 +01:00
|
|
|
from customers.models import Location
|
2021-12-24 12:55:34 +01:00
|
|
|
|
2020-05-01 15:22:53 +02:00
|
|
|
from devices.models import Device
|
2022-02-02 22:27:48 +01:00
|
|
|
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
|
|
|
|
|
2021-12-30 14:57:47 +01:00
|
|
|
from users.models import User
|
|
|
|
|
2020-05-01 15:22:53 +02:00
|
|
|
|
2022-02-02 22:27:48 +01:00
|
|
|
class DeviceCategoryForm(forms.ModelForm):
|
|
|
|
class Meta:
|
|
|
|
model = DeviceCategory
|
|
|
|
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 = {
|
|
|
|
'hx-post': reverse_lazy('device_category_create'),
|
2022-02-07 12:21:11 +01:00
|
|
|
'id': 'device-category-form',
|
2022-02-02 23:23:38 +01:00
|
|
|
}
|
2022-02-11 08:38:51 +01:00
|
|
|
self.helper.layout.append(
|
2022-02-02 22:27:48 +01:00
|
|
|
FormActions(
|
2022-02-02 23:23:38 +01:00
|
|
|
Submit('save_category', 'Save'),
|
2022-02-02 22:27:48 +01:00
|
|
|
Button('cancel', 'Cancel', css_class="btn btn-secondary",
|
2022-02-02 23:23:38 +01:00
|
|
|
onclick="closeModal()")
|
|
|
|
))
|
2022-02-02 22:27:48 +01:00
|
|
|
|
|
|
|
|
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)
|
2021-12-30 14:57:47 +01: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.
|
|
|
|
"""
|
2021-12-30 14:57:47 +01:00
|
|
|
|
|
|
|
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
|
2022-02-02 22:27:48 +01:00
|
|
|
self.helper = FormHelper()
|
2022-02-02 23:23:38 +01:00
|
|
|
self.helper.form_id = 'htmx-device-form'
|
2022-02-02 22:27:48 +01:00
|
|
|
self.helper.layout = Layout(
|
2022-02-07 12:20:54 +01:00
|
|
|
'name',
|
|
|
|
'customer',
|
2022-03-23 14:07:52 +01:00
|
|
|
Div('location',
|
2022-03-23 12:38:29 +01:00
|
|
|
HTML("""
|
|
|
|
<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-23 14:07:52 +01:00
|
|
|
css_class="input-group", id="htmx-location-target"),
|
2022-02-07 12:20:54 +01:00
|
|
|
'user',
|
2022-02-11 10:54:17 +01:00
|
|
|
Div(Field('category'),
|
|
|
|
HTML("""
|
2022-02-03 22:24:30 +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-02-02 22:27:48 +01:00
|
|
|
"""),
|
2022-03-23 14:07:52 +01:00
|
|
|
css_class="input-group", id="htmx-category-target"),
|
2022-02-07 12:20:54 +01:00
|
|
|
'serialnumber',
|
|
|
|
'description',
|
2022-02-02 22:27:48 +01:00
|
|
|
FormActions(
|
2022-02-02 23:23:38 +01:00
|
|
|
Submit('save_device', 'Save'),
|
2022-02-02 22:27:48 +01:00
|
|
|
HTML(
|
|
|
|
"""<a href="{{ request.META.HTTP_REFERER }}" class="btn btn-secondary">Cancel</a>""")
|
2022-02-03 22:24:30 +01:00
|
|
|
),
|
|
|
|
Div(css_id='htmx-modal-position', css_class="col")
|
2022-02-02 22:27:48 +01:00
|
|
|
)
|
2021-12-30 14:57:47 +01:00
|
|
|
|
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__'
|