bulk_import.py 5.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171
  1. from django import forms
  2. from django.contrib.contenttypes.models import ContentType
  3. from django.contrib.postgres.forms import SimpleArrayField
  4. from django.utils.safestring import mark_safe
  5. from django.utils.translation import gettext as _
  6. from extras.choices import CustomFieldVisibilityChoices, CustomFieldTypeChoices, JournalEntryKindChoices
  7. from extras.models import *
  8. from extras.utils import FeatureQuery
  9. from netbox.forms import NetBoxModelImportForm
  10. from utilities.forms import CSVModelForm
  11. from utilities.forms.fields import (
  12. CSVChoiceField, CSVContentTypeField, CSVModelChoiceField, CSVMultipleContentTypeField, SlugField,
  13. )
  14. __all__ = (
  15. 'ConfigTemplateImportForm',
  16. 'CustomFieldChoiceSetImportForm',
  17. 'CustomFieldImportForm',
  18. 'CustomLinkImportForm',
  19. 'ExportTemplateImportForm',
  20. 'JournalEntryImportForm',
  21. 'SavedFilterImportForm',
  22. 'TagImportForm',
  23. 'WebhookImportForm',
  24. )
  25. class CustomFieldImportForm(CSVModelForm):
  26. content_types = CSVMultipleContentTypeField(
  27. queryset=ContentType.objects.all(),
  28. limit_choices_to=FeatureQuery('custom_fields'),
  29. help_text=_("One or more assigned object types")
  30. )
  31. type = CSVChoiceField(
  32. choices=CustomFieldTypeChoices,
  33. help_text=_('Field data type (e.g. text, integer, etc.)')
  34. )
  35. object_type = CSVContentTypeField(
  36. queryset=ContentType.objects.all(),
  37. limit_choices_to=FeatureQuery('custom_fields'),
  38. required=False,
  39. help_text=_("Object type (for object or multi-object fields)")
  40. )
  41. choice_set = CSVModelChoiceField(
  42. queryset=CustomFieldChoiceSet.objects.all(),
  43. to_field_name='name',
  44. required=False,
  45. help_text=_('Choice set (for selection fields)')
  46. )
  47. ui_visibility = CSVChoiceField(
  48. choices=CustomFieldVisibilityChoices,
  49. help_text=_('How the custom field is displayed in the user interface')
  50. )
  51. class Meta:
  52. model = CustomField
  53. fields = (
  54. 'name', 'label', 'group_name', 'type', 'content_types', 'object_type', 'required', 'description',
  55. 'search_weight', 'filter_logic', 'default', 'choice_set', 'weight', 'validation_minimum',
  56. 'validation_maximum', 'validation_regex', 'ui_visibility', 'is_cloneable',
  57. )
  58. class CustomFieldChoiceSetImportForm(CSVModelForm):
  59. extra_choices = SimpleArrayField(
  60. base_field=forms.CharField(),
  61. required=False,
  62. help_text=_('Comma-separated list of field choices')
  63. )
  64. class Meta:
  65. model = CustomFieldChoiceSet
  66. fields = (
  67. 'name', 'description', 'extra_choices', 'order_alphabetically',
  68. )
  69. class CustomLinkImportForm(CSVModelForm):
  70. content_types = CSVMultipleContentTypeField(
  71. queryset=ContentType.objects.all(),
  72. limit_choices_to=FeatureQuery('custom_links'),
  73. help_text=_("One or more assigned object types")
  74. )
  75. class Meta:
  76. model = CustomLink
  77. fields = (
  78. 'name', 'content_types', 'enabled', 'weight', 'group_name', 'button_class', 'new_window', 'link_text',
  79. 'link_url',
  80. )
  81. class ExportTemplateImportForm(CSVModelForm):
  82. content_types = CSVMultipleContentTypeField(
  83. queryset=ContentType.objects.all(),
  84. limit_choices_to=FeatureQuery('export_templates'),
  85. help_text=_("One or more assigned object types")
  86. )
  87. class Meta:
  88. model = ExportTemplate
  89. fields = (
  90. 'name', 'content_types', 'description', 'mime_type', 'file_extension', 'as_attachment', 'template_code',
  91. )
  92. class ConfigTemplateImportForm(CSVModelForm):
  93. class Meta:
  94. model = ConfigTemplate
  95. fields = (
  96. 'name', 'description', 'environment_params', 'template_code', 'tags',
  97. )
  98. class SavedFilterImportForm(CSVModelForm):
  99. content_types = CSVMultipleContentTypeField(
  100. queryset=ContentType.objects.all(),
  101. help_text=_("One or more assigned object types")
  102. )
  103. class Meta:
  104. model = SavedFilter
  105. fields = (
  106. 'name', 'slug', 'content_types', 'description', 'weight', 'enabled', 'shared', 'parameters',
  107. )
  108. class WebhookImportForm(CSVModelForm):
  109. content_types = CSVMultipleContentTypeField(
  110. queryset=ContentType.objects.all(),
  111. limit_choices_to=FeatureQuery('webhooks'),
  112. help_text=_("One or more assigned object types")
  113. )
  114. class Meta:
  115. model = Webhook
  116. fields = (
  117. 'name', 'enabled', 'content_types', 'type_create', 'type_update', 'type_delete', 'type_job_start',
  118. 'type_job_end', 'payload_url', 'http_method', 'http_content_type', 'additional_headers', 'body_template',
  119. 'secret', 'ssl_verification', 'ca_file_path'
  120. )
  121. class TagImportForm(CSVModelForm):
  122. slug = SlugField()
  123. class Meta:
  124. model = Tag
  125. fields = ('name', 'slug', 'color', 'description')
  126. help_texts = {
  127. 'color': mark_safe(_('RGB color in hexadecimal (e.g. <code>00ff00</code>)')),
  128. }
  129. class JournalEntryImportForm(NetBoxModelImportForm):
  130. assigned_object_type = CSVContentTypeField(
  131. queryset=ContentType.objects.all(),
  132. label=_('Assigned object type'),
  133. )
  134. kind = CSVChoiceField(
  135. choices=JournalEntryKindChoices,
  136. help_text=_('The classification of entry')
  137. )
  138. class Meta:
  139. model = JournalEntry
  140. fields = (
  141. 'assigned_object_type', 'assigned_object_id', 'created_by', 'kind', 'comments', 'tags'
  142. )