import re from django import forms from django.contrib.postgres.forms import SimpleArrayField from django.core.exceptions import ObjectDoesNotExist from django.utils.safestring import mark_safe from django.utils.translation import gettext_lazy as _ from core.models import ObjectType from extras.choices import * from extras.models import * from netbox.forms import NetBoxModelImportForm from utilities.forms import CSVModelForm from utilities.forms.fields import ( CSVChoiceField, CSVContentTypeField, CSVModelChoiceField, CSVMultipleContentTypeField, SlugField, ) __all__ = ( 'ConfigTemplateImportForm', 'CustomFieldChoiceSetImportForm', 'CustomFieldImportForm', 'CustomLinkImportForm', 'EventRuleImportForm', 'ExportTemplateImportForm', 'JournalEntryImportForm', 'SavedFilterImportForm', 'TagImportForm', 'WebhookImportForm', ) class CustomFieldImportForm(CSVModelForm): object_types = CSVMultipleContentTypeField( label=_('Object types'), queryset=ObjectType.objects.with_feature('custom_fields'), help_text=_("One or more assigned object types") ) type = CSVChoiceField( label=_('Type'), choices=CustomFieldTypeChoices, help_text=_('Field data type (e.g. text, integer, etc.)') ) related_object_type = CSVContentTypeField( label=_('Object type'), queryset=ObjectType.objects.public(), required=False, help_text=_("Object type (for object or multi-object fields)") ) choice_set = CSVModelChoiceField( label=_('Choice set'), queryset=CustomFieldChoiceSet.objects.all(), to_field_name='name', required=False, help_text=_('Choice set (for selection fields)') ) ui_visible = CSVChoiceField( label=_('UI visible'), choices=CustomFieldUIVisibleChoices, required=False, help_text=_('Whether the custom field is displayed in the UI') ) ui_editable = CSVChoiceField( label=_('UI editable'), choices=CustomFieldUIEditableChoices, required=False, help_text=_('Whether the custom field is editable in the UI') ) class Meta: model = CustomField fields = ( 'name', 'label', 'group_name', 'type', 'object_types', 'related_object_type', 'required', 'description', 'search_weight', 'filter_logic', 'default', 'choice_set', 'weight', 'validation_minimum', 'validation_maximum', 'validation_regex', 'ui_visible', 'ui_editable', 'is_cloneable', ) class CustomFieldChoiceSetImportForm(CSVModelForm): base_choices = CSVChoiceField( choices=CustomFieldChoiceSetBaseChoices, required=False, help_text=_('The base set of predefined choices to use (if any)') ) extra_choices = SimpleArrayField( base_field=forms.CharField(), required=False, help_text=_( 'Quoted string of comma-separated field choices with optional labels separated by colon: ' '"choice1:First Choice,choice2:Second Choice"' ) ) class Meta: model = CustomFieldChoiceSet fields = ( 'name', 'description', 'extra_choices', 'order_alphabetically', ) def clean_extra_choices(self): if isinstance(self.cleaned_data['extra_choices'], list): data = [] for line in self.cleaned_data['extra_choices']: try: value, label = re.split(r'(?00ff00'), } class JournalEntryImportForm(NetBoxModelImportForm): assigned_object_type = CSVContentTypeField( queryset=ObjectType.objects.all(), label=_('Assigned object type'), ) kind = CSVChoiceField( label=_('Kind'), choices=JournalEntryKindChoices, help_text=_('The classification of entry') ) class Meta: model = JournalEntry fields = ( 'assigned_object_type', 'assigned_object_id', 'created_by', 'kind', 'comments', 'tags' )