object_import.py 9.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295
  1. from django import forms
  2. from django.db.models import Q
  3. from django.utils.translation import gettext_lazy as _
  4. from dcim.choices import InterfacePoEModeChoices, InterfacePoETypeChoices, InterfaceTypeChoices, PortTypeChoices
  5. from dcim.models import *
  6. from wireless.choices import WirelessRoleChoices
  7. __all__ = (
  8. 'ConsolePortTemplateImportForm',
  9. 'ConsoleServerPortTemplateImportForm',
  10. 'CoolingIntakeTemplateImportForm',
  11. 'CoolingOutflowTemplateImportForm',
  12. 'DeviceBayTemplateImportForm',
  13. 'FrontPortTemplateImportForm',
  14. 'InterfaceTemplateImportForm',
  15. 'InventoryItemTemplateImportForm',
  16. 'ModuleBayTemplateImportForm',
  17. 'PortTemplateMappingImportForm',
  18. 'PowerOutletTemplateImportForm',
  19. 'PowerPortTemplateImportForm',
  20. 'RearPortTemplateImportForm',
  21. )
  22. #
  23. # Component template import forms
  24. #
  25. class ConsolePortTemplateImportForm(forms.ModelForm):
  26. class Meta:
  27. model = ConsolePortTemplate
  28. fields = [
  29. 'device_type', 'module_type', 'name', 'label', 'type', 'description',
  30. ]
  31. class ConsoleServerPortTemplateImportForm(forms.ModelForm):
  32. class Meta:
  33. model = ConsoleServerPortTemplate
  34. fields = [
  35. 'device_type', 'module_type', 'name', 'label', 'type', 'description',
  36. ]
  37. class PowerPortTemplateImportForm(forms.ModelForm):
  38. class Meta:
  39. model = PowerPortTemplate
  40. fields = [
  41. 'device_type', 'module_type', 'name', 'label', 'type', 'maximum_draw', 'allocated_draw', 'description',
  42. ]
  43. class PowerOutletTemplateImportForm(forms.ModelForm):
  44. power_port = forms.ModelChoiceField(
  45. label=_('Power port'),
  46. queryset=PowerPortTemplate.objects.all(),
  47. to_field_name='name',
  48. required=False
  49. )
  50. class Meta:
  51. model = PowerOutletTemplate
  52. fields = [
  53. 'device_type', 'module_type', 'name', 'label', 'type', 'power_port', 'feed_leg', 'description',
  54. ]
  55. def clean_device_type(self):
  56. if device_type := self.cleaned_data['device_type']:
  57. power_port = self.fields['power_port']
  58. power_port.queryset = power_port.queryset.filter(device_type=device_type)
  59. return device_type
  60. def clean_module_type(self):
  61. if module_type := self.cleaned_data['module_type']:
  62. power_port = self.fields['power_port']
  63. power_port.queryset = power_port.queryset.filter(module_type=module_type)
  64. return module_type
  65. class CoolingIntakeTemplateImportForm(forms.ModelForm):
  66. class Meta:
  67. model = CoolingIntakeTemplate
  68. fields = [
  69. 'device_type', 'module_type', 'name', 'label', 'type', 'diameter', 'diameter_unit',
  70. 'max_flow', 'max_flow_unit', 'description',
  71. ]
  72. class CoolingOutflowTemplateImportForm(forms.ModelForm):
  73. cooling_intake = forms.ModelChoiceField(
  74. label=_('Cooling intake'),
  75. queryset=CoolingIntakeTemplate.objects.all(),
  76. to_field_name='name',
  77. required=False
  78. )
  79. class Meta:
  80. model = CoolingOutflowTemplate
  81. fields = [
  82. 'device_type', 'module_type', 'name', 'label', 'type', 'diameter', 'diameter_unit',
  83. 'cooling_intake', 'description',
  84. ]
  85. def clean_device_type(self):
  86. if device_type := self.cleaned_data['device_type']:
  87. cooling_intake = self.fields['cooling_intake']
  88. cooling_intake.queryset = cooling_intake.queryset.filter(device_type=device_type)
  89. return device_type
  90. def clean_module_type(self):
  91. if module_type := self.cleaned_data['module_type']:
  92. cooling_intake = self.fields['cooling_intake']
  93. cooling_intake.queryset = cooling_intake.queryset.filter(module_type=module_type)
  94. return module_type
  95. class InterfaceTemplateImportForm(forms.ModelForm):
  96. type = forms.ChoiceField(
  97. label=_('Type'),
  98. choices=InterfaceTypeChoices.CHOICES
  99. )
  100. poe_mode = forms.ChoiceField(
  101. choices=InterfacePoEModeChoices,
  102. required=False,
  103. label=_('PoE mode')
  104. )
  105. poe_type = forms.ChoiceField(
  106. choices=InterfacePoETypeChoices,
  107. required=False,
  108. label=_('PoE type')
  109. )
  110. rf_role = forms.ChoiceField(
  111. choices=WirelessRoleChoices,
  112. required=False,
  113. label=_('Wireless role')
  114. )
  115. class Meta:
  116. model = InterfaceTemplate
  117. fields = [
  118. 'device_type', 'module_type', 'name', 'label', 'type', 'channels', 'channel_id', 'enabled', 'mgmt_only',
  119. 'description', 'poe_mode', 'poe_type', 'rf_role'
  120. ]
  121. class FrontPortTemplateImportForm(forms.ModelForm):
  122. type = forms.ChoiceField(
  123. label=_('Type'),
  124. choices=PortTypeChoices.CHOICES
  125. )
  126. class Meta:
  127. model = FrontPortTemplate
  128. fields = [
  129. 'device_type', 'module_type', 'name', 'type', 'color', 'positions', 'label', 'description',
  130. ]
  131. class RearPortTemplateImportForm(forms.ModelForm):
  132. type = forms.ChoiceField(
  133. label=_('Type'),
  134. choices=PortTypeChoices.CHOICES
  135. )
  136. class Meta:
  137. model = RearPortTemplate
  138. fields = [
  139. 'device_type', 'module_type', 'name', 'type', 'color', 'positions', 'label', 'description',
  140. ]
  141. class PortTemplateMappingImportForm(forms.ModelForm):
  142. front_port = forms.ModelChoiceField(
  143. label=_('Front port'),
  144. queryset=FrontPortTemplate.objects.all(),
  145. to_field_name='name',
  146. )
  147. rear_port = forms.ModelChoiceField(
  148. label=_('Rear port'),
  149. queryset=RearPortTemplate.objects.all(),
  150. to_field_name='name',
  151. )
  152. class Meta:
  153. model = PortTemplateMapping
  154. fields = [
  155. 'device_type', 'module_type', 'front_port', 'front_port_position', 'rear_port', 'rear_port_position',
  156. ]
  157. def clean_device_type(self):
  158. if device_type := self.cleaned_data['device_type']:
  159. front_port = self.fields['front_port']
  160. rear_port = self.fields['rear_port']
  161. front_port.queryset = front_port.queryset.filter(device_type=device_type)
  162. rear_port.queryset = rear_port.queryset.filter(device_type=device_type)
  163. return device_type
  164. def clean_module_type(self):
  165. if module_type := self.cleaned_data['module_type']:
  166. front_port = self.fields['front_port']
  167. rear_port = self.fields['rear_port']
  168. front_port.queryset = front_port.queryset.filter(module_type=module_type)
  169. rear_port.queryset = rear_port.queryset.filter(module_type=module_type)
  170. return module_type
  171. class ModuleBayTemplateImportForm(forms.ModelForm):
  172. module_bay_types = forms.ModelMultipleChoiceField(
  173. label=_('Module bay types'),
  174. queryset=ModuleBayType.objects.all(),
  175. to_field_name='name',
  176. required=False,
  177. )
  178. class Meta:
  179. model = ModuleBayTemplate
  180. fields = [
  181. 'device_type', 'module_type', 'name', 'label', 'position', 'description', 'module_bay_types',
  182. ]
  183. def clean_device_type(self):
  184. if device_type := self.cleaned_data['device_type']:
  185. module_bay_types = self.fields['module_bay_types']
  186. module_bay_types.queryset = module_bay_types.queryset.filter(
  187. Q(manufacturer__isnull=True) | Q(manufacturer=device_type.manufacturer)
  188. )
  189. return device_type
  190. def clean_module_type(self):
  191. if module_type := self.cleaned_data['module_type']:
  192. module_bay_types = self.fields['module_bay_types']
  193. module_bay_types.queryset = module_bay_types.queryset.filter(
  194. Q(manufacturer__isnull=True) | Q(manufacturer=module_type.manufacturer)
  195. )
  196. return module_type
  197. class DeviceBayTemplateImportForm(forms.ModelForm):
  198. class Meta:
  199. model = DeviceBayTemplate
  200. fields = [
  201. 'device_type', 'name', 'label', 'description',
  202. ]
  203. class InventoryItemTemplateImportForm(forms.ModelForm):
  204. parent = forms.ModelChoiceField(
  205. label=_('Parent'),
  206. queryset=InventoryItemTemplate.objects.all(),
  207. required=False
  208. )
  209. role = forms.ModelChoiceField(
  210. label=_('Role'),
  211. queryset=InventoryItemRole.objects.all(),
  212. to_field_name='name',
  213. required=False
  214. )
  215. manufacturer = forms.ModelChoiceField(
  216. label=_('Manufacturer'),
  217. queryset=Manufacturer.objects.all(),
  218. to_field_name='name',
  219. required=False
  220. )
  221. class Meta:
  222. model = InventoryItemTemplate
  223. fields = [
  224. 'device_type', 'parent', 'name', 'label', 'role', 'manufacturer', 'part_id', 'description',
  225. ]
  226. def clean_device_type(self):
  227. if device_type := self.cleaned_data['device_type']:
  228. parent = self.fields['parent']
  229. parent.queryset = parent.queryset.filter(device_type=device_type)
  230. return device_type
  231. def clean_module_type(self):
  232. if module_type := self.cleaned_data['module_type']:
  233. parent = self.fields['parent']
  234. parent.queryset = parent.queryset.filter(module_type=module_type)
  235. return module_type