2017-05-24 11:33:11 -04:00
|
|
|
from __future__ import unicode_literals
|
|
|
|
|
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
|
2016-07-26 14:58:37 -04:00
|
|
|
|
2016-08-23 12:05:28 -04:00
|
|
|
from extras.forms import 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
|
|
|
|
fields = ['name', 'slug']
|
|
|
|
|
|
|
|
|
|
|
|
#
|
|
|
|
# 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()
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Tenant
|
2016-07-26 17:44:32 -04:00
|
|
|
fields = ['name', 'slug', 'group', 'description', 'comments']
|
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
|
2017-05-31 17:40:11 -04:00
|
|
|
fields = ['name', 'slug', 'group', 'description', 'comments']
|
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
|
|
|
|
|
|
|
|
2016-08-22 13:11:57 -04:00
|
|
|
class TenantBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
|
2016-07-26 14:58:37 -04:00
|
|
|
pk = forms.ModelMultipleChoiceField(queryset=Tenant.objects.all(), widget=forms.MultipleHiddenInput)
|
2016-09-30 16:17:41 -04:00
|
|
|
group = forms.ModelChoiceField(queryset=TenantGroup.objects.all(), required=False)
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
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
|
2017-01-24 12:05:39 -05:00
|
|
|
q = forms.CharField(required=False, label='Search')
|
2017-03-01 13:09:19 -05:00
|
|
|
group = FilterChoiceField(
|
|
|
|
queryset=TenantGroup.objects.annotate(filter_count=Count('tenants')),
|
|
|
|
to_field_name='slug',
|
|
|
|
null_option=(0, 'None')
|
|
|
|
)
|
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(
|
|
|
|
attrs={'filter-for': 'tenant', 'nullable': 'true'}
|
|
|
|
)
|
|
|
|
)
|
|
|
|
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
|
|
|
|
|
|
|
super(TenancyForm, self).__init__(*args, **kwargs)
|