forms.py 9.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358
  1. from django import forms
  2. from taggit.forms import TagField
  3. from dcim.models import Region, Site
  4. from extras.forms import AddRemoveTagsForm, CustomFieldForm, CustomFieldBulkEditForm, CustomFieldFilterForm
  5. from tenancy.forms import TenancyFilterForm, TenancyForm
  6. from tenancy.models import Tenant
  7. from utilities.forms import (
  8. APISelect, APISelectMultiple, add_blank_choice, BootstrapMixin, CommentField, CSVChoiceField,
  9. DatePicker, FilterChoiceField, SmallTextarea, SlugField, StaticSelect2, StaticSelect2Multiple
  10. )
  11. from .choices import CircuitStatusChoices
  12. from .models import Circuit, CircuitTermination, CircuitType, Provider
  13. #
  14. # Providers
  15. #
  16. class ProviderForm(BootstrapMixin, CustomFieldForm):
  17. slug = SlugField()
  18. comments = CommentField()
  19. tags = TagField(
  20. required=False
  21. )
  22. class Meta:
  23. model = Provider
  24. fields = [
  25. 'name', 'slug', 'asn', 'account', 'portal_url', 'noc_contact', 'admin_contact', 'comments', 'tags',
  26. ]
  27. widgets = {
  28. 'noc_contact': SmallTextarea(
  29. attrs={'rows': 5}
  30. ),
  31. 'admin_contact': SmallTextarea(
  32. attrs={'rows': 5}
  33. ),
  34. }
  35. help_texts = {
  36. 'name': "Full name of the provider",
  37. 'asn': "BGP autonomous system number (if applicable)",
  38. 'portal_url': "URL of the provider's customer support portal",
  39. 'noc_contact': "NOC email address and phone number",
  40. 'admin_contact': "Administrative contact email address and phone number",
  41. }
  42. class ProviderCSVForm(forms.ModelForm):
  43. slug = SlugField()
  44. class Meta:
  45. model = Provider
  46. fields = Provider.csv_headers
  47. help_texts = {
  48. 'name': 'Provider name',
  49. 'asn': '32-bit autonomous system number',
  50. 'portal_url': 'Portal URL',
  51. 'comments': 'Free-form comments',
  52. }
  53. class ProviderBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
  54. pk = forms.ModelMultipleChoiceField(
  55. queryset=Provider.objects.all(),
  56. widget=forms.MultipleHiddenInput
  57. )
  58. asn = forms.IntegerField(
  59. required=False,
  60. label='ASN'
  61. )
  62. account = forms.CharField(
  63. max_length=30,
  64. required=False,
  65. label='Account number'
  66. )
  67. portal_url = forms.URLField(
  68. required=False,
  69. label='Portal'
  70. )
  71. noc_contact = forms.CharField(
  72. required=False,
  73. widget=SmallTextarea,
  74. label='NOC contact'
  75. )
  76. admin_contact = forms.CharField(
  77. required=False,
  78. widget=SmallTextarea,
  79. label='Admin contact'
  80. )
  81. comments = CommentField(
  82. widget=SmallTextarea()
  83. )
  84. class Meta:
  85. nullable_fields = [
  86. 'asn', 'account', 'portal_url', 'noc_contact', 'admin_contact', 'comments',
  87. ]
  88. class ProviderFilterForm(BootstrapMixin, CustomFieldFilterForm):
  89. model = Provider
  90. q = forms.CharField(
  91. required=False,
  92. label='Search'
  93. )
  94. region = FilterChoiceField(
  95. queryset=Region.objects.all(),
  96. to_field_name='slug',
  97. required=False,
  98. widget=APISelectMultiple(
  99. api_url="/api/dcim/regions/",
  100. value_field="slug",
  101. filter_for={
  102. 'site': 'region'
  103. }
  104. )
  105. )
  106. site = FilterChoiceField(
  107. queryset=Site.objects.all(),
  108. to_field_name='slug',
  109. widget=APISelectMultiple(
  110. api_url="/api/dcim/sites/",
  111. value_field="slug",
  112. )
  113. )
  114. asn = forms.IntegerField(
  115. required=False,
  116. label='ASN'
  117. )
  118. #
  119. # Circuit types
  120. #
  121. class CircuitTypeForm(BootstrapMixin, forms.ModelForm):
  122. slug = SlugField()
  123. class Meta:
  124. model = CircuitType
  125. fields = [
  126. 'name', 'slug', 'description',
  127. ]
  128. class CircuitTypeCSVForm(forms.ModelForm):
  129. slug = SlugField()
  130. class Meta:
  131. model = CircuitType
  132. fields = CircuitType.csv_headers
  133. help_texts = {
  134. 'name': 'Name of circuit type',
  135. }
  136. #
  137. # Circuits
  138. #
  139. class CircuitForm(BootstrapMixin, TenancyForm, CustomFieldForm):
  140. comments = CommentField()
  141. tags = TagField(
  142. required=False
  143. )
  144. class Meta:
  145. model = Circuit
  146. fields = [
  147. 'cid', 'type', 'provider', 'status', 'install_date', 'commit_rate', 'description', 'tenant_group', 'tenant',
  148. 'comments', 'tags',
  149. ]
  150. help_texts = {
  151. 'cid': "Unique circuit ID",
  152. 'commit_rate': "Committed rate",
  153. }
  154. widgets = {
  155. 'provider': APISelect(
  156. api_url="/api/circuits/providers/"
  157. ),
  158. 'type': APISelect(
  159. api_url="/api/circuits/circuit-types/"
  160. ),
  161. 'status': StaticSelect2(),
  162. 'install_date': DatePicker(),
  163. }
  164. class CircuitCSVForm(forms.ModelForm):
  165. provider = forms.ModelChoiceField(
  166. queryset=Provider.objects.all(),
  167. to_field_name='name',
  168. help_text='Name of parent provider',
  169. error_messages={
  170. 'invalid_choice': 'Provider not found.'
  171. }
  172. )
  173. type = forms.ModelChoiceField(
  174. queryset=CircuitType.objects.all(),
  175. to_field_name='name',
  176. help_text='Type of circuit',
  177. error_messages={
  178. 'invalid_choice': 'Invalid circuit type.'
  179. }
  180. )
  181. status = CSVChoiceField(
  182. choices=CircuitStatusChoices,
  183. required=False,
  184. help_text='Operational status'
  185. )
  186. tenant = forms.ModelChoiceField(
  187. queryset=Tenant.objects.all(),
  188. required=False,
  189. to_field_name='name',
  190. help_text='Name of assigned tenant',
  191. error_messages={
  192. 'invalid_choice': 'Tenant not found.'
  193. }
  194. )
  195. class Meta:
  196. model = Circuit
  197. fields = [
  198. 'cid', 'provider', 'type', 'status', 'tenant', 'install_date', 'commit_rate', 'description', 'comments',
  199. ]
  200. class CircuitBulkEditForm(BootstrapMixin, AddRemoveTagsForm, CustomFieldBulkEditForm):
  201. pk = forms.ModelMultipleChoiceField(
  202. queryset=Circuit.objects.all(),
  203. widget=forms.MultipleHiddenInput
  204. )
  205. type = forms.ModelChoiceField(
  206. queryset=CircuitType.objects.all(),
  207. required=False,
  208. widget=APISelect(
  209. api_url="/api/circuits/circuit-types/"
  210. )
  211. )
  212. provider = forms.ModelChoiceField(
  213. queryset=Provider.objects.all(),
  214. required=False,
  215. widget=APISelect(
  216. api_url="/api/circuits/providers/"
  217. )
  218. )
  219. status = forms.ChoiceField(
  220. choices=add_blank_choice(CircuitStatusChoices),
  221. required=False,
  222. initial='',
  223. widget=StaticSelect2()
  224. )
  225. tenant = forms.ModelChoiceField(
  226. queryset=Tenant.objects.all(),
  227. required=False,
  228. widget=APISelect(
  229. api_url="/api/tenancy/tenants/"
  230. )
  231. )
  232. commit_rate = forms.IntegerField(
  233. required=False,
  234. label='Commit rate (Kbps)'
  235. )
  236. description = forms.CharField(
  237. max_length=100,
  238. required=False
  239. )
  240. comments = CommentField(
  241. widget=SmallTextarea,
  242. label='Comments'
  243. )
  244. class Meta:
  245. nullable_fields = [
  246. 'tenant', 'commit_rate', 'description', 'comments',
  247. ]
  248. class CircuitFilterForm(BootstrapMixin, TenancyFilterForm, CustomFieldFilterForm):
  249. model = Circuit
  250. field_order = [
  251. 'q', 'type', 'provider', 'status', 'region', 'site', 'tenant_group', 'tenant', 'commit_rate',
  252. ]
  253. q = forms.CharField(
  254. required=False,
  255. label='Search'
  256. )
  257. type = FilterChoiceField(
  258. queryset=CircuitType.objects.all(),
  259. to_field_name='slug',
  260. widget=APISelectMultiple(
  261. api_url="/api/circuits/circuit-types/",
  262. value_field="slug",
  263. )
  264. )
  265. provider = FilterChoiceField(
  266. queryset=Provider.objects.all(),
  267. to_field_name='slug',
  268. widget=APISelectMultiple(
  269. api_url="/api/circuits/providers/",
  270. value_field="slug",
  271. )
  272. )
  273. status = forms.MultipleChoiceField(
  274. choices=CircuitStatusChoices,
  275. required=False,
  276. widget=StaticSelect2Multiple()
  277. )
  278. region = forms.ModelMultipleChoiceField(
  279. queryset=Region.objects.all(),
  280. to_field_name='slug',
  281. required=False,
  282. widget=APISelectMultiple(
  283. api_url="/api/dcim/regions/",
  284. value_field="slug",
  285. filter_for={
  286. 'site': 'region'
  287. }
  288. )
  289. )
  290. site = FilterChoiceField(
  291. queryset=Site.objects.all(),
  292. to_field_name='slug',
  293. widget=APISelectMultiple(
  294. api_url="/api/dcim/sites/",
  295. value_field="slug",
  296. )
  297. )
  298. commit_rate = forms.IntegerField(
  299. required=False,
  300. min_value=0,
  301. label='Commit rate (Kbps)'
  302. )
  303. #
  304. # Circuit terminations
  305. #
  306. class CircuitTerminationForm(BootstrapMixin, forms.ModelForm):
  307. class Meta:
  308. model = CircuitTermination
  309. fields = [
  310. 'term_side', 'site', 'port_speed', 'upstream_speed', 'xconnect_id', 'pp_info', 'description',
  311. ]
  312. help_texts = {
  313. 'port_speed': "Physical circuit speed",
  314. 'xconnect_id': "ID of the local cross-connect",
  315. 'pp_info': "Patch panel ID and port number(s)"
  316. }
  317. widgets = {
  318. 'term_side': forms.HiddenInput(),
  319. 'site': APISelect(
  320. api_url="/api/dcim/sites/"
  321. )
  322. }