forms.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  1. from __future__ import unicode_literals
  2. from django import forms
  3. from django.db.models import Count
  4. from extras.forms import CustomFieldForm, CustomFieldBulkEditForm, CustomFieldFilterForm
  5. from utilities.forms import (
  6. APISelect, BootstrapMixin, ChainedFieldsMixin, ChainedModelChoiceField, CommentField, FilterChoiceField, SlugField,
  7. )
  8. from .models import Tenant, TenantGroup
  9. #
  10. # Tenant groups
  11. #
  12. class TenantGroupForm(BootstrapMixin, forms.ModelForm):
  13. slug = SlugField()
  14. class Meta:
  15. model = TenantGroup
  16. fields = ['name', 'slug']
  17. class TenantGroupCSVForm(forms.ModelForm):
  18. slug = SlugField()
  19. class Meta:
  20. model = TenantGroup
  21. fields = ['name', 'slug']
  22. help_texts = {
  23. 'name': 'Group name',
  24. }
  25. #
  26. # Tenants
  27. #
  28. class TenantForm(BootstrapMixin, CustomFieldForm):
  29. slug = SlugField()
  30. comments = CommentField()
  31. class Meta:
  32. model = Tenant
  33. fields = ['name', 'slug', 'group', 'description', 'comments']
  34. class TenantCSVForm(forms.ModelForm):
  35. slug = SlugField()
  36. group = forms.ModelChoiceField(
  37. queryset=TenantGroup.objects.all(),
  38. required=False,
  39. to_field_name='name',
  40. help_text='Name of parent group',
  41. error_messages={
  42. 'invalid_choice': 'Group not found.'
  43. }
  44. )
  45. class Meta:
  46. model = Tenant
  47. fields = ['name', 'slug', 'group', 'description', 'comments']
  48. help_texts = {
  49. 'name': 'Tenant name',
  50. 'comments': 'Free-form comments'
  51. }
  52. class TenantBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
  53. pk = forms.ModelMultipleChoiceField(queryset=Tenant.objects.all(), widget=forms.MultipleHiddenInput)
  54. group = forms.ModelChoiceField(queryset=TenantGroup.objects.all(), required=False)
  55. class Meta:
  56. nullable_fields = ['group']
  57. class TenantFilterForm(BootstrapMixin, CustomFieldFilterForm):
  58. model = Tenant
  59. q = forms.CharField(required=False, label='Search')
  60. group = FilterChoiceField(
  61. queryset=TenantGroup.objects.annotate(filter_count=Count('tenants')),
  62. to_field_name='slug',
  63. null_option=(0, 'None')
  64. )
  65. #
  66. # Tenancy form extension
  67. #
  68. class TenancyForm(ChainedFieldsMixin, forms.Form):
  69. tenant_group = forms.ModelChoiceField(
  70. queryset=TenantGroup.objects.all(),
  71. required=False,
  72. widget=forms.Select(
  73. attrs={'filter-for': 'tenant', 'nullable': 'true'}
  74. )
  75. )
  76. tenant = ChainedModelChoiceField(
  77. queryset=Tenant.objects.all(),
  78. chains=(
  79. ('group', 'tenant_group'),
  80. ),
  81. required=False,
  82. widget=APISelect(
  83. api_url='/api/tenancy/tenants/?group_id={{tenant_group}}'
  84. )
  85. )
  86. def __init__(self, *args, **kwargs):
  87. # Initialize helper selector
  88. instance = kwargs.get('instance')
  89. if instance and instance.tenant is not None:
  90. initial = kwargs.get('initial', {}).copy()
  91. initial['tenant_group'] = instance.tenant.group
  92. kwargs['initial'] = initial
  93. super(TenancyForm, self).__init__(*args, **kwargs)