forms.py 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173
  1. from django import forms
  2. from django.utils.translation import gettext as _
  3. from extras.forms import (
  4. AddRemoveTagsForm, CustomFieldModelForm, CustomFieldBulkEditForm, CustomFieldFilterForm, CustomFieldModelCSVForm,
  5. )
  6. from extras.models import Tag
  7. from utilities.forms import (
  8. BootstrapMixin, CommentField, CSVModelChoiceField, DynamicModelChoiceField, DynamicModelMultipleChoiceField,
  9. SlugField, TagFilterField,
  10. )
  11. from .models import Tenant, TenantGroup
  12. #
  13. # Tenant groups
  14. #
  15. class TenantGroupForm(BootstrapMixin, CustomFieldModelForm):
  16. parent = DynamicModelChoiceField(
  17. queryset=TenantGroup.objects.all(),
  18. required=False
  19. )
  20. slug = SlugField()
  21. class Meta:
  22. model = TenantGroup
  23. fields = [
  24. 'parent', 'name', 'slug', 'description',
  25. ]
  26. class TenantGroupCSVForm(CustomFieldModelCSVForm):
  27. parent = CSVModelChoiceField(
  28. queryset=TenantGroup.objects.all(),
  29. required=False,
  30. to_field_name='name',
  31. help_text='Parent group'
  32. )
  33. slug = SlugField()
  34. class Meta:
  35. model = TenantGroup
  36. fields = ('name', 'slug', 'parent', 'description')
  37. class TenantGroupBulkEditForm(BootstrapMixin, CustomFieldBulkEditForm):
  38. pk = forms.ModelMultipleChoiceField(
  39. queryset=TenantGroup.objects.all(),
  40. widget=forms.MultipleHiddenInput
  41. )
  42. parent = DynamicModelChoiceField(
  43. queryset=TenantGroup.objects.all(),
  44. required=False
  45. )
  46. description = forms.CharField(
  47. max_length=200,
  48. required=False
  49. )
  50. class Meta:
  51. nullable_fields = ['parent', 'description']
  52. #
  53. # Tenants
  54. #
  55. class TenantForm(BootstrapMixin, CustomFieldModelForm):
  56. slug = SlugField()
  57. group = DynamicModelChoiceField(
  58. queryset=TenantGroup.objects.all(),
  59. required=False
  60. )
  61. comments = CommentField()
  62. tags = DynamicModelMultipleChoiceField(
  63. queryset=Tag.objects.all(),
  64. required=False
  65. )
  66. class Meta:
  67. model = Tenant
  68. fields = (
  69. 'name', 'slug', 'group', 'description', 'comments', 'tags',
  70. )
  71. fieldsets = (
  72. ('Tenant', ('name', 'slug', 'group', 'description', 'tags')),
  73. )
  74. class TenantCSVForm(CustomFieldModelCSVForm):
  75. slug = SlugField()
  76. group = CSVModelChoiceField(
  77. queryset=TenantGroup.objects.all(),
  78. required=False,
  79. to_field_name='name',
  80. help_text='Assigned group'
  81. )
  82. class Meta:
  83. model = Tenant
  84. fields = ('name', 'slug', 'group', 'description', 'comments')
  85. class TenantBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
  86. pk = forms.ModelMultipleChoiceField(
  87. queryset=Tenant.objects.all(),
  88. widget=forms.MultipleHiddenInput()
  89. )
  90. group = DynamicModelChoiceField(
  91. queryset=TenantGroup.objects.all(),
  92. required=False
  93. )
  94. class Meta:
  95. nullable_fields = [
  96. 'group',
  97. ]
  98. class TenantFilterForm(BootstrapMixin, CustomFieldFilterForm):
  99. model = Tenant
  100. q = forms.CharField(
  101. required=False,
  102. label=_('Search')
  103. )
  104. group_id = DynamicModelMultipleChoiceField(
  105. queryset=TenantGroup.objects.all(),
  106. required=False,
  107. null_option='None',
  108. label=_('Group')
  109. )
  110. tag = TagFilterField(model)
  111. #
  112. # Form extensions
  113. #
  114. class TenancyForm(forms.Form):
  115. tenant_group = DynamicModelChoiceField(
  116. queryset=TenantGroup.objects.all(),
  117. required=False,
  118. null_option='None',
  119. initial_params={
  120. 'tenants': '$tenant'
  121. }
  122. )
  123. tenant = DynamicModelChoiceField(
  124. queryset=Tenant.objects.all(),
  125. required=False,
  126. query_params={
  127. 'group_id': '$tenant_group'
  128. }
  129. )
  130. class TenancyFilterForm(forms.Form):
  131. tenant_group_id = DynamicModelMultipleChoiceField(
  132. queryset=TenantGroup.objects.all(),
  133. required=False,
  134. null_option='None',
  135. label=_('Tenant group')
  136. )
  137. tenant_id = DynamicModelMultipleChoiceField(
  138. queryset=Tenant.objects.all(),
  139. required=False,
  140. null_option='None',
  141. query_params={
  142. 'group_id': '$tenant_group_id'
  143. },
  144. label=_('Tenant')
  145. )