forms.py 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150
  1. from django import forms
  2. from django.db.models import Count
  3. from taggit.forms import TagField
  4. from extras.forms import AddRemoveTagsForm, CustomFieldForm, CustomFieldBulkEditForm, CustomFieldFilterForm
  5. from utilities.forms import (
  6. APISelect, BootstrapMixin, ChainedFieldsMixin, ChainedModelChoiceField, CommentField, 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. )
  73. class Meta:
  74. nullable_fields = [
  75. 'group',
  76. ]
  77. class TenantFilterForm(BootstrapMixin, CustomFieldFilterForm):
  78. model = Tenant
  79. q = forms.CharField(
  80. required=False,
  81. label='Search'
  82. )
  83. group = FilterChoiceField(
  84. queryset=TenantGroup.objects.annotate(
  85. filter_count=Count('tenants')
  86. ),
  87. to_field_name='slug',
  88. null_label='-- None --'
  89. )
  90. #
  91. # Tenancy form extension
  92. #
  93. class TenancyForm(ChainedFieldsMixin, forms.Form):
  94. tenant_group = forms.ModelChoiceField(
  95. queryset=TenantGroup.objects.all(),
  96. required=False,
  97. widget=APISelect(
  98. api_url="/api/tenancy/tenant-groups/",
  99. filter_for={
  100. 'tenant': 'group_id',
  101. },
  102. attrs={
  103. 'nullable': 'true',
  104. }
  105. )
  106. )
  107. tenant = ChainedModelChoiceField(
  108. queryset=Tenant.objects.all(),
  109. chains=(
  110. ('group', 'tenant_group'),
  111. ),
  112. required=False,
  113. widget=APISelect(
  114. api_url='/api/tenancy/tenants/'
  115. )
  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)