forms.py 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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 = ['name', 'slug']
  17. class TenantGroupCSVForm(forms.ModelForm):
  18. slug = SlugField()
  19. class Meta:
  20. model = TenantGroup
  21. fields = TenantGroup.csv_headers
  22. help_texts = {
  23. 'name': 'Group name',
  24. }
  25. #
  26. # Tenants
  27. #
  28. class TenantForm(BootstrapMixin, CustomFieldForm):
  29. slug = SlugField()
  30. comments = CommentField()
  31. tags = TagField(required=False)
  32. class Meta:
  33. model = Tenant
  34. fields = ['name', 'slug', 'group', 'description', 'comments', 'tags']
  35. class TenantCSVForm(forms.ModelForm):
  36. slug = SlugField()
  37. group = forms.ModelChoiceField(
  38. queryset=TenantGroup.objects.all(),
  39. required=False,
  40. to_field_name='name',
  41. help_text='Name of parent group',
  42. error_messages={
  43. 'invalid_choice': 'Group not found.'
  44. }
  45. )
  46. class Meta:
  47. model = Tenant
  48. fields = Tenant.csv_headers
  49. help_texts = {
  50. 'name': 'Tenant name',
  51. 'comments': 'Free-form comments'
  52. }
  53. class TenantBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
  54. pk = forms.ModelMultipleChoiceField(queryset=Tenant.objects.all(), widget=forms.MultipleHiddenInput)
  55. group = forms.ModelChoiceField(queryset=TenantGroup.objects.all(), required=False)
  56. class Meta:
  57. nullable_fields = ['group']
  58. class TenantFilterForm(BootstrapMixin, CustomFieldFilterForm):
  59. model = Tenant
  60. q = forms.CharField(required=False, label='Search')
  61. group = FilterChoiceField(
  62. queryset=TenantGroup.objects.annotate(filter_count=Count('tenants')),
  63. to_field_name='slug',
  64. null_label='-- None --'
  65. )
  66. #
  67. # Tenancy form extension
  68. #
  69. class TenancyForm(ChainedFieldsMixin, forms.Form):
  70. tenant_group = forms.ModelChoiceField(
  71. queryset=TenantGroup.objects.all(),
  72. required=False,
  73. widget=forms.Select(
  74. attrs={'filter-for': 'tenant', 'nullable': 'true'}
  75. )
  76. )
  77. tenant = ChainedModelChoiceField(
  78. queryset=Tenant.objects.all(),
  79. chains=(
  80. ('group', 'tenant_group'),
  81. ),
  82. required=False,
  83. widget=APISelect(
  84. api_url='/api/tenancy/tenants/?group_id={{tenant_group}}'
  85. )
  86. )
  87. def __init__(self, *args, **kwargs):
  88. # Initialize helper selector
  89. instance = kwargs.get('instance')
  90. if instance and instance.tenant is not None:
  91. initial = kwargs.get('initial', {}).copy()
  92. initial['tenant_group'] = instance.tenant.group
  93. kwargs['initial'] = initial
  94. super(TenancyForm, self).__init__(*args, **kwargs)