2016-07-26 14:58:37 -04:00
|
|
|
from django import forms
|
2018-05-10 12:53:11 -04:00
|
|
|
from taggit.forms import TagField
|
2016-07-26 14:58:37 -04:00
|
|
|
|
2020-01-29 13:53:26 -05:00
|
|
|
from extras.forms import (
|
2020-02-10 17:23:52 -05:00
|
|
|
AddRemoveTagsForm, CustomFieldModelForm, CustomFieldBulkEditForm, CustomFieldFilterForm,
|
2020-01-29 13:53:26 -05:00
|
|
|
)
|
2017-05-11 17:35:20 -04:00
|
|
|
from utilities.forms import (
|
2020-02-10 17:23:52 -05:00
|
|
|
APISelect, APISelectMultiple, BootstrapMixin, CommentField, DynamicModelChoiceField, FilterChoiceField, SlugField,
|
|
|
|
TagFilterField,
|
2017-05-11 17:35:20 -04:00
|
|
|
)
|
2016-07-26 14:58:37 -04:00
|
|
|
from .models import Tenant, TenantGroup
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Tenant groups
|
|
|
|
#
|
|
|
|
|
2016-12-21 14:15:18 -05:00
|
|
|
class TenantGroupForm(BootstrapMixin, forms.ModelForm):
|
2016-07-26 14:58:37 -04:00
|
|
|
slug = SlugField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = TenantGroup
|
2018-11-27 11:57:29 -05:00
|
|
|
fields = [
|
|
|
|
'name', 'slug',
|
|
|
|
]
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
2017-10-09 15:09:40 -04:00
|
|
|
class TenantGroupCSVForm(forms.ModelForm):
|
|
|
|
slug = SlugField()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = TenantGroup
|
2018-02-02 14:26:16 -05:00
|
|
|
fields = TenantGroup.csv_headers
|
2017-10-09 15:09:40 -04:00
|
|
|
help_texts = {
|
|
|
|
'name': 'Group name',
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2016-07-26 14:58:37 -04:00
|
|
|
#
|
|
|
|
# Tenants
|
|
|
|
#
|
|
|
|
|
2020-01-29 10:49:02 -05:00
|
|
|
class TenantForm(BootstrapMixin, CustomFieldModelForm):
|
2016-07-26 14:58:37 -04:00
|
|
|
slug = SlugField()
|
|
|
|
comments = CommentField()
|
2018-11-27 11:57:29 -05:00
|
|
|
tags = TagField(
|
|
|
|
required=False
|
|
|
|
)
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Tenant
|
2018-11-27 11:57:29 -05:00
|
|
|
fields = [
|
|
|
|
'name', 'slug', 'group', 'description', 'comments', 'tags',
|
|
|
|
]
|
2019-01-04 14:41:36 -05:00
|
|
|
widgets = {
|
|
|
|
'group': APISelect(
|
|
|
|
api_url="/api/tenancy/tenant-groups/"
|
|
|
|
)
|
|
|
|
}
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
2020-01-29 10:49:02 -05:00
|
|
|
class TenantCSVForm(CustomFieldModelForm):
|
2017-06-02 13:40:52 -04:00
|
|
|
slug = SlugField()
|
2017-05-31 17:40:11 -04:00
|
|
|
group = forms.ModelChoiceField(
|
|
|
|
queryset=TenantGroup.objects.all(),
|
|
|
|
required=False,
|
|
|
|
to_field_name='name',
|
2017-06-02 13:40:52 -04:00
|
|
|
help_text='Name of parent group',
|
2017-05-31 17:40:11 -04:00
|
|
|
error_messages={
|
|
|
|
'invalid_choice': 'Group not found.'
|
|
|
|
}
|
|
|
|
)
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Tenant
|
2018-02-02 14:26:16 -05:00
|
|
|
fields = Tenant.csv_headers
|
2017-06-02 13:40:52 -04:00
|
|
|
help_texts = {
|
|
|
|
'name': 'Tenant name',
|
|
|
|
'comments': 'Free-form comments'
|
|
|
|
}
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
2018-07-10 10:00:21 -04:00
|
|
|
class TenantBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
|
2018-11-27 11:57:29 -05:00
|
|
|
pk = forms.ModelMultipleChoiceField(
|
|
|
|
queryset=Tenant.objects.all(),
|
|
|
|
widget=forms.MultipleHiddenInput()
|
|
|
|
)
|
|
|
|
group = forms.ModelChoiceField(
|
|
|
|
queryset=TenantGroup.objects.all(),
|
2019-01-09 23:33:08 -05:00
|
|
|
required=False,
|
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/tenancy/tenant-groups/"
|
|
|
|
)
|
2018-11-27 11:57:29 -05:00
|
|
|
)
|
2016-09-30 16:17:41 -04:00
|
|
|
|
|
|
|
class Meta:
|
2018-11-27 11:57:29 -05:00
|
|
|
nullable_fields = [
|
|
|
|
'group',
|
|
|
|
]
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
2016-08-23 12:05:28 -04:00
|
|
|
class TenantFilterForm(BootstrapMixin, CustomFieldFilterForm):
|
|
|
|
model = Tenant
|
2018-11-27 11:57:29 -05:00
|
|
|
q = forms.CharField(
|
|
|
|
required=False,
|
|
|
|
label='Search'
|
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
group = FilterChoiceField(
|
2019-01-09 23:33:08 -05:00
|
|
|
queryset=TenantGroup.objects.all(),
|
2017-03-01 13:09:19 -05:00
|
|
|
to_field_name='slug',
|
2019-01-09 23:33:08 -05:00
|
|
|
widget=APISelectMultiple(
|
|
|
|
api_url="/api/tenancy/tenant-groups/",
|
|
|
|
value_field="slug",
|
|
|
|
null_option=True,
|
|
|
|
)
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2020-01-14 08:22:27 +00:00
|
|
|
tag = TagFilterField(model)
|
2020-01-13 20:16:13 +00:00
|
|
|
|
2017-05-11 17:35:20 -04:00
|
|
|
|
|
|
|
#
|
2019-05-09 14:36:18 -04:00
|
|
|
# Form extensions
|
2017-05-11 17:35:20 -04:00
|
|
|
#
|
2019-05-09 14:32:49 -04:00
|
|
|
|
2020-02-10 17:23:52 -05:00
|
|
|
class TenancyForm(forms.Form):
|
|
|
|
tenant_group = DynamicModelChoiceField(
|
2017-05-11 17:35:20 -04:00
|
|
|
queryset=TenantGroup.objects.all(),
|
|
|
|
required=False,
|
2019-01-04 14:41:36 -05:00
|
|
|
widget=APISelect(
|
|
|
|
api_url="/api/tenancy/tenant-groups/",
|
|
|
|
filter_for={
|
|
|
|
'tenant': 'group_id',
|
|
|
|
},
|
2018-11-27 11:57:29 -05:00
|
|
|
attrs={
|
|
|
|
'nullable': 'true',
|
|
|
|
}
|
2017-05-11 17:35:20 -04:00
|
|
|
)
|
|
|
|
)
|
2020-02-10 17:23:52 -05:00
|
|
|
tenant = DynamicModelChoiceField(
|
2017-05-11 17:35:20 -04:00
|
|
|
queryset=Tenant.objects.all(),
|
|
|
|
required=False,
|
|
|
|
widget=APISelect(
|
2019-01-04 14:41:36 -05:00
|
|
|
api_url='/api/tenancy/tenants/'
|
2017-05-11 17:35:20 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
def __init__(self, *args, **kwargs):
|
|
|
|
|
|
|
|
# Initialize helper selector
|
|
|
|
instance = kwargs.get('instance')
|
|
|
|
if instance and instance.tenant is not None:
|
2017-07-11 14:52:50 -04:00
|
|
|
initial = kwargs.get('initial', {}).copy()
|
2017-05-11 17:52:23 -04:00
|
|
|
initial['tenant_group'] = instance.tenant.group
|
|
|
|
kwargs['initial'] = initial
|
2017-05-11 17:35:20 -04:00
|
|
|
|
2019-04-10 08:42:27 -05:00
|
|
|
super().__init__(*args, **kwargs)
|
2019-05-09 14:36:18 -04:00
|
|
|
|
|
|
|
|
|
|
|
class TenancyFilterForm(forms.Form):
|
|
|
|
tenant_group = FilterChoiceField(
|
|
|
|
queryset=TenantGroup.objects.all(),
|
|
|
|
to_field_name='slug',
|
|
|
|
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',
|
|
|
|
widget=APISelectMultiple(
|
|
|
|
api_url="/api/tenancy/tenants/",
|
|
|
|
value_field="slug",
|
|
|
|
null_option=True,
|
|
|
|
)
|
|
|
|
)
|