mirror of
https://github.com/netbox-community/netbox.git
synced 2025-08-17 04:58:16 -06:00
add disk field for bulk edit and virt add/edit
This commit is contained in:
parent
fd5c832a56
commit
289f59c8f9
@ -35,6 +35,7 @@ __all__ = (
|
||||
'CSVMultipleChoiceField',
|
||||
'CSVMultipleContentTypeField',
|
||||
'CSVTypedChoiceField',
|
||||
'DiskField',
|
||||
'DynamicModelChoiceField',
|
||||
'DynamicModelMultipleChoiceField',
|
||||
'ExpandableIPAddressField',
|
||||
@ -164,7 +165,7 @@ class MemoryField(forms.MultiValueField):
|
||||
fields = (
|
||||
forms.IntegerField(required=False),
|
||||
forms.ChoiceField(
|
||||
choices=MemoryUnitChoices.CHOICES,
|
||||
choices=MemoryUnitChoices.MEMORY_CHOICES,
|
||||
required=False
|
||||
),
|
||||
)
|
||||
@ -185,6 +186,38 @@ class MemoryField(forms.MultiValueField):
|
||||
raise forms.ValidationError("Please enter a memory value when unit is selected.")
|
||||
|
||||
|
||||
class DiskField(forms.MultiValueField):
|
||||
widget = widgets.DiskWidget
|
||||
MULTIPLIERS = {
|
||||
MemoryUnitChoices.UNIT_GB: 1024**0,
|
||||
MemoryUnitChoices.UNIT_TB: 1024**1,
|
||||
}
|
||||
|
||||
def __init__(self, **kwargs):
|
||||
fields = (
|
||||
forms.IntegerField(required=False),
|
||||
forms.ChoiceField(
|
||||
choices=MemoryUnitChoices.DISK_CHOICES,
|
||||
required=False
|
||||
),
|
||||
)
|
||||
super(DiskField, self).__init__(
|
||||
fields=fields, required=False,
|
||||
require_all_fields=False, **kwargs
|
||||
)
|
||||
|
||||
@classmethod
|
||||
def compress(cls, data):
|
||||
if data:
|
||||
size, unit = data
|
||||
if size and not unit:
|
||||
raise forms.ValidationError("Disk unit cannot be blank.")
|
||||
elif size and unit:
|
||||
return size * cls.MULTIPLIERS[unit]
|
||||
elif not size and unit:
|
||||
raise forms.ValidationError("Please enter a disk value when unit is selected.")
|
||||
|
||||
|
||||
#
|
||||
# Content type fields
|
||||
#
|
||||
|
@ -6,8 +6,8 @@ from django.conf import settings
|
||||
from django.contrib.postgres.forms import SimpleArrayField
|
||||
|
||||
from utilities.choices import ColorChoices
|
||||
from virtualization.choices import MemoryUnitChoices
|
||||
from .utils import add_blank_choice, parse_numeric_range
|
||||
from virtualization.choices import MemoryUnitChoices
|
||||
|
||||
__all__ = (
|
||||
'APISelect',
|
||||
@ -17,6 +17,7 @@ __all__ = (
|
||||
'ColorSelect',
|
||||
'DatePicker',
|
||||
'DateTimePicker',
|
||||
'DiskWidget',
|
||||
'MemoryWidget',
|
||||
'NumericArrayField',
|
||||
'SelectSpeedWidget',
|
||||
@ -321,7 +322,7 @@ class MemoryWidget(forms.MultiWidget):
|
||||
def __init__(self, attrs=None):
|
||||
widgets = (
|
||||
forms.NumberInput(),
|
||||
StaticSelect(choices=add_blank_choice(MemoryUnitChoices.CHOICES))
|
||||
StaticSelect(choices=add_blank_choice(MemoryUnitChoices.MEMORY_CHOICES))
|
||||
)
|
||||
super(MemoryWidget, self).__init__(widgets, attrs)
|
||||
|
||||
@ -330,3 +331,21 @@ class MemoryWidget(forms.MultiWidget):
|
||||
return [value, MemoryUnitChoices.UNIT_MB]
|
||||
else:
|
||||
return ['', '']
|
||||
|
||||
|
||||
class DiskWidget(forms.MultiWidget):
|
||||
"""
|
||||
Disk Widget.
|
||||
"""
|
||||
def __init__(self, attrs=None):
|
||||
widgets = (
|
||||
forms.NumberInput(),
|
||||
StaticSelect(choices=add_blank_choice(MemoryUnitChoices.DISK_CHOICES))
|
||||
)
|
||||
super(DiskWidget, self).__init__(widgets, attrs)
|
||||
|
||||
def decompress(self, value):
|
||||
if value:
|
||||
return [value, MemoryUnitChoices.UNIT_GB]
|
||||
else:
|
||||
return ['', '']
|
||||
|
@ -84,7 +84,7 @@
|
||||
</div>
|
||||
</div>
|
||||
|
||||
{% elif field|widget_type == 'memorywidget'%}
|
||||
{% elif field|widget_type == 'memorywidget' or field|widget_type == 'diskwidget' %}
|
||||
<div class="row mb-3">
|
||||
<label class="col-sm-3 col-form-label text-lg-end{% if field.field.required %} required{% endif %}" for="{{ field.id_for_label }}">
|
||||
{{ field.label }}
|
||||
|
@ -10,7 +10,12 @@ class MemoryUnitChoices(ChoiceSet):
|
||||
UNIT_GB = 'gb'
|
||||
UNIT_TB = 'tb'
|
||||
|
||||
CHOICES = (
|
||||
DISK_CHOICES = (
|
||||
(UNIT_GB, 'GB'),
|
||||
(UNIT_TB, 'TB'),
|
||||
)
|
||||
|
||||
MEMORY_CHOICES = (
|
||||
(UNIT_MB, 'MB'),
|
||||
(UNIT_GB, 'GB'),
|
||||
(UNIT_TB, 'TB'),
|
||||
|
@ -7,8 +7,8 @@ from extras.forms import AddRemoveTagsForm, CustomFieldModelBulkEditForm
|
||||
from ipam.models import VLAN
|
||||
from tenancy.models import Tenant
|
||||
from utilities.forms import (
|
||||
add_blank_choice, BulkEditNullBooleanSelect, BulkRenameForm, CommentField, DynamicModelChoiceField,
|
||||
DynamicModelMultipleChoiceField, SmallTextarea, StaticSelect, MemoryField
|
||||
add_blank_choice, BulkEditNullBooleanSelect, BulkRenameForm, CommentField, DiskField, DynamicModelChoiceField,
|
||||
DynamicModelMultipleChoiceField, MemoryField, SmallTextarea, StaticSelect
|
||||
)
|
||||
from virtualization.choices import *
|
||||
from virtualization.models import *
|
||||
@ -132,13 +132,7 @@ class VirtualMachineBulkEditForm(AddRemoveTagsForm, CustomFieldModelBulkEditForm
|
||||
label='vCPUs'
|
||||
)
|
||||
memory = MemoryField()
|
||||
# memory = forms.IntegerField(
|
||||
# required=False
|
||||
# )
|
||||
disk = forms.IntegerField(
|
||||
required=False,
|
||||
label='Disk (GB)'
|
||||
)
|
||||
disk = DiskField()
|
||||
comments = CommentField(
|
||||
widget=SmallTextarea,
|
||||
label='Comments'
|
||||
|
@ -10,8 +10,8 @@ from extras.models import Tag
|
||||
from ipam.models import IPAddress, VLAN, VLANGroup
|
||||
from tenancy.forms import TenancyForm
|
||||
from utilities.forms import (
|
||||
BootstrapMixin, CommentField, ConfirmationForm, DynamicModelChoiceField, DynamicModelMultipleChoiceField,
|
||||
JSONField, SlugField, StaticSelect, MemoryField
|
||||
BootstrapMixin, CommentField, ConfirmationForm, DiskField, DynamicModelChoiceField, DynamicModelMultipleChoiceField,
|
||||
JSONField, MemoryField, SlugField, StaticSelect
|
||||
)
|
||||
from virtualization.models import *
|
||||
|
||||
@ -206,6 +206,7 @@ class VirtualMachineForm(TenancyForm, CustomFieldModelForm):
|
||||
required=False
|
||||
)
|
||||
memory = MemoryField()
|
||||
disk = DiskField()
|
||||
|
||||
class Meta:
|
||||
model = VirtualMachine
|
||||
|
Loading…
Reference in New Issue
Block a user