bulk_import.py 6.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189
  1. from django.utils.translation import gettext_lazy as _
  2. from dcim.choices import LinkStatusChoices
  3. from dcim.forms.mixins import ScopedImportForm
  4. from dcim.models import Device, Interface, Site
  5. from ipam.models import VLAN
  6. from netbox.choices import *
  7. from netbox.forms import NestedGroupModelImportForm, PrimaryModelImportForm
  8. from tenancy.models import Tenant
  9. from utilities.forms.fields import CSVChoiceField, CSVModelChoiceField
  10. from wireless.choices import *
  11. from wireless.models import *
  12. __all__ = (
  13. 'WirelessLANImportForm',
  14. 'WirelessLANGroupImportForm',
  15. 'WirelessLinkImportForm',
  16. )
  17. class WirelessLANGroupImportForm(NestedGroupModelImportForm):
  18. parent = CSVModelChoiceField(
  19. label=_('Parent'),
  20. queryset=WirelessLANGroup.objects.all(),
  21. required=False,
  22. to_field_name='name',
  23. help_text=_('Parent group')
  24. )
  25. class Meta:
  26. model = WirelessLANGroup
  27. fields = ('name', 'slug', 'parent', 'description', 'owner', 'comments', 'tags')
  28. class WirelessLANImportForm(ScopedImportForm, PrimaryModelImportForm):
  29. group = CSVModelChoiceField(
  30. label=_('Group'),
  31. queryset=WirelessLANGroup.objects.all(),
  32. required=False,
  33. to_field_name='name',
  34. help_text=_('Assigned group')
  35. )
  36. status = CSVChoiceField(
  37. label=_('Status'),
  38. choices=WirelessLANStatusChoices,
  39. help_text=_('Operational status')
  40. )
  41. vlan = CSVModelChoiceField(
  42. label=_('VLAN'),
  43. queryset=VLAN.objects.all(),
  44. required=False,
  45. to_field_name='name',
  46. help_text=_('Bridged VLAN')
  47. )
  48. tenant = CSVModelChoiceField(
  49. label=_('Tenant'),
  50. queryset=Tenant.objects.all(),
  51. required=False,
  52. to_field_name='name',
  53. help_text=_('Assigned tenant')
  54. )
  55. auth_type = CSVChoiceField(
  56. label=_('Authentication type'),
  57. choices=WirelessAuthTypeChoices,
  58. required=False,
  59. help_text=_('Authentication type')
  60. )
  61. auth_cipher = CSVChoiceField(
  62. label=_('Authentication cipher'),
  63. choices=WirelessAuthCipherChoices,
  64. required=False,
  65. help_text=_('Authentication cipher')
  66. )
  67. class Meta:
  68. model = WirelessLAN
  69. fields = (
  70. 'ssid', 'group', 'status', 'vlan', 'tenant', 'auth_type', 'auth_cipher', 'auth_psk', 'scope_type',
  71. 'scope_id', 'description', 'owner', 'comments', 'tags',
  72. )
  73. labels = {
  74. 'scope_id': _('Scope ID'),
  75. }
  76. class WirelessLinkImportForm(PrimaryModelImportForm):
  77. # Termination A
  78. site_a = CSVModelChoiceField(
  79. label=_('Site A'),
  80. queryset=Site.objects.all(),
  81. required=False,
  82. to_field_name='name',
  83. help_text=_('Site of parent device A (if any)'),
  84. )
  85. device_a = CSVModelChoiceField(
  86. label=_('Device A'),
  87. queryset=Device.objects.all(),
  88. to_field_name='name',
  89. help_text=_('Parent device of assigned interface A'),
  90. )
  91. interface_a = CSVModelChoiceField(
  92. label=_('Interface A'),
  93. queryset=Interface.objects.all(),
  94. to_field_name='name',
  95. help_text=_('Assigned interface A'),
  96. )
  97. # Termination B
  98. site_b = CSVModelChoiceField(
  99. label=_('Site B'),
  100. queryset=Site.objects.all(),
  101. required=False,
  102. to_field_name='name',
  103. help_text=_('Site of parent device B (if any)'),
  104. )
  105. device_b = CSVModelChoiceField(
  106. label=_('Device B'),
  107. queryset=Device.objects.all(),
  108. to_field_name='name',
  109. help_text=_('Parent device of assigned interface B'),
  110. )
  111. interface_b = CSVModelChoiceField(
  112. label=_('Interface B'),
  113. queryset=Interface.objects.all(),
  114. to_field_name='name',
  115. help_text=_('Assigned interface B'),
  116. )
  117. # WirelessLink attributes
  118. status = CSVChoiceField(
  119. label=_('Status'),
  120. choices=LinkStatusChoices,
  121. help_text=_('Connection status'),
  122. )
  123. tenant = CSVModelChoiceField(
  124. label=_('Tenant'),
  125. queryset=Tenant.objects.all(),
  126. required=False,
  127. to_field_name='name',
  128. help_text=_('Assigned tenant')
  129. )
  130. auth_type = CSVChoiceField(
  131. label=_('Authentication type'),
  132. choices=WirelessAuthTypeChoices,
  133. required=False,
  134. help_text=_('Authentication type')
  135. )
  136. auth_cipher = CSVChoiceField(
  137. label=_('Authentication cipher'),
  138. choices=WirelessAuthCipherChoices,
  139. required=False,
  140. help_text=_('Authentication cipher')
  141. )
  142. distance_unit = CSVChoiceField(
  143. label=_('Distance unit'),
  144. choices=DistanceUnitChoices,
  145. required=False,
  146. help_text=_('Distance unit')
  147. )
  148. class Meta:
  149. model = WirelessLink
  150. fields = (
  151. 'site_a', 'device_a', 'interface_a', 'site_b', 'device_b', 'interface_b', 'status', 'ssid', 'tenant',
  152. 'auth_type', 'auth_cipher', 'auth_psk', 'distance', 'distance_unit', 'description', 'owner', 'comments',
  153. 'tags',
  154. )
  155. def __init__(self, data=None, *args, **kwargs):
  156. super().__init__(data, *args, **kwargs)
  157. if data:
  158. # Limit choices for interface_a to the assigned device_a
  159. interface_a_params = {f'device__{self.fields["device_a"].to_field_name}': data.get('device_a')}
  160. # Limit choices for device_a to the assigned site_a
  161. if site_a := data.get('site_a'):
  162. device_a_params = {f'site__{self.fields["site_a"].to_field_name}': site_a}
  163. self.fields['device_a'].queryset = self.fields['device_a'].queryset.filter(**device_a_params)
  164. interface_a_params.update({f'device__site__{self.fields["site_a"].to_field_name}': site_a})
  165. self.fields['interface_a'].queryset = self.fields['interface_a'].queryset.filter(**interface_a_params)
  166. # Limit choices for interface_b to the assigned device_b
  167. interface_b_params = {f'device__{self.fields["device_b"].to_field_name}': data.get('device_b')}
  168. # Limit choices for device_b to the assigned site_b
  169. if site_b := data.get('site_b'):
  170. device_b_params = {f'site__{self.fields["site_b"].to_field_name}': site_b}
  171. self.fields['device_b'].queryset = self.fields['device_b'].queryset.filter(**device_b_params)
  172. interface_b_params.update({f'device__site__{self.fields["site_b"].to_field_name}': site_b})
  173. self.fields['interface_b'].queryset = self.fields['interface_b'].queryset.filter(**interface_b_params)