2016-07-26 14:58:37 -04:00
|
|
|
from django import forms
|
2016-09-20 11:08:25 -04:00
|
|
|
from django.db.models import Count
|
2018-05-10 12:53:11 -04:00
|
|
|
from taggit.forms import TagField
|
2016-07-26 14:58:37 -04:00
|
|
|
|
2018-07-10 10:00:21 -04:00
|
|
|
from extras.forms import AddRemoveTagsForm, CustomFieldForm, CustomFieldBulkEditForm, CustomFieldFilterForm
|
2017-05-11 17:35:20 -04:00
|
|
|
from utilities.forms import (
|
2017-05-31 17:40:11 -04:00
|
|
|
APISelect, BootstrapMixin, ChainedFieldsMixin, ChainedModelChoiceField, CommentField, FilterChoiceField, SlugField,
|
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
|
|
|
|
#
|
|
|
|
|
2016-08-15 15:24:23 -04:00
|
|
|
class TenantForm(BootstrapMixin, CustomFieldForm):
|
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',
|
|
|
|
]
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
2017-05-31 17:40:11 -04:00
|
|
|
class TenantCSVForm(forms.ModelForm):
|
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(),
|
|
|
|
required=False
|
|
|
|
)
|
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(
|
2018-11-27 11:57:29 -05:00
|
|
|
queryset=TenantGroup.objects.annotate(
|
|
|
|
filter_count=Count('tenants')
|
|
|
|
),
|
2017-03-01 13:09:19 -05:00
|
|
|
to_field_name='slug',
|
2017-12-26 12:08:22 -05:00
|
|
|
null_label='-- None --'
|
2017-03-01 13:09:19 -05:00
|
|
|
)
|
2017-05-11 17:35:20 -04:00
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# Tenancy form extension
|
|
|
|
#
|
|
|
|
|
|
|
|
class TenancyForm(ChainedFieldsMixin, forms.Form):
|
|
|
|
tenant_group = forms.ModelChoiceField(
|
|
|
|
queryset=TenantGroup.objects.all(),
|
|
|
|
required=False,
|
|
|
|
widget=forms.Select(
|
2018-11-27 11:57:29 -05:00
|
|
|
attrs={
|
|
|
|
'filter-for': 'tenant',
|
|
|
|
'nullable': 'true',
|
|
|
|
}
|
2017-05-11 17:35:20 -04:00
|
|
|
)
|
|
|
|
)
|
|
|
|
tenant = ChainedModelChoiceField(
|
|
|
|
queryset=Tenant.objects.all(),
|
2017-05-25 14:33:50 -04:00
|
|
|
chains=(
|
|
|
|
('group', 'tenant_group'),
|
|
|
|
),
|
2017-05-11 17:35:20 -04:00
|
|
|
required=False,
|
|
|
|
widget=APISelect(
|
|
|
|
api_url='/api/tenancy/tenants/?group_id={{tenant_group}}'
|
|
|
|
)
|
|
|
|
)
|
|
|
|
|
|
|
|
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
|
|
|
|
2018-11-27 10:52:24 -05:00
|
|
|
super().__init__(*args, **kwargs)
|