forms.py 4.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185
  1. from django import forms
  2. from taggit.forms import TagField
  3. from extras.forms import (
  4. AddRemoveTagsForm, CustomFieldModelForm, CustomFieldBulkEditForm, CustomFieldModelCSVForm, CustomFieldFilterForm,
  5. )
  6. from utilities.forms import (
  7. APISelect, APISelectMultiple, BootstrapMixin, ChainedFieldsMixin, ChainedModelChoiceField, CommentField,
  8. FilterChoiceField, SlugField, TagFilterField
  9. )
  10. from .models import Tenant, TenantGroup
  11. #
  12. # Tenant groups
  13. #
  14. class TenantGroupForm(BootstrapMixin, forms.ModelForm):
  15. slug = SlugField()
  16. class Meta:
  17. model = TenantGroup
  18. fields = [
  19. 'name', 'slug',
  20. ]
  21. class TenantGroupCSVForm(forms.ModelForm):
  22. slug = SlugField()
  23. class Meta:
  24. model = TenantGroup
  25. fields = TenantGroup.csv_headers
  26. help_texts = {
  27. 'name': 'Group name',
  28. }
  29. #
  30. # Tenants
  31. #
  32. class TenantForm(BootstrapMixin, CustomFieldModelForm):
  33. slug = SlugField()
  34. comments = CommentField()
  35. tags = TagField(
  36. required=False
  37. )
  38. class Meta:
  39. model = Tenant
  40. fields = [
  41. 'name', 'slug', 'group', 'description', 'comments', 'tags',
  42. ]
  43. widgets = {
  44. 'group': APISelect(
  45. api_url="/api/tenancy/tenant-groups/"
  46. )
  47. }
  48. class TenantCSVForm(CustomFieldModelForm):
  49. slug = SlugField()
  50. group = forms.ModelChoiceField(
  51. queryset=TenantGroup.objects.all(),
  52. required=False,
  53. to_field_name='name',
  54. help_text='Name of parent group',
  55. error_messages={
  56. 'invalid_choice': 'Group not found.'
  57. }
  58. )
  59. class Meta:
  60. model = Tenant
  61. fields = Tenant.csv_headers
  62. help_texts = {
  63. 'name': 'Tenant name',
  64. 'comments': 'Free-form comments'
  65. }
  66. class TenantBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
  67. pk = forms.ModelMultipleChoiceField(
  68. queryset=Tenant.objects.all(),
  69. widget=forms.MultipleHiddenInput()
  70. )
  71. group = forms.ModelChoiceField(
  72. queryset=TenantGroup.objects.all(),
  73. required=False,
  74. widget=APISelect(
  75. api_url="/api/tenancy/tenant-groups/"
  76. )
  77. )
  78. class Meta:
  79. nullable_fields = [
  80. 'group',
  81. ]
  82. class TenantFilterForm(BootstrapMixin, CustomFieldFilterForm):
  83. model = Tenant
  84. q = forms.CharField(
  85. required=False,
  86. label='Search'
  87. )
  88. group = FilterChoiceField(
  89. queryset=TenantGroup.objects.all(),
  90. to_field_name='slug',
  91. null_label='-- None --',
  92. widget=APISelectMultiple(
  93. api_url="/api/tenancy/tenant-groups/",
  94. value_field="slug",
  95. null_option=True,
  96. )
  97. )
  98. tag = TagFilterField(model)
  99. #
  100. # Form extensions
  101. #
  102. class TenancyForm(ChainedFieldsMixin, forms.Form):
  103. tenant_group = forms.ModelChoiceField(
  104. queryset=TenantGroup.objects.all(),
  105. required=False,
  106. widget=APISelect(
  107. api_url="/api/tenancy/tenant-groups/",
  108. filter_for={
  109. 'tenant': 'group_id',
  110. },
  111. attrs={
  112. 'nullable': 'true',
  113. }
  114. )
  115. )
  116. tenant = ChainedModelChoiceField(
  117. queryset=Tenant.objects.all(),
  118. chains=(
  119. ('group', 'tenant_group'),
  120. ),
  121. required=False,
  122. widget=APISelect(
  123. api_url='/api/tenancy/tenants/'
  124. )
  125. )
  126. def __init__(self, *args, **kwargs):
  127. # Initialize helper selector
  128. instance = kwargs.get('instance')
  129. if instance and instance.tenant is not None:
  130. initial = kwargs.get('initial', {}).copy()
  131. initial['tenant_group'] = instance.tenant.group
  132. kwargs['initial'] = initial
  133. super().__init__(*args, **kwargs)
  134. class TenancyFilterForm(forms.Form):
  135. tenant_group = FilterChoiceField(
  136. queryset=TenantGroup.objects.all(),
  137. to_field_name='slug',
  138. null_label='-- None --',
  139. widget=APISelectMultiple(
  140. api_url="/api/tenancy/tenant-groups/",
  141. value_field="slug",
  142. null_option=True,
  143. filter_for={
  144. 'tenant': 'group'
  145. }
  146. )
  147. )
  148. tenant = FilterChoiceField(
  149. queryset=Tenant.objects.all(),
  150. to_field_name='slug',
  151. null_label='-- None --',
  152. widget=APISelectMultiple(
  153. api_url="/api/tenancy/tenants/",
  154. value_field="slug",
  155. null_option=True,
  156. )
  157. )