Coverage for wireless/forms/bulk_import.py: 69%

58 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 18:35 +0000

1from django.utils.translation import gettext_lazy as _ 

2 

3from dcim.choices import LinkStatusChoices 

4from dcim.forms.mixins import ScopedImportForm 

5from dcim.models import Device, Interface, Site 

6from ipam.models import VLAN 

7from netbox.choices import * 

8from netbox.forms import NestedGroupModelImportForm, PrimaryModelImportForm 

9from tenancy.models import Tenant 

10from utilities.forms.fields import CSVChoiceField, CSVModelChoiceField 

11from wireless.choices import * 

12from wireless.models import * 

13 

14__all__ = ( 

15 'WirelessLANGroupImportForm', 

16 'WirelessLANImportForm', 

17 'WirelessLinkImportForm', 

18) 

19 

20 

21class WirelessLANGroupImportForm(NestedGroupModelImportForm): 

22 parent = CSVModelChoiceField( 

23 label=_('Parent'), 

24 queryset=WirelessLANGroup.objects.all(), 

25 required=False, 

26 to_field_name='name', 

27 help_text=_('Parent group') 

28 ) 

29 

30 class Meta: 

31 model = WirelessLANGroup 

32 fields = ('name', 'slug', 'parent', 'description', 'owner', 'comments', 'tags') 

33 

34 

35class WirelessLANImportForm(ScopedImportForm, PrimaryModelImportForm): 

36 group = CSVModelChoiceField( 

37 label=_('Group'), 

38 queryset=WirelessLANGroup.objects.all(), 

39 required=False, 

40 to_field_name='name', 

41 help_text=_('Assigned group') 

42 ) 

43 status = CSVChoiceField( 

44 label=_('Status'), 

45 choices=WirelessLANStatusChoices, 

46 help_text=_('Operational status') 

47 ) 

48 vlan = CSVModelChoiceField( 

49 label=_('VLAN'), 

50 queryset=VLAN.objects.all(), 

51 required=False, 

52 to_field_name='name', 

53 help_text=_('Bridged VLAN') 

54 ) 

55 tenant = CSVModelChoiceField( 

56 label=_('Tenant'), 

57 queryset=Tenant.objects.all(), 

58 required=False, 

59 to_field_name='name', 

60 help_text=_('Assigned tenant') 

61 ) 

62 auth_type = CSVChoiceField( 

63 label=_('Authentication type'), 

64 choices=WirelessAuthTypeChoices, 

65 required=False, 

66 help_text=_('Authentication type') 

67 ) 

68 auth_cipher = CSVChoiceField( 

69 label=_('Authentication cipher'), 

70 choices=WirelessAuthCipherChoices, 

71 required=False, 

72 help_text=_('Authentication cipher') 

73 ) 

74 

75 class Meta: 

76 model = WirelessLAN 

77 fields = ( 

78 'ssid', 'group', 'status', 'vlan', 'tenant', 'auth_type', 'auth_cipher', 'auth_psk', 'scope_type', 

79 'scope_name', 'scope_id', 'description', 'owner', 'comments', 'tags', 

80 ) 

81 labels = { 

82 'scope_id': _('Scope ID'), 

83 } 

84 

85 

86class WirelessLinkImportForm(PrimaryModelImportForm): 

87 # Termination A 

88 site_a = CSVModelChoiceField( 

89 label=_('Site A'), 

90 queryset=Site.objects.all(), 

91 required=False, 

92 to_field_name='name', 

93 help_text=_('Site of parent device A (if any)'), 

94 ) 

95 device_a = CSVModelChoiceField( 

96 label=_('Device A'), 

97 queryset=Device.objects.all(), 

98 to_field_name='name', 

99 help_text=_('Parent device of assigned interface A'), 

100 ) 

101 interface_a = CSVModelChoiceField( 

102 label=_('Interface A'), 

103 queryset=Interface.objects.all(), 

104 to_field_name='name', 

105 help_text=_('Assigned interface A'), 

106 ) 

107 

108 # Termination B 

109 site_b = CSVModelChoiceField( 

110 label=_('Site B'), 

111 queryset=Site.objects.all(), 

112 required=False, 

113 to_field_name='name', 

114 help_text=_('Site of parent device B (if any)'), 

115 ) 

116 device_b = CSVModelChoiceField( 

117 label=_('Device B'), 

118 queryset=Device.objects.all(), 

119 to_field_name='name', 

120 help_text=_('Parent device of assigned interface B'), 

121 ) 

122 interface_b = CSVModelChoiceField( 

123 label=_('Interface B'), 

124 queryset=Interface.objects.all(), 

125 to_field_name='name', 

126 help_text=_('Assigned interface B'), 

127 ) 

128 

129 # WirelessLink attributes 

130 status = CSVChoiceField( 

131 label=_('Status'), 

132 choices=LinkStatusChoices, 

133 help_text=_('Connection status'), 

134 ) 

135 tenant = CSVModelChoiceField( 

136 label=_('Tenant'), 

137 queryset=Tenant.objects.all(), 

138 required=False, 

139 to_field_name='name', 

140 help_text=_('Assigned tenant') 

141 ) 

142 auth_type = CSVChoiceField( 

143 label=_('Authentication type'), 

144 choices=WirelessAuthTypeChoices, 

145 required=False, 

146 help_text=_('Authentication type') 

147 ) 

148 auth_cipher = CSVChoiceField( 

149 label=_('Authentication cipher'), 

150 choices=WirelessAuthCipherChoices, 

151 required=False, 

152 help_text=_('Authentication cipher') 

153 ) 

154 distance_unit = CSVChoiceField( 

155 label=_('Distance unit'), 

156 choices=DistanceUnitChoices, 

157 required=False, 

158 help_text=_('Distance unit') 

159 ) 

160 

161 class Meta: 

162 model = WirelessLink 

163 fields = ( 

164 'site_a', 'device_a', 'interface_a', 'site_b', 'device_b', 'interface_b', 'status', 'ssid', 'tenant', 

165 'auth_type', 'auth_cipher', 'auth_psk', 'distance', 'distance_unit', 'description', 'owner', 'comments', 

166 'tags', 

167 ) 

168 

169 def __init__(self, data=None, *args, **kwargs): 

170 super().__init__(data, *args, **kwargs) 

171 

172 if data: 

173 # Limit choices for interface_a to the assigned device_a 

174 interface_a_params = {f'device__{self.fields["device_a"].to_field_name}': data.get('device_a')} 

175 # Limit choices for device_a to the assigned site_a 

176 if site_a := data.get('site_a'): 

177 device_a_params = {f'site__{self.fields["site_a"].to_field_name}': site_a} 

178 self.fields['device_a'].queryset = self.fields['device_a'].queryset.filter(**device_a_params) 

179 interface_a_params.update({f'device__site__{self.fields["site_a"].to_field_name}': site_a}) 

180 self.fields['interface_a'].queryset = self.fields['interface_a'].queryset.filter(**interface_a_params) 

181 

182 # Limit choices for interface_b to the assigned device_b 

183 interface_b_params = {f'device__{self.fields["device_b"].to_field_name}': data.get('device_b')} 

184 # Limit choices for device_b to the assigned site_b 

185 if site_b := data.get('site_b'): 

186 device_b_params = {f'site__{self.fields["site_b"].to_field_name}': site_b} 

187 self.fields['device_b'].queryset = self.fields['device_b'].queryset.filter(**device_b_params) 

188 interface_b_params.update({f'device__site__{self.fields["site_b"].to_field_name}': site_b}) 

189 self.fields['interface_b'].queryset = self.fields['interface_b'].queryset.filter(**interface_b_params)