From dba9602c75f98a6ee1f18d6ef0129491f31f7059 Mon Sep 17 00:00:00 2001 From: jeremystretch Date: Mon, 27 Sep 2021 17:19:05 -0400 Subject: [PATCH] Refactor tenancy forms --- netbox/tenancy/forms.py | 196 ---------------------------- netbox/tenancy/forms/__init__.py | 5 + netbox/tenancy/forms/bulk_edit.py | 44 +++++++ netbox/tenancy/forms/bulk_import.py | 36 +++++ netbox/tenancy/forms/filtersets.py | 42 ++++++ netbox/tenancy/forms/forms.py | 48 +++++++ netbox/tenancy/forms/models.py | 47 +++++++ 7 files changed, 222 insertions(+), 196 deletions(-) delete mode 100644 netbox/tenancy/forms.py create mode 100644 netbox/tenancy/forms/__init__.py create mode 100644 netbox/tenancy/forms/bulk_edit.py create mode 100644 netbox/tenancy/forms/bulk_import.py create mode 100644 netbox/tenancy/forms/filtersets.py create mode 100644 netbox/tenancy/forms/forms.py create mode 100644 netbox/tenancy/forms/models.py diff --git a/netbox/tenancy/forms.py b/netbox/tenancy/forms.py deleted file mode 100644 index 63dcdd468..000000000 --- a/netbox/tenancy/forms.py +++ /dev/null @@ -1,196 +0,0 @@ -from django import forms -from django.utils.translation import gettext as _ - -from extras.forms import ( - AddRemoveTagsForm, CustomFieldModelForm, CustomFieldModelBulkEditForm, CustomFieldModelFilterForm, CustomFieldModelCSVForm, -) -from extras.models import Tag -from utilities.forms import ( - BootstrapMixin, CommentField, CSVModelChoiceField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, - SlugField, TagFilterField, -) -from .models import Tenant, TenantGroup - - -# -# Tenant groups -# - -class TenantGroupForm(BootstrapMixin, CustomFieldModelForm): - parent = DynamicModelChoiceField( - queryset=TenantGroup.objects.all(), - required=False - ) - slug = SlugField() - - class Meta: - model = TenantGroup - fields = [ - 'parent', 'name', 'slug', 'description', - ] - - -class TenantGroupCSVForm(CustomFieldModelCSVForm): - parent = CSVModelChoiceField( - queryset=TenantGroup.objects.all(), - required=False, - to_field_name='name', - help_text='Parent group' - ) - slug = SlugField() - - class Meta: - model = TenantGroup - fields = ('name', 'slug', 'parent', 'description') - - -class TenantGroupBulkEditForm(BootstrapMixin, CustomFieldModelBulkEditForm): - pk = forms.ModelMultipleChoiceField( - queryset=TenantGroup.objects.all(), - widget=forms.MultipleHiddenInput - ) - parent = DynamicModelChoiceField( - queryset=TenantGroup.objects.all(), - required=False - ) - description = forms.CharField( - max_length=200, - required=False - ) - - class Meta: - nullable_fields = ['parent', 'description'] - - -class TenantGroupFilterForm(BootstrapMixin, CustomFieldModelFilterForm): - model = TenantGroup - q = forms.CharField( - required=False, - widget=forms.TextInput(attrs={'placeholder': _('All Fields')}), - label=_('Search') - ) - parent_id = DynamicModelMultipleChoiceField( - queryset=TenantGroup.objects.all(), - required=False, - label=_('Parent group'), - fetch_trigger='open' - ) - - -# -# Tenants -# - -class TenantForm(BootstrapMixin, CustomFieldModelForm): - slug = SlugField() - group = DynamicModelChoiceField( - queryset=TenantGroup.objects.all(), - required=False - ) - comments = CommentField() - tags = DynamicModelMultipleChoiceField( - queryset=Tag.objects.all(), - required=False - ) - - class Meta: - model = Tenant - fields = ( - 'name', 'slug', 'group', 'description', 'comments', 'tags', - ) - fieldsets = ( - ('Tenant', ('name', 'slug', 'group', 'description', 'tags')), - ) - - -class TenantCSVForm(CustomFieldModelCSVForm): - slug = SlugField() - group = CSVModelChoiceField( - queryset=TenantGroup.objects.all(), - required=False, - to_field_name='name', - help_text='Assigned group' - ) - - class Meta: - model = Tenant - fields = ('name', 'slug', 'group', 'description', 'comments') - - -class TenantBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldModelBulkEditForm): - pk = forms.ModelMultipleChoiceField( - queryset=Tenant.objects.all(), - widget=forms.MultipleHiddenInput() - ) - group = DynamicModelChoiceField( - queryset=TenantGroup.objects.all(), - required=False - ) - - class Meta: - nullable_fields = [ - 'group', - ] - - -class TenantFilterForm(BootstrapMixin, CustomFieldModelFilterForm): - model = Tenant - field_groups = ( - ('q', 'tag'), - ('group_id',), - ) - q = forms.CharField( - required=False, - widget=forms.TextInput(attrs={'placeholder': _('All Fields')}), - label=_('Search') - ) - group_id = DynamicModelMultipleChoiceField( - queryset=TenantGroup.objects.all(), - required=False, - null_option='None', - label=_('Group'), - fetch_trigger='open' - ) - tag = TagFilterField(model) - - -# -# Form extensions -# - -class TenancyForm(forms.Form): - tenant_group = DynamicModelChoiceField( - queryset=TenantGroup.objects.all(), - required=False, - null_option='None', - initial_params={ - 'tenants': '$tenant' - } - ) - tenant = DynamicModelChoiceField( - queryset=Tenant.objects.all(), - required=False, - query_params={ - 'group_id': '$tenant_group' - } - ) - - -class TenancyFilterForm(forms.Form): - tenant_group_id = DynamicModelMultipleChoiceField( - queryset=TenantGroup.objects.all(), - required=False, - null_option='None', - label=_('Tenant group'), - fetch_trigger='open' - ) - tenant_id = DynamicModelMultipleChoiceField( - queryset=Tenant.objects.all(), - required=False, - null_option='None', - query_params={ - 'group_id': '$tenant_group_id' - }, - label=_('Tenant'), - fetch_trigger='open' - ) diff --git a/netbox/tenancy/forms/__init__.py b/netbox/tenancy/forms/__init__.py new file mode 100644 index 000000000..61f0bc961 --- /dev/null +++ b/netbox/tenancy/forms/__init__.py @@ -0,0 +1,5 @@ +from .forms import * +from .models import * +from .filtersets import * +from .bulk_edit import * +from .bulk_import import * diff --git a/netbox/tenancy/forms/bulk_edit.py b/netbox/tenancy/forms/bulk_edit.py new file mode 100644 index 000000000..b2fc7dafd --- /dev/null +++ b/netbox/tenancy/forms/bulk_edit.py @@ -0,0 +1,44 @@ +from django import forms + +from extras.forms import AddRemoveTagsForm, CustomFieldModelBulkEditForm +from tenancy.models import Tenant, TenantGroup +from utilities.forms import BootstrapMixin, DynamicModelChoiceField + +__all__ = ( + 'TenantBulkEditForm', + 'TenantGroupBulkEditForm', +) + + +class TenantGroupBulkEditForm(BootstrapMixin, CustomFieldModelBulkEditForm): + pk = forms.ModelMultipleChoiceField( + queryset=TenantGroup.objects.all(), + widget=forms.MultipleHiddenInput + ) + parent = DynamicModelChoiceField( + queryset=TenantGroup.objects.all(), + required=False + ) + description = forms.CharField( + max_length=200, + required=False + ) + + class Meta: + nullable_fields = ['parent', 'description'] + + +class TenantBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldModelBulkEditForm): + pk = forms.ModelMultipleChoiceField( + queryset=Tenant.objects.all(), + widget=forms.MultipleHiddenInput() + ) + group = DynamicModelChoiceField( + queryset=TenantGroup.objects.all(), + required=False + ) + + class Meta: + nullable_fields = [ + 'group', + ] diff --git a/netbox/tenancy/forms/bulk_import.py b/netbox/tenancy/forms/bulk_import.py new file mode 100644 index 000000000..335d71ef6 --- /dev/null +++ b/netbox/tenancy/forms/bulk_import.py @@ -0,0 +1,36 @@ +from extras.forms import CustomFieldModelCSVForm +from tenancy.models import Tenant, TenantGroup +from utilities.forms import CSVModelChoiceField, SlugField + +__all__ = ( + 'TenantCSVForm', + 'TenantGroupCSVForm', +) + + +class TenantGroupCSVForm(CustomFieldModelCSVForm): + parent = CSVModelChoiceField( + queryset=TenantGroup.objects.all(), + required=False, + to_field_name='name', + help_text='Parent group' + ) + slug = SlugField() + + class Meta: + model = TenantGroup + fields = ('name', 'slug', 'parent', 'description') + + +class TenantCSVForm(CustomFieldModelCSVForm): + slug = SlugField() + group = CSVModelChoiceField( + queryset=TenantGroup.objects.all(), + required=False, + to_field_name='name', + help_text='Assigned group' + ) + + class Meta: + model = Tenant + fields = ('name', 'slug', 'group', 'description', 'comments') diff --git a/netbox/tenancy/forms/filtersets.py b/netbox/tenancy/forms/filtersets.py new file mode 100644 index 000000000..6e2eb7fd1 --- /dev/null +++ b/netbox/tenancy/forms/filtersets.py @@ -0,0 +1,42 @@ +from django import forms +from django.utils.translation import gettext as _ + +from extras.forms import CustomFieldModelFilterForm +from tenancy.models import Tenant, TenantGroup +from utilities.forms import BootstrapMixin, DynamicModelMultipleChoiceField, TagFilterField + + +class TenantGroupFilterForm(BootstrapMixin, CustomFieldModelFilterForm): + model = TenantGroup + q = forms.CharField( + required=False, + widget=forms.TextInput(attrs={'placeholder': _('All Fields')}), + label=_('Search') + ) + parent_id = DynamicModelMultipleChoiceField( + queryset=TenantGroup.objects.all(), + required=False, + label=_('Parent group'), + fetch_trigger='open' + ) + + +class TenantFilterForm(BootstrapMixin, CustomFieldModelFilterForm): + model = Tenant + field_groups = ( + ('q', 'tag'), + ('group_id',), + ) + q = forms.CharField( + required=False, + widget=forms.TextInput(attrs={'placeholder': _('All Fields')}), + label=_('Search') + ) + group_id = DynamicModelMultipleChoiceField( + queryset=TenantGroup.objects.all(), + required=False, + null_option='None', + label=_('Group'), + fetch_trigger='open' + ) + tag = TagFilterField(model) diff --git a/netbox/tenancy/forms/forms.py b/netbox/tenancy/forms/forms.py new file mode 100644 index 000000000..cad63c1a6 --- /dev/null +++ b/netbox/tenancy/forms/forms.py @@ -0,0 +1,48 @@ +from django import forms +from django.utils.translation import gettext as _ + +from tenancy.models import Tenant, TenantGroup +from utilities.forms import DynamicModelChoiceField, DynamicModelMultipleChoiceField + +__all__ = ( + 'TenancyForm', + 'TenancyFilterForm', +) + + +class TenancyForm(forms.Form): + tenant_group = DynamicModelChoiceField( + queryset=TenantGroup.objects.all(), + required=False, + null_option='None', + initial_params={ + 'tenants': '$tenant' + } + ) + tenant = DynamicModelChoiceField( + queryset=Tenant.objects.all(), + required=False, + query_params={ + 'group_id': '$tenant_group' + } + ) + + +class TenancyFilterForm(forms.Form): + tenant_group_id = DynamicModelMultipleChoiceField( + queryset=TenantGroup.objects.all(), + required=False, + null_option='None', + label=_('Tenant group'), + fetch_trigger='open' + ) + tenant_id = DynamicModelMultipleChoiceField( + queryset=Tenant.objects.all(), + required=False, + null_option='None', + query_params={ + 'group_id': '$tenant_group_id' + }, + label=_('Tenant'), + fetch_trigger='open' + ) diff --git a/netbox/tenancy/forms/models.py b/netbox/tenancy/forms/models.py new file mode 100644 index 000000000..de3a9e515 --- /dev/null +++ b/netbox/tenancy/forms/models.py @@ -0,0 +1,47 @@ +from extras.forms import CustomFieldModelForm +from extras.models import Tag +from tenancy.models import Tenant, TenantGroup +from utilities.forms import ( + BootstrapMixin, CommentField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, SlugField, +) + +__all__ = ( + 'TenantForm', + 'TenantGroupForm', +) + + +class TenantGroupForm(BootstrapMixin, CustomFieldModelForm): + parent = DynamicModelChoiceField( + queryset=TenantGroup.objects.all(), + required=False + ) + slug = SlugField() + + class Meta: + model = TenantGroup + fields = [ + 'parent', 'name', 'slug', 'description', + ] + + +class TenantForm(BootstrapMixin, CustomFieldModelForm): + slug = SlugField() + group = DynamicModelChoiceField( + queryset=TenantGroup.objects.all(), + required=False + ) + comments = CommentField() + tags = DynamicModelMultipleChoiceField( + queryset=Tag.objects.all(), + required=False + ) + + class Meta: + model = Tenant + fields = ( + 'name', 'slug', 'group', 'description', 'comments', 'tags', + ) + fieldsets = ( + ('Tenant', ('name', 'slug', 'group', 'description', 'tags')), + )