forms.py 4.4 KB

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