model_forms.py 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152
  1. from django import forms
  2. from django.utils.translation import gettext_lazy as _
  3. from netbox.forms import NestedGroupModelForm, NetBoxModelForm, OrganizationalModelForm, PrimaryModelForm
  4. from tenancy.models import *
  5. from utilities.forms.fields import DynamicModelChoiceField, DynamicModelMultipleChoiceField, SlugField
  6. from utilities.forms.rendering import FieldSet, ObjectAttribute
  7. __all__ = (
  8. 'ContactAssignmentForm',
  9. 'ContactForm',
  10. 'ContactGroupForm',
  11. 'ContactRoleForm',
  12. 'TenantForm',
  13. 'TenantGroupForm',
  14. )
  15. #
  16. # Tenants
  17. #
  18. class TenantGroupForm(NestedGroupModelForm):
  19. parent = DynamicModelChoiceField(
  20. label=_('Parent'),
  21. queryset=TenantGroup.objects.all(),
  22. required=False
  23. )
  24. fieldsets = (
  25. FieldSet('parent', 'name', 'slug', 'description', 'tags', name=_('Tenant Group')),
  26. )
  27. class Meta:
  28. model = TenantGroup
  29. fields = [
  30. 'parent', 'name', 'slug', 'description', 'owner', 'comments', 'tags',
  31. ]
  32. class TenantForm(PrimaryModelForm):
  33. slug = SlugField()
  34. group = DynamicModelChoiceField(
  35. label=_('Group'),
  36. queryset=TenantGroup.objects.all(),
  37. required=False
  38. )
  39. fieldsets = (
  40. FieldSet('name', 'slug', 'group', 'description', 'tags', name=_('Tenant')),
  41. )
  42. class Meta:
  43. model = Tenant
  44. fields = (
  45. 'name', 'slug', 'group', 'description', 'owner', 'comments', 'tags',
  46. )
  47. #
  48. # Contacts
  49. #
  50. class ContactGroupForm(NestedGroupModelForm):
  51. parent = DynamicModelChoiceField(
  52. label=_('Parent'),
  53. queryset=ContactGroup.objects.all(),
  54. required=False
  55. )
  56. fieldsets = (
  57. FieldSet('parent', 'name', 'slug', 'description', 'tags', name=_('Contact Group')),
  58. )
  59. class Meta:
  60. model = ContactGroup
  61. fields = ('parent', 'name', 'slug', 'description', 'owner', 'comments', 'tags')
  62. class ContactRoleForm(OrganizationalModelForm):
  63. fieldsets = (
  64. FieldSet('name', 'slug', 'description', 'tags', name=_('Contact Role')),
  65. )
  66. class Meta:
  67. model = ContactRole
  68. fields = ('name', 'slug', 'description', 'owner', 'comments', 'tags')
  69. class ContactForm(PrimaryModelForm):
  70. groups = DynamicModelMultipleChoiceField(
  71. label=_('Groups'),
  72. queryset=ContactGroup.objects.all(),
  73. required=False
  74. )
  75. link = forms.URLField(
  76. label=_('Link'),
  77. assume_scheme='https',
  78. required=False,
  79. )
  80. fieldsets = (
  81. FieldSet(
  82. 'groups', 'name', 'title', 'phone', 'email', 'address', 'link', 'description', 'tags',
  83. name=_('Contact')
  84. ),
  85. )
  86. class Meta:
  87. model = Contact
  88. fields = (
  89. 'groups', 'name', 'title', 'phone', 'email', 'address', 'link', 'description', 'owner', 'comments', 'tags',
  90. )
  91. widgets = {
  92. 'address': forms.Textarea(attrs={'rows': 3}),
  93. }
  94. class ContactAssignmentForm(NetBoxModelForm):
  95. group = DynamicModelChoiceField(
  96. label=_('Group'),
  97. queryset=ContactGroup.objects.all(),
  98. required=False,
  99. initial_params={
  100. 'contact': '$contact'
  101. }
  102. )
  103. contact = DynamicModelChoiceField(
  104. label=_('Contact'),
  105. queryset=Contact.objects.all(),
  106. query_params={
  107. 'group_id': '$group'
  108. }
  109. )
  110. role = DynamicModelChoiceField(
  111. label=_('Role'),
  112. queryset=ContactRole.objects.all()
  113. )
  114. fieldsets = (
  115. FieldSet(ObjectAttribute('object'), 'group', 'contact', 'role', 'priority', 'tags'),
  116. )
  117. class Meta:
  118. model = ContactAssignment
  119. fields = (
  120. 'object_type', 'object_id', 'group', 'contact', 'role', 'priority', 'tags'
  121. )
  122. widgets = {
  123. 'object_type': forms.HiddenInput(),
  124. 'object_id': forms.HiddenInput(),
  125. }