forms.py 2.9 KB

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