models.py 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251
  1. from django import forms
  2. from django.contrib.contenttypes.models import ContentType
  3. from dcim.models import DeviceRole, DeviceType, Location, Platform, Region, Site, SiteGroup
  4. from extras.choices import *
  5. from extras.models import *
  6. from extras.utils import FeatureQuery
  7. from netbox.forms import NetBoxModelForm
  8. from tenancy.models import Tenant, TenantGroup
  9. from utilities.forms import (
  10. add_blank_choice, BootstrapMixin, CommentField, ContentTypeChoiceField, ContentTypeMultipleChoiceField,
  11. DynamicModelMultipleChoiceField, JSONField, SlugField, StaticSelect,
  12. )
  13. from virtualization.models import Cluster, ClusterGroup, ClusterType
  14. __all__ = (
  15. 'ConfigContextForm',
  16. 'CustomFieldForm',
  17. 'CustomLinkForm',
  18. 'ExportTemplateForm',
  19. 'ImageAttachmentForm',
  20. 'JournalEntryForm',
  21. 'TagForm',
  22. 'WebhookForm',
  23. )
  24. class CustomFieldForm(BootstrapMixin, forms.ModelForm):
  25. content_types = ContentTypeMultipleChoiceField(
  26. queryset=ContentType.objects.all(),
  27. limit_choices_to=FeatureQuery('custom_fields'),
  28. label='Model(s)'
  29. )
  30. object_type = ContentTypeChoiceField(
  31. queryset=ContentType.objects.all(),
  32. # TODO: Come up with a canonical way to register suitable models
  33. limit_choices_to=FeatureQuery('webhooks'),
  34. required=False,
  35. help_text="Type of the related object (for object/multi-object fields only)"
  36. )
  37. fieldsets = (
  38. ('Custom Field', (
  39. 'content_types', 'name', 'label', 'group_name', 'type', 'object_type', 'weight', 'required', 'description',
  40. )),
  41. ('Behavior', ('filter_logic', 'ui_visibility')),
  42. ('Values', ('default', 'choices')),
  43. ('Validation', ('validation_minimum', 'validation_maximum', 'validation_regex')),
  44. )
  45. class Meta:
  46. model = CustomField
  47. fields = '__all__'
  48. help_texts = {
  49. 'type': "The type of data stored in this field. For object/multi-object fields, select the related object "
  50. "type below."
  51. }
  52. widgets = {
  53. 'type': StaticSelect(),
  54. 'filter_logic': StaticSelect(),
  55. 'ui_visibility': StaticSelect(),
  56. }
  57. class CustomLinkForm(BootstrapMixin, forms.ModelForm):
  58. content_type = ContentTypeChoiceField(
  59. queryset=ContentType.objects.all(),
  60. limit_choices_to=FeatureQuery('custom_links')
  61. )
  62. fieldsets = (
  63. ('Custom Link', ('name', 'content_type', 'weight', 'group_name', 'button_class', 'enabled', 'new_window')),
  64. ('Templates', ('link_text', 'link_url')),
  65. )
  66. class Meta:
  67. model = CustomLink
  68. fields = '__all__'
  69. widgets = {
  70. 'button_class': StaticSelect(),
  71. 'link_text': forms.Textarea(attrs={'class': 'font-monospace'}),
  72. 'link_url': forms.Textarea(attrs={'class': 'font-monospace'}),
  73. }
  74. help_texts = {
  75. 'link_text': 'Jinja2 template code for the link text. Reference the object as <code>{{ object }}</code>. '
  76. 'Links which render as empty text will not be displayed.',
  77. 'link_url': 'Jinja2 template code for the link URL. Reference the object as <code>{{ object }}</code>.',
  78. }
  79. class ExportTemplateForm(BootstrapMixin, forms.ModelForm):
  80. content_type = ContentTypeChoiceField(
  81. queryset=ContentType.objects.all(),
  82. limit_choices_to=FeatureQuery('export_templates')
  83. )
  84. fieldsets = (
  85. ('Export Template', ('name', 'content_type', 'description')),
  86. ('Template', ('template_code',)),
  87. ('Rendering', ('mime_type', 'file_extension', 'as_attachment')),
  88. )
  89. class Meta:
  90. model = ExportTemplate
  91. fields = '__all__'
  92. widgets = {
  93. 'template_code': forms.Textarea(attrs={'class': 'font-monospace'}),
  94. }
  95. class WebhookForm(BootstrapMixin, forms.ModelForm):
  96. content_types = ContentTypeMultipleChoiceField(
  97. queryset=ContentType.objects.all(),
  98. limit_choices_to=FeatureQuery('webhooks')
  99. )
  100. fieldsets = (
  101. ('Webhook', ('name', 'content_types', 'enabled')),
  102. ('Events', ('type_create', 'type_update', 'type_delete')),
  103. ('HTTP Request', (
  104. 'payload_url', 'http_method', 'http_content_type', 'additional_headers', 'body_template', 'secret',
  105. )),
  106. ('Conditions', ('conditions',)),
  107. ('SSL', ('ssl_verification', 'ca_file_path')),
  108. )
  109. class Meta:
  110. model = Webhook
  111. fields = '__all__'
  112. labels = {
  113. 'type_create': 'Creations',
  114. 'type_update': 'Updates',
  115. 'type_delete': 'Deletions',
  116. }
  117. widgets = {
  118. 'http_method': StaticSelect(),
  119. 'additional_headers': forms.Textarea(attrs={'class': 'font-monospace'}),
  120. 'body_template': forms.Textarea(attrs={'class': 'font-monospace'}),
  121. }
  122. class TagForm(BootstrapMixin, forms.ModelForm):
  123. slug = SlugField()
  124. fieldsets = (
  125. ('Tag', ('name', 'slug', 'color', 'description')),
  126. )
  127. class Meta:
  128. model = Tag
  129. fields = [
  130. 'name', 'slug', 'color', 'description'
  131. ]
  132. class ConfigContextForm(BootstrapMixin, forms.ModelForm):
  133. regions = DynamicModelMultipleChoiceField(
  134. queryset=Region.objects.all(),
  135. required=False
  136. )
  137. site_groups = DynamicModelMultipleChoiceField(
  138. queryset=SiteGroup.objects.all(),
  139. required=False
  140. )
  141. sites = DynamicModelMultipleChoiceField(
  142. queryset=Site.objects.all(),
  143. required=False
  144. )
  145. locations = DynamicModelMultipleChoiceField(
  146. queryset=Location.objects.all(),
  147. required=False
  148. )
  149. device_types = DynamicModelMultipleChoiceField(
  150. queryset=DeviceType.objects.all(),
  151. required=False
  152. )
  153. roles = DynamicModelMultipleChoiceField(
  154. queryset=DeviceRole.objects.all(),
  155. required=False
  156. )
  157. platforms = DynamicModelMultipleChoiceField(
  158. queryset=Platform.objects.all(),
  159. required=False
  160. )
  161. cluster_types = DynamicModelMultipleChoiceField(
  162. queryset=ClusterType.objects.all(),
  163. required=False
  164. )
  165. cluster_groups = DynamicModelMultipleChoiceField(
  166. queryset=ClusterGroup.objects.all(),
  167. required=False
  168. )
  169. clusters = DynamicModelMultipleChoiceField(
  170. queryset=Cluster.objects.all(),
  171. required=False
  172. )
  173. tenant_groups = DynamicModelMultipleChoiceField(
  174. queryset=TenantGroup.objects.all(),
  175. required=False
  176. )
  177. tenants = DynamicModelMultipleChoiceField(
  178. queryset=Tenant.objects.all(),
  179. required=False
  180. )
  181. tags = DynamicModelMultipleChoiceField(
  182. queryset=Tag.objects.all(),
  183. required=False
  184. )
  185. data = JSONField()
  186. fieldsets = (
  187. ('Config Context', ('name', 'weight', 'description', 'data', 'is_active')),
  188. ('Assignment', (
  189. 'regions', 'site_groups', 'sites', 'locations', 'device_types', 'roles', 'platforms', 'cluster_types',
  190. 'cluster_groups', 'clusters', 'tenant_groups', 'tenants', 'tags',
  191. )),
  192. )
  193. class Meta:
  194. model = ConfigContext
  195. fields = (
  196. 'name', 'weight', 'description', 'data', 'is_active', 'regions', 'site_groups', 'sites', 'locations',
  197. 'roles', 'device_types', 'platforms', 'cluster_types', 'cluster_groups', 'clusters', 'tenant_groups',
  198. 'tenants', 'tags',
  199. )
  200. class ImageAttachmentForm(BootstrapMixin, forms.ModelForm):
  201. class Meta:
  202. model = ImageAttachment
  203. fields = [
  204. 'name', 'image',
  205. ]
  206. class JournalEntryForm(NetBoxModelForm):
  207. kind = forms.ChoiceField(
  208. choices=add_blank_choice(JournalEntryKindChoices),
  209. required=False,
  210. widget=StaticSelect()
  211. )
  212. comments = CommentField()
  213. class Meta:
  214. model = JournalEntry
  215. fields = ['assigned_object_type', 'assigned_object_id', 'kind', 'tags', 'comments']
  216. widgets = {
  217. 'assigned_object_type': forms.HiddenInput,
  218. 'assigned_object_id': forms.HiddenInput,
  219. }