forms.py 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142
  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. class TenantCSVForm(forms.ModelForm):
  42. slug = SlugField()
  43. group = forms.ModelChoiceField(
  44. queryset=TenantGroup.objects.all(),
  45. required=False,
  46. to_field_name='name',
  47. help_text='Name of parent group',
  48. error_messages={
  49. 'invalid_choice': 'Group not found.'
  50. }
  51. )
  52. class Meta:
  53. model = Tenant
  54. fields = Tenant.csv_headers
  55. help_texts = {
  56. 'name': 'Tenant name',
  57. 'comments': 'Free-form comments'
  58. }
  59. class TenantBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
  60. pk = forms.ModelMultipleChoiceField(
  61. queryset=Tenant.objects.all(),
  62. widget=forms.MultipleHiddenInput()
  63. )
  64. group = forms.ModelChoiceField(
  65. queryset=TenantGroup.objects.all(),
  66. required=False
  67. )
  68. class Meta:
  69. nullable_fields = [
  70. 'group',
  71. ]
  72. class TenantFilterForm(BootstrapMixin, CustomFieldFilterForm):
  73. model = Tenant
  74. q = forms.CharField(
  75. required=False,
  76. label='Search'
  77. )
  78. group = FilterChoiceField(
  79. queryset=TenantGroup.objects.annotate(
  80. filter_count=Count('tenants')
  81. ),
  82. to_field_name='slug',
  83. null_label='-- None --'
  84. )
  85. #
  86. # Tenancy form extension
  87. #
  88. class TenancyForm(ChainedFieldsMixin, forms.Form):
  89. tenant_group = forms.ModelChoiceField(
  90. queryset=TenantGroup.objects.all(),
  91. required=False,
  92. widget=forms.Select(
  93. attrs={
  94. 'filter-for': 'tenant',
  95. 'nullable': 'true',
  96. }
  97. )
  98. )
  99. tenant = ChainedModelChoiceField(
  100. queryset=Tenant.objects.all(),
  101. chains=(
  102. ('group', 'tenant_group'),
  103. ),
  104. required=False,
  105. widget=APISelect(
  106. api_url='/api/tenancy/tenants/?group_id={{tenant_group}}'
  107. )
  108. )
  109. def __init__(self, *args, **kwargs):
  110. # Initialize helper selector
  111. instance = kwargs.get('instance')
  112. if instance and instance.tenant is not None:
  113. initial = kwargs.get('initial', {}).copy()
  114. initial['tenant_group'] = instance.tenant.group
  115. kwargs['initial'] = initial
  116. super().__init__(*args, **kwargs)