| 123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185 |
- from django import forms
- from taggit.forms import TagField
- from extras.forms import (
- AddRemoveTagsForm, CustomFieldModelForm, CustomFieldBulkEditForm, CustomFieldModelCSVForm, CustomFieldFilterForm,
- )
- from utilities.forms import (
- APISelect, APISelectMultiple, BootstrapMixin, ChainedFieldsMixin, ChainedModelChoiceField, CommentField,
- FilterChoiceField, SlugField, TagFilterField
- )
- from .models import Tenant, TenantGroup
- #
- # Tenant groups
- #
- class TenantGroupForm(BootstrapMixin, forms.ModelForm):
- slug = SlugField()
- class Meta:
- model = TenantGroup
- fields = [
- 'name', 'slug',
- ]
- class TenantGroupCSVForm(forms.ModelForm):
- slug = SlugField()
- class Meta:
- model = TenantGroup
- fields = TenantGroup.csv_headers
- help_texts = {
- 'name': 'Group name',
- }
- #
- # Tenants
- #
- class TenantForm(BootstrapMixin, CustomFieldModelForm):
- slug = SlugField()
- comments = CommentField()
- tags = TagField(
- required=False
- )
- class Meta:
- model = Tenant
- fields = [
- 'name', 'slug', 'group', 'description', 'comments', 'tags',
- ]
- widgets = {
- 'group': APISelect(
- api_url="/api/tenancy/tenant-groups/"
- )
- }
- class TenantCSVForm(CustomFieldModelForm):
- slug = SlugField()
- group = forms.ModelChoiceField(
- queryset=TenantGroup.objects.all(),
- required=False,
- to_field_name='name',
- help_text='Name of parent group',
- error_messages={
- 'invalid_choice': 'Group not found.'
- }
- )
- class Meta:
- model = Tenant
- fields = Tenant.csv_headers
- help_texts = {
- 'name': 'Tenant name',
- 'comments': 'Free-form comments'
- }
- class TenantBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
- pk = forms.ModelMultipleChoiceField(
- queryset=Tenant.objects.all(),
- widget=forms.MultipleHiddenInput()
- )
- group = forms.ModelChoiceField(
- queryset=TenantGroup.objects.all(),
- required=False,
- widget=APISelect(
- api_url="/api/tenancy/tenant-groups/"
- )
- )
- class Meta:
- nullable_fields = [
- 'group',
- ]
- class TenantFilterForm(BootstrapMixin, CustomFieldFilterForm):
- model = Tenant
- q = forms.CharField(
- required=False,
- label='Search'
- )
- group = FilterChoiceField(
- queryset=TenantGroup.objects.all(),
- to_field_name='slug',
- null_label='-- None --',
- widget=APISelectMultiple(
- api_url="/api/tenancy/tenant-groups/",
- value_field="slug",
- null_option=True,
- )
- )
- tag = TagFilterField(model)
- #
- # Form extensions
- #
- class TenancyForm(ChainedFieldsMixin, forms.Form):
- tenant_group = forms.ModelChoiceField(
- queryset=TenantGroup.objects.all(),
- required=False,
- widget=APISelect(
- api_url="/api/tenancy/tenant-groups/",
- filter_for={
- 'tenant': 'group_id',
- },
- attrs={
- 'nullable': 'true',
- }
- )
- )
- tenant = ChainedModelChoiceField(
- queryset=Tenant.objects.all(),
- chains=(
- ('group', 'tenant_group'),
- ),
- required=False,
- widget=APISelect(
- api_url='/api/tenancy/tenants/'
- )
- )
- def __init__(self, *args, **kwargs):
- # Initialize helper selector
- instance = kwargs.get('instance')
- if instance and instance.tenant is not None:
- initial = kwargs.get('initial', {}).copy()
- initial['tenant_group'] = instance.tenant.group
- kwargs['initial'] = initial
- super().__init__(*args, **kwargs)
- class TenancyFilterForm(forms.Form):
- tenant_group = FilterChoiceField(
- queryset=TenantGroup.objects.all(),
- to_field_name='slug',
- null_label='-- None --',
- widget=APISelectMultiple(
- api_url="/api/tenancy/tenant-groups/",
- value_field="slug",
- null_option=True,
- filter_for={
- 'tenant': 'group'
- }
- )
- )
- tenant = FilterChoiceField(
- queryset=Tenant.objects.all(),
- to_field_name='slug',
- null_label='-- None --',
- widget=APISelectMultiple(
- api_url="/api/tenancy/tenants/",
- value_field="slug",
- null_option=True,
- )
- )
|