models.py 7.5 KB

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