bulk_import.py 7.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230
  1. from django import forms
  2. from django.contrib.postgres.forms import SimpleArrayField
  3. from django.core.exceptions import ObjectDoesNotExist
  4. from django.utils.safestring import mark_safe
  5. from django.utils.translation import gettext_lazy as _
  6. from core.models import ContentType
  7. from extras.choices import *
  8. from extras.models import *
  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. 'EventRuleImportForm',
  20. 'ExportTemplateImportForm',
  21. 'JournalEntryImportForm',
  22. 'SavedFilterImportForm',
  23. 'TagImportForm',
  24. 'WebhookImportForm',
  25. )
  26. class CustomFieldImportForm(CSVModelForm):
  27. content_types = CSVMultipleContentTypeField(
  28. label=_('Content types'),
  29. queryset=ContentType.objects.with_feature('custom_fields'),
  30. help_text=_("One or more assigned object types")
  31. )
  32. type = CSVChoiceField(
  33. label=_('Type'),
  34. choices=CustomFieldTypeChoices,
  35. help_text=_('Field data type (e.g. text, integer, etc.)')
  36. )
  37. object_type = CSVContentTypeField(
  38. label=_('Object type'),
  39. queryset=ContentType.objects.public(),
  40. required=False,
  41. help_text=_("Object type (for object or multi-object fields)")
  42. )
  43. choice_set = CSVModelChoiceField(
  44. label=_('Choice set'),
  45. queryset=CustomFieldChoiceSet.objects.all(),
  46. to_field_name='name',
  47. required=False,
  48. help_text=_('Choice set (for selection fields)')
  49. )
  50. ui_visible = CSVChoiceField(
  51. label=_('UI visible'),
  52. choices=CustomFieldUIVisibleChoices,
  53. required=False,
  54. help_text=_('Whether the custom field is displayed in the UI')
  55. )
  56. ui_editable = CSVChoiceField(
  57. label=_('UI editable'),
  58. choices=CustomFieldUIEditableChoices,
  59. required=False,
  60. help_text=_('Whether the custom field is editable in the UI')
  61. )
  62. class Meta:
  63. model = CustomField
  64. fields = (
  65. 'name', 'label', 'group_name', 'type', 'content_types', 'object_type', 'required', 'description',
  66. 'search_weight', 'filter_logic', 'default', 'choice_set', 'weight', 'validation_minimum',
  67. 'validation_maximum', 'validation_regex', 'ui_visible', 'ui_editable', 'is_cloneable',
  68. )
  69. class CustomFieldChoiceSetImportForm(CSVModelForm):
  70. base_choices = CSVChoiceField(
  71. choices=CustomFieldChoiceSetBaseChoices,
  72. required=False,
  73. help_text=_('The base set of predefined choices to use (if any)')
  74. )
  75. extra_choices = SimpleArrayField(
  76. base_field=forms.CharField(),
  77. required=False,
  78. help_text=_('Comma-separated list of field choices')
  79. )
  80. class Meta:
  81. model = CustomFieldChoiceSet
  82. fields = (
  83. 'name', 'description', 'extra_choices', 'order_alphabetically',
  84. )
  85. class CustomLinkImportForm(CSVModelForm):
  86. content_types = CSVMultipleContentTypeField(
  87. label=_('Content types'),
  88. queryset=ContentType.objects.with_feature('custom_links'),
  89. help_text=_("One or more assigned object types")
  90. )
  91. class Meta:
  92. model = CustomLink
  93. fields = (
  94. 'name', 'content_types', 'enabled', 'weight', 'group_name', 'button_class', 'new_window', 'link_text',
  95. 'link_url',
  96. )
  97. class ExportTemplateImportForm(CSVModelForm):
  98. content_types = CSVMultipleContentTypeField(
  99. label=_('Content types'),
  100. queryset=ContentType.objects.with_feature('export_templates'),
  101. help_text=_("One or more assigned object types")
  102. )
  103. class Meta:
  104. model = ExportTemplate
  105. fields = (
  106. 'name', 'content_types', 'description', 'mime_type', 'file_extension', 'as_attachment', 'template_code',
  107. )
  108. class ConfigTemplateImportForm(CSVModelForm):
  109. class Meta:
  110. model = ConfigTemplate
  111. fields = (
  112. 'name', 'description', 'environment_params', 'template_code', 'tags',
  113. )
  114. class SavedFilterImportForm(CSVModelForm):
  115. content_types = CSVMultipleContentTypeField(
  116. label=_('Content types'),
  117. queryset=ContentType.objects.all(),
  118. help_text=_("One or more assigned object types")
  119. )
  120. class Meta:
  121. model = SavedFilter
  122. fields = (
  123. 'name', 'slug', 'content_types', 'description', 'weight', 'enabled', 'shared', 'parameters',
  124. )
  125. class WebhookImportForm(NetBoxModelImportForm):
  126. class Meta:
  127. model = Webhook
  128. fields = (
  129. 'name', 'payload_url', 'http_method', 'http_content_type', 'additional_headers', 'body_template',
  130. 'secret', 'ssl_verification', 'ca_file_path', 'description', 'tags'
  131. )
  132. class EventRuleImportForm(NetBoxModelImportForm):
  133. content_types = CSVMultipleContentTypeField(
  134. label=_('Content types'),
  135. queryset=ContentType.objects.with_feature('event_rules'),
  136. help_text=_("One or more assigned object types")
  137. )
  138. action_object = forms.CharField(
  139. label=_('Action object'),
  140. required=True,
  141. help_text=_('Webhook name or script as dotted path module.Class')
  142. )
  143. class Meta:
  144. model = EventRule
  145. fields = (
  146. 'name', 'description', 'enabled', 'conditions', 'content_types', 'type_create', 'type_update',
  147. 'type_delete', 'type_job_start', 'type_job_end', 'action_type', 'action_object', 'comments', 'tags'
  148. )
  149. def clean(self):
  150. super().clean()
  151. action_object = self.cleaned_data.get('action_object')
  152. action_type = self.cleaned_data.get('action_type')
  153. if action_object and action_type:
  154. # Webhook
  155. if action_type == EventRuleActionChoices.WEBHOOK:
  156. try:
  157. webhook = Webhook.objects.get(name=action_object)
  158. except Webhook.DoesNotExist:
  159. raise forms.ValidationError(f"Webhook {action_object} not found")
  160. self.instance.action_object = webhook
  161. # Script
  162. elif action_type == EventRuleActionChoices.SCRIPT:
  163. from extras.scripts import get_module_and_script
  164. module_name, script_name = action_object.split('.', 1)
  165. try:
  166. module, script = get_module_and_script(module_name, script_name)
  167. except ObjectDoesNotExist:
  168. raise forms.ValidationError(f"Script {action_object} not found")
  169. self.instance.action_object = module
  170. self.instance.action_object_type = ContentType.objects.get_for_model(module, for_concrete_model=False)
  171. self.instance.action_parameters = {
  172. 'script_name': script_name,
  173. }
  174. class TagImportForm(CSVModelForm):
  175. slug = SlugField()
  176. class Meta:
  177. model = Tag
  178. fields = ('name', 'slug', 'color', 'description')
  179. help_texts = {
  180. 'color': mark_safe(_('RGB color in hexadecimal. Example:') + ' <code>00ff00</code>'),
  181. }
  182. class JournalEntryImportForm(NetBoxModelImportForm):
  183. assigned_object_type = CSVContentTypeField(
  184. queryset=ContentType.objects.all(),
  185. label=_('Assigned object type'),
  186. )
  187. kind = CSVChoiceField(
  188. label=_('Kind'),
  189. choices=JournalEntryKindChoices,
  190. help_text=_('The classification of entry')
  191. )
  192. class Meta:
  193. model = JournalEntry
  194. fields = (
  195. 'assigned_object_type', 'assigned_object_id', 'created_by', 'kind', 'comments', 'tags'
  196. )