forms.py 4.2 KB

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