model_forms.py 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153
  1. from django import forms
  2. from django.utils.translation import gettext_lazy as _
  3. from netbox.forms import NetBoxModelForm
  4. from tenancy.models import *
  5. from utilities.forms.fields import CommentField, DynamicModelChoiceField, 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(NetBoxModelForm):
  19. parent = DynamicModelChoiceField(
  20. label=_('Parent'),
  21. queryset=TenantGroup.objects.all(),
  22. required=False
  23. )
  24. slug = SlugField()
  25. fieldsets = (
  26. FieldSet('parent', 'name', 'slug', 'description', 'tags', name=_('Tenant Group')),
  27. )
  28. class Meta:
  29. model = TenantGroup
  30. fields = [
  31. 'parent', 'name', 'slug', 'description', 'tags',
  32. ]
  33. class TenantForm(NetBoxModelForm):
  34. slug = SlugField()
  35. group = DynamicModelChoiceField(
  36. label=_('Group'),
  37. queryset=TenantGroup.objects.all(),
  38. required=False
  39. )
  40. comments = CommentField()
  41. fieldsets = (
  42. FieldSet('name', 'slug', 'group', 'description', 'tags', name=_('Tenant')),
  43. )
  44. class Meta:
  45. model = Tenant
  46. fields = (
  47. 'name', 'slug', 'group', 'description', 'comments', 'tags',
  48. )
  49. #
  50. # Contacts
  51. #
  52. class ContactGroupForm(NetBoxModelForm):
  53. parent = DynamicModelChoiceField(
  54. label=_('Parent'),
  55. queryset=ContactGroup.objects.all(),
  56. required=False
  57. )
  58. slug = SlugField()
  59. fieldsets = (
  60. FieldSet('parent', 'name', 'slug', 'description', 'tags', name=_('Contact Group')),
  61. )
  62. class Meta:
  63. model = ContactGroup
  64. fields = ('parent', 'name', 'slug', 'description', 'tags')
  65. class ContactRoleForm(NetBoxModelForm):
  66. slug = SlugField()
  67. fieldsets = (
  68. FieldSet('name', 'slug', 'description', 'tags', name=_('Contact Role')),
  69. )
  70. class Meta:
  71. model = ContactRole
  72. fields = ('name', 'slug', 'description', 'tags')
  73. class ContactForm(NetBoxModelForm):
  74. group = DynamicModelChoiceField(
  75. label=_('Group'),
  76. queryset=ContactGroup.objects.all(),
  77. required=False
  78. )
  79. comments = CommentField()
  80. fieldsets = (
  81. FieldSet(
  82. 'group', 'name', 'title', 'phone', 'email', 'address', 'link', 'description', 'tags',
  83. name=_('Contact')
  84. ),
  85. )
  86. class Meta:
  87. model = Contact
  88. fields = (
  89. 'group', 'name', 'title', 'phone', 'email', 'address', 'link', 'description', '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. 'contacts': '$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. }