bulk_edit.py 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528
  1. from django import forms
  2. from django.utils.translation import gettext as _
  3. from dcim.models import Region, Site, SiteGroup
  4. from ipam.choices import *
  5. from ipam.constants import *
  6. from ipam.models import *
  7. from ipam.models import ASN
  8. from netbox.forms import NetBoxModelBulkEditForm
  9. from tenancy.models import Tenant
  10. from utilities.forms import add_blank_choice
  11. from utilities.forms.fields import (
  12. CommentField, DynamicModelChoiceField, DynamicModelMultipleChoiceField, NumericArrayField,
  13. )
  14. from utilities.forms.widgets import BulkEditNullBooleanSelect
  15. __all__ = (
  16. 'AggregateBulkEditForm',
  17. 'ASNBulkEditForm',
  18. 'ASNRangeBulkEditForm',
  19. 'FHRPGroupBulkEditForm',
  20. 'IPAddressBulkEditForm',
  21. 'IPRangeBulkEditForm',
  22. 'L2VPNBulkEditForm',
  23. 'L2VPNTerminationBulkEditForm',
  24. 'PrefixBulkEditForm',
  25. 'RIRBulkEditForm',
  26. 'RoleBulkEditForm',
  27. 'RouteTargetBulkEditForm',
  28. 'ServiceBulkEditForm',
  29. 'ServiceTemplateBulkEditForm',
  30. 'VLANBulkEditForm',
  31. 'VLANGroupBulkEditForm',
  32. 'VRFBulkEditForm',
  33. )
  34. class VRFBulkEditForm(NetBoxModelBulkEditForm):
  35. tenant = DynamicModelChoiceField(
  36. queryset=Tenant.objects.all(),
  37. required=False
  38. )
  39. enforce_unique = forms.NullBooleanField(
  40. required=False,
  41. widget=BulkEditNullBooleanSelect(),
  42. label=_('Enforce unique space')
  43. )
  44. description = forms.CharField(
  45. max_length=200,
  46. required=False
  47. )
  48. comments = CommentField(
  49. label='Comments'
  50. )
  51. model = VRF
  52. fieldsets = (
  53. (None, ('tenant', 'enforce_unique', 'description')),
  54. )
  55. nullable_fields = ('tenant', 'description', 'comments')
  56. class RouteTargetBulkEditForm(NetBoxModelBulkEditForm):
  57. tenant = DynamicModelChoiceField(
  58. queryset=Tenant.objects.all(),
  59. required=False
  60. )
  61. description = forms.CharField(
  62. max_length=200,
  63. required=False
  64. )
  65. comments = CommentField(
  66. label='Comments'
  67. )
  68. model = RouteTarget
  69. fieldsets = (
  70. (None, ('tenant', 'description')),
  71. )
  72. nullable_fields = ('tenant', 'description', 'comments')
  73. class RIRBulkEditForm(NetBoxModelBulkEditForm):
  74. is_private = forms.NullBooleanField(
  75. required=False,
  76. widget=BulkEditNullBooleanSelect
  77. )
  78. description = forms.CharField(
  79. max_length=200,
  80. required=False
  81. )
  82. model = RIR
  83. fieldsets = (
  84. (None, ('is_private', 'description')),
  85. )
  86. nullable_fields = ('is_private', 'description')
  87. class ASNRangeBulkEditForm(NetBoxModelBulkEditForm):
  88. rir = DynamicModelChoiceField(
  89. queryset=RIR.objects.all(),
  90. required=False,
  91. label=_('RIR')
  92. )
  93. tenant = DynamicModelChoiceField(
  94. queryset=Tenant.objects.all(),
  95. required=False
  96. )
  97. description = forms.CharField(
  98. max_length=200,
  99. required=False
  100. )
  101. model = ASNRange
  102. fieldsets = (
  103. (None, ('rir', 'tenant', 'description')),
  104. )
  105. nullable_fields = ('description',)
  106. class ASNBulkEditForm(NetBoxModelBulkEditForm):
  107. sites = DynamicModelMultipleChoiceField(
  108. queryset=Site.objects.all(),
  109. required=False
  110. )
  111. rir = DynamicModelChoiceField(
  112. queryset=RIR.objects.all(),
  113. required=False,
  114. label=_('RIR')
  115. )
  116. tenant = DynamicModelChoiceField(
  117. queryset=Tenant.objects.all(),
  118. required=False
  119. )
  120. description = forms.CharField(
  121. max_length=200,
  122. required=False
  123. )
  124. comments = CommentField(
  125. label='Comments'
  126. )
  127. model = ASN
  128. fieldsets = (
  129. (None, ('sites', 'rir', 'tenant', 'description')),
  130. )
  131. nullable_fields = ('tenant', 'description', 'comments')
  132. class AggregateBulkEditForm(NetBoxModelBulkEditForm):
  133. rir = DynamicModelChoiceField(
  134. queryset=RIR.objects.all(),
  135. required=False,
  136. label=_('RIR')
  137. )
  138. tenant = DynamicModelChoiceField(
  139. queryset=Tenant.objects.all(),
  140. required=False
  141. )
  142. date_added = forms.DateField(
  143. required=False
  144. )
  145. description = forms.CharField(
  146. max_length=200,
  147. required=False
  148. )
  149. comments = CommentField(
  150. label='Comments'
  151. )
  152. model = Aggregate
  153. fieldsets = (
  154. (None, ('rir', 'tenant', 'date_added', 'description')),
  155. )
  156. nullable_fields = ('date_added', 'description', 'comments')
  157. class RoleBulkEditForm(NetBoxModelBulkEditForm):
  158. weight = forms.IntegerField(
  159. required=False
  160. )
  161. description = forms.CharField(
  162. max_length=200,
  163. required=False
  164. )
  165. model = Role
  166. fieldsets = (
  167. (None, ('weight', 'description')),
  168. )
  169. nullable_fields = ('description',)
  170. class PrefixBulkEditForm(NetBoxModelBulkEditForm):
  171. region = DynamicModelChoiceField(
  172. queryset=Region.objects.all(),
  173. required=False
  174. )
  175. site_group = DynamicModelChoiceField(
  176. queryset=SiteGroup.objects.all(),
  177. required=False
  178. )
  179. site = DynamicModelChoiceField(
  180. queryset=Site.objects.all(),
  181. required=False,
  182. query_params={
  183. 'region_id': '$region',
  184. 'group_id': '$site_group',
  185. }
  186. )
  187. vrf = DynamicModelChoiceField(
  188. queryset=VRF.objects.all(),
  189. required=False,
  190. label=_('VRF')
  191. )
  192. prefix_length = forms.IntegerField(
  193. min_value=PREFIX_LENGTH_MIN,
  194. max_value=PREFIX_LENGTH_MAX,
  195. required=False
  196. )
  197. tenant = DynamicModelChoiceField(
  198. queryset=Tenant.objects.all(),
  199. required=False
  200. )
  201. status = forms.ChoiceField(
  202. choices=add_blank_choice(PrefixStatusChoices),
  203. required=False
  204. )
  205. role = DynamicModelChoiceField(
  206. queryset=Role.objects.all(),
  207. required=False
  208. )
  209. is_pool = forms.NullBooleanField(
  210. required=False,
  211. widget=BulkEditNullBooleanSelect(),
  212. label=_('Is a pool')
  213. )
  214. mark_utilized = forms.NullBooleanField(
  215. required=False,
  216. widget=BulkEditNullBooleanSelect(),
  217. label=_('Treat as 100% utilized')
  218. )
  219. description = forms.CharField(
  220. max_length=200,
  221. required=False
  222. )
  223. comments = CommentField(
  224. label='Comments'
  225. )
  226. model = Prefix
  227. fieldsets = (
  228. (None, ('tenant', 'status', 'role', 'description')),
  229. ('Site', ('region', 'site_group', 'site')),
  230. ('Addressing', ('vrf', 'prefix_length', 'is_pool', 'mark_utilized')),
  231. )
  232. nullable_fields = (
  233. 'site', 'vrf', 'tenant', 'role', 'description', 'comments',
  234. )
  235. class IPRangeBulkEditForm(NetBoxModelBulkEditForm):
  236. vrf = DynamicModelChoiceField(
  237. queryset=VRF.objects.all(),
  238. required=False,
  239. label=_('VRF')
  240. )
  241. tenant = DynamicModelChoiceField(
  242. queryset=Tenant.objects.all(),
  243. required=False
  244. )
  245. status = forms.ChoiceField(
  246. choices=add_blank_choice(IPRangeStatusChoices),
  247. required=False
  248. )
  249. role = DynamicModelChoiceField(
  250. queryset=Role.objects.all(),
  251. required=False
  252. )
  253. mark_utilized = forms.NullBooleanField(
  254. required=False,
  255. widget=BulkEditNullBooleanSelect(),
  256. label=_('Treat as 100% utilized')
  257. )
  258. description = forms.CharField(
  259. max_length=200,
  260. required=False
  261. )
  262. comments = CommentField(
  263. label='Comments'
  264. )
  265. model = IPRange
  266. fieldsets = (
  267. (None, ('status', 'role', 'vrf', 'tenant', 'mark_utilized', 'description')),
  268. )
  269. nullable_fields = (
  270. 'vrf', 'tenant', 'role', 'description', 'comments',
  271. )
  272. class IPAddressBulkEditForm(NetBoxModelBulkEditForm):
  273. vrf = DynamicModelChoiceField(
  274. queryset=VRF.objects.all(),
  275. required=False,
  276. label=_('VRF')
  277. )
  278. mask_length = forms.IntegerField(
  279. min_value=IPADDRESS_MASK_LENGTH_MIN,
  280. max_value=IPADDRESS_MASK_LENGTH_MAX,
  281. required=False
  282. )
  283. tenant = DynamicModelChoiceField(
  284. queryset=Tenant.objects.all(),
  285. required=False
  286. )
  287. status = forms.ChoiceField(
  288. choices=add_blank_choice(IPAddressStatusChoices),
  289. required=False
  290. )
  291. role = forms.ChoiceField(
  292. choices=add_blank_choice(IPAddressRoleChoices),
  293. required=False
  294. )
  295. dns_name = forms.CharField(
  296. max_length=255,
  297. required=False,
  298. label=_('DNS name')
  299. )
  300. description = forms.CharField(
  301. max_length=200,
  302. required=False
  303. )
  304. comments = CommentField(
  305. label='Comments'
  306. )
  307. model = IPAddress
  308. fieldsets = (
  309. (None, ('status', 'role', 'tenant', 'description')),
  310. ('Addressing', ('vrf', 'mask_length', 'dns_name')),
  311. )
  312. nullable_fields = (
  313. 'vrf', 'role', 'tenant', 'dns_name', 'description', 'comments',
  314. )
  315. class FHRPGroupBulkEditForm(NetBoxModelBulkEditForm):
  316. protocol = forms.ChoiceField(
  317. choices=add_blank_choice(FHRPGroupProtocolChoices),
  318. required=False
  319. )
  320. group_id = forms.IntegerField(
  321. min_value=0,
  322. required=False,
  323. label=_('Group ID')
  324. )
  325. auth_type = forms.ChoiceField(
  326. choices=add_blank_choice(FHRPGroupAuthTypeChoices),
  327. required=False,
  328. label=_('Authentication type')
  329. )
  330. auth_key = forms.CharField(
  331. max_length=255,
  332. required=False,
  333. label=_('Authentication key')
  334. )
  335. name = forms.CharField(
  336. max_length=100,
  337. required=False
  338. )
  339. description = forms.CharField(
  340. max_length=200,
  341. required=False
  342. )
  343. comments = CommentField(
  344. label='Comments'
  345. )
  346. model = FHRPGroup
  347. fieldsets = (
  348. (None, ('protocol', 'group_id', 'name', 'description')),
  349. ('Authentication', ('auth_type', 'auth_key')),
  350. )
  351. nullable_fields = ('auth_type', 'auth_key', 'name', 'description', 'comments')
  352. class VLANGroupBulkEditForm(NetBoxModelBulkEditForm):
  353. site = DynamicModelChoiceField(
  354. queryset=Site.objects.all(),
  355. required=False
  356. )
  357. min_vid = forms.IntegerField(
  358. min_value=VLAN_VID_MIN,
  359. max_value=VLAN_VID_MAX,
  360. required=False,
  361. label=_('Minimum child VLAN VID')
  362. )
  363. max_vid = forms.IntegerField(
  364. min_value=VLAN_VID_MIN,
  365. max_value=VLAN_VID_MAX,
  366. required=False,
  367. label=_('Maximum child VLAN VID')
  368. )
  369. description = forms.CharField(
  370. max_length=200,
  371. required=False
  372. )
  373. model = VLANGroup
  374. fieldsets = (
  375. (None, ('site', 'min_vid', 'max_vid', 'description')),
  376. )
  377. nullable_fields = ('site', 'description')
  378. class VLANBulkEditForm(NetBoxModelBulkEditForm):
  379. region = DynamicModelChoiceField(
  380. queryset=Region.objects.all(),
  381. required=False
  382. )
  383. site_group = DynamicModelChoiceField(
  384. queryset=SiteGroup.objects.all(),
  385. required=False
  386. )
  387. site = DynamicModelChoiceField(
  388. queryset=Site.objects.all(),
  389. required=False,
  390. query_params={
  391. 'region_id': '$region',
  392. 'group_id': '$site_group',
  393. }
  394. )
  395. group = DynamicModelChoiceField(
  396. queryset=VLANGroup.objects.all(),
  397. required=False,
  398. query_params={
  399. 'site_id': '$site'
  400. }
  401. )
  402. tenant = DynamicModelChoiceField(
  403. queryset=Tenant.objects.all(),
  404. required=False
  405. )
  406. status = forms.ChoiceField(
  407. choices=add_blank_choice(VLANStatusChoices),
  408. required=False
  409. )
  410. role = DynamicModelChoiceField(
  411. queryset=Role.objects.all(),
  412. required=False
  413. )
  414. description = forms.CharField(
  415. max_length=200,
  416. required=False
  417. )
  418. comments = CommentField(
  419. label='Comments'
  420. )
  421. model = VLAN
  422. fieldsets = (
  423. (None, ('status', 'role', 'tenant', 'description')),
  424. ('Site & Group', ('region', 'site_group', 'site', 'group')),
  425. )
  426. nullable_fields = (
  427. 'site', 'group', 'tenant', 'role', 'description', 'comments',
  428. )
  429. class ServiceTemplateBulkEditForm(NetBoxModelBulkEditForm):
  430. protocol = forms.ChoiceField(
  431. choices=add_blank_choice(ServiceProtocolChoices),
  432. required=False
  433. )
  434. ports = NumericArrayField(
  435. base_field=forms.IntegerField(
  436. min_value=SERVICE_PORT_MIN,
  437. max_value=SERVICE_PORT_MAX
  438. ),
  439. required=False
  440. )
  441. description = forms.CharField(
  442. max_length=200,
  443. required=False
  444. )
  445. comments = CommentField(
  446. label='Comments'
  447. )
  448. model = ServiceTemplate
  449. fieldsets = (
  450. (None, ('protocol', 'ports', 'description')),
  451. )
  452. nullable_fields = ('description', 'comments')
  453. class ServiceBulkEditForm(ServiceTemplateBulkEditForm):
  454. model = Service
  455. class L2VPNBulkEditForm(NetBoxModelBulkEditForm):
  456. type = forms.ChoiceField(
  457. choices=add_blank_choice(L2VPNTypeChoices),
  458. required=False
  459. )
  460. tenant = DynamicModelChoiceField(
  461. queryset=Tenant.objects.all(),
  462. required=False
  463. )
  464. description = forms.CharField(
  465. max_length=200,
  466. required=False
  467. )
  468. comments = CommentField(
  469. label='Comments'
  470. )
  471. model = L2VPN
  472. fieldsets = (
  473. (None, ('type', 'tenant', 'description')),
  474. )
  475. nullable_fields = ('tenant', 'description', 'comments')
  476. class L2VPNTerminationBulkEditForm(NetBoxModelBulkEditForm):
  477. model = L2VPN