forms.py 4.3 KB

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