1# SPDX-License-Identifier: Apache-2.0
2# Copyright (C) 2025 Marcin Zieba <marcinpsk@gmail.com>
3from django import forms
4from dcim.models import Site, Location
5from tenancy.models import Tenant
6from netbox.forms import NetBoxModelForm
7from utilities.forms.fields import DynamicModelChoiceField
8from .models import ImportProfile, ColumnMapping, ClassRoleMapping, DeviceTypeMapping, ColumnTransformRule
9
10
11class ImportProfileForm(NetBoxModelForm):
12 """Form for creating and editing ImportProfile instances."""
13
14 class Meta:
15 model = ImportProfile
16 fields = [
17 "name",
18 "description",
19 "sheet_name",
20 "source_id_column",
21 "custom_field_name",
22 "update_existing",
23 "create_missing_device_types",
24 "preview_view_mode",
25 "tags",
26 ]
27
28
29class ColumnMappingForm(forms.ModelForm):
30 """Form for creating and editing ColumnMapping instances."""
31
32 class Meta:
33 model = ColumnMapping
34 fields = ["profile", "source_column", "target_field"]
35 widgets = {"profile": forms.HiddenInput()}
36
37
38class ClassRoleMappingForm(forms.ModelForm):
39 """Form for creating and editing ClassRoleMapping instances."""
40
41 class Meta:
42 model = ClassRoleMapping
43 fields = ["profile", "source_class", "creates_rack", "role_slug", "ignore"]
44 widgets = {"profile": forms.HiddenInput()}
45
46
47class DeviceTypeMappingForm(forms.ModelForm):
48 """Form for creating and editing DeviceTypeMapping instances."""
49
50 class Meta:
51 model = DeviceTypeMapping
52 fields = [
53 "profile",
54 "source_make",
55 "source_model",
56 "netbox_manufacturer_slug",
57 "netbox_device_type_slug",
58 ]
59 widgets = {"profile": forms.HiddenInput()}
60
61
62class ColumnTransformRuleForm(forms.ModelForm):
63 """Form for creating and editing ColumnTransformRule instances."""
64
65 class Meta:
66 model = ColumnTransformRule
67 fields = ["profile", "source_column", "pattern", "group_1_target", "group_2_target"]
68 widgets = {"profile": forms.HiddenInput()}
69
70
71class ImportSetupForm(forms.Form):
72 """Form for the import wizard step 1: select profile, upload file, choose site/location/tenant."""
73
74 # ImportProfile has no REST API endpoint yet, so use a plain select
75 profile = forms.ModelChoiceField(
76 queryset=ImportProfile.objects.all(),
77 label="Import Profile",
78 empty_label="— Select a profile —",
79 )
80 excel_file = forms.FileField(
81 label="Excel File",
82 help_text="Upload the Excel file to import (.xlsx)",
83 )
84 site = DynamicModelChoiceField(
85 queryset=Site.objects.all(),
86 label="Target Site",
87 )
88 location = DynamicModelChoiceField(
89 queryset=Location.objects.all(),
90 label="Location (optional)",
91 required=False,
92 query_params={"site_id": "$site"},
93 )
94 tenant = DynamicModelChoiceField(
95 queryset=Tenant.objects.all(),
96 label="Tenant (optional)",
97 required=False,
98 )