2016-07-26 14:58:37 -04:00
|
|
|
from django import forms
|
|
|
|
from django.db.models import Count
|
|
|
|
|
2016-08-22 13:11:57 -04:00
|
|
|
from extras.forms import CustomFieldForm, CustomFieldBulkEditForm
|
2016-08-15 15:24:23 -04:00
|
|
|
from utilities.forms import BootstrapMixin, BulkImportForm, CommentField, CSVDataField, SlugField
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
from .models import Tenant, TenantGroup
|
|
|
|
|
|
|
|
|
2016-08-02 16:04:25 -04:00
|
|
|
def bulkedit_tenantgroup_choices():
|
|
|
|
"""
|
|
|
|
Include an option to remove the currently assigned TenantGroup from a Tenant.
|
|
|
|
"""
|
|
|
|
choices = [
|
|
|
|
(None, '---------'),
|
|
|
|
(0, 'None'),
|
|
|
|
]
|
|
|
|
choices += [(g.pk, g.name) for g in TenantGroup.objects.all()]
|
|
|
|
return choices
|
|
|
|
|
|
|
|
|
2016-07-28 15:59:49 -04:00
|
|
|
def bulkedit_tenant_choices():
|
|
|
|
"""
|
|
|
|
Include an option to remove the currently assigned Tenant from an object.
|
|
|
|
"""
|
|
|
|
choices = [
|
|
|
|
(None, '---------'),
|
|
|
|
(0, 'None'),
|
|
|
|
]
|
|
|
|
choices += [(t.pk, t.name) for t in Tenant.objects.all()]
|
|
|
|
return choices
|
|
|
|
|
|
|
|
|
2016-07-26 14:58:37 -04:00
|
|
|
#
|
|
|
|
# Tenant groups
|
|
|
|
#
|
|
|
|
|
|
|
|
class TenantGroupForm(forms.ModelForm, BootstrapMixin):
|
|
|
|
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
|
|
|
|
|
|
|
|
|
|
|
class TenantFromCSVForm(forms.ModelForm):
|
2016-08-02 16:04:25 -04:00
|
|
|
group = forms.ModelChoiceField(TenantGroup.objects.all(), required=False, to_field_name='name',
|
2016-07-26 14:58:37 -04:00
|
|
|
error_messages={'invalid_choice': 'Group not found.'})
|
|
|
|
|
|
|
|
class Meta:
|
|
|
|
model = Tenant
|
2016-07-26 17:47:40 -04:00
|
|
|
fields = ['name', 'slug', 'group', 'description']
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
class TenantImportForm(BulkImportForm, BootstrapMixin):
|
|
|
|
csv = CSVDataField(csv_form=TenantFromCSVForm)
|
|
|
|
|
|
|
|
|
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-08-02 16:04:25 -04:00
|
|
|
group = forms.TypedChoiceField(choices=bulkedit_tenantgroup_choices, coerce=int, required=False, label='Group')
|
2016-07-26 14:58:37 -04:00
|
|
|
|
|
|
|
|
|
|
|
def tenant_group_choices():
|
|
|
|
group_choices = TenantGroup.objects.annotate(tenant_count=Count('tenants'))
|
|
|
|
return [(g.slug, u'{} ({})'.format(g.name, g.tenant_count)) for g in group_choices]
|
|
|
|
|
|
|
|
|
|
|
|
class TenantFilterForm(forms.Form, BootstrapMixin):
|
|
|
|
group = forms.MultipleChoiceField(required=False, choices=tenant_group_choices,
|
|
|
|
widget=forms.SelectMultiple(attrs={'size': 8}))
|