bulk_import.py 5.0 KB

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