models.py 5.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. from django import forms
  2. from circuits.models import *
  3. from dcim.models import Region, Site, SiteGroup
  4. from extras.forms import CustomFieldModelForm
  5. from extras.models import Tag
  6. from tenancy.forms import TenancyForm
  7. from utilities.forms import (
  8. BootstrapMixin, CommentField, DatePicker, DynamicModelChoiceField, DynamicModelMultipleChoiceField,
  9. SelectSpeedWidget, SmallTextarea, SlugField, StaticSelect,
  10. )
  11. __all__ = (
  12. 'CircuitForm',
  13. 'CircuitTerminationForm',
  14. 'CircuitTypeForm',
  15. 'ProviderForm',
  16. 'ProviderNetworkForm',
  17. )
  18. class ProviderForm(CustomFieldModelForm):
  19. slug = SlugField()
  20. comments = CommentField()
  21. tags = DynamicModelMultipleChoiceField(
  22. queryset=Tag.objects.all(),
  23. required=False
  24. )
  25. class Meta:
  26. model = Provider
  27. fields = [
  28. 'name', 'slug', 'asn', 'account', 'portal_url', 'noc_contact', 'admin_contact', 'comments', 'tags',
  29. ]
  30. fieldsets = (
  31. ('Provider', ('name', 'slug', 'asn', 'tags')),
  32. ('Support Info', ('account', 'portal_url', 'noc_contact', 'admin_contact')),
  33. )
  34. widgets = {
  35. 'noc_contact': SmallTextarea(
  36. attrs={'rows': 5}
  37. ),
  38. 'admin_contact': SmallTextarea(
  39. attrs={'rows': 5}
  40. ),
  41. }
  42. help_texts = {
  43. 'name': "Full name of the provider",
  44. 'asn': "BGP autonomous system number (if applicable)",
  45. 'portal_url': "URL of the provider's customer support portal",
  46. 'noc_contact': "NOC email address and phone number",
  47. 'admin_contact': "Administrative contact email address and phone number",
  48. }
  49. class ProviderNetworkForm(CustomFieldModelForm):
  50. provider = DynamicModelChoiceField(
  51. queryset=Provider.objects.all()
  52. )
  53. comments = CommentField()
  54. tags = DynamicModelMultipleChoiceField(
  55. queryset=Tag.objects.all(),
  56. required=False
  57. )
  58. class Meta:
  59. model = ProviderNetwork
  60. fields = [
  61. 'provider', 'name', 'description', 'comments', 'tags',
  62. ]
  63. fieldsets = (
  64. ('Provider Network', ('provider', 'name', 'description', 'tags')),
  65. )
  66. class CircuitTypeForm(CustomFieldModelForm):
  67. slug = SlugField()
  68. tags = DynamicModelMultipleChoiceField(
  69. queryset=Tag.objects.all(),
  70. required=False
  71. )
  72. class Meta:
  73. model = CircuitType
  74. fields = [
  75. 'name', 'slug', 'description', 'tags',
  76. ]
  77. class CircuitForm(TenancyForm, CustomFieldModelForm):
  78. provider = DynamicModelChoiceField(
  79. queryset=Provider.objects.all()
  80. )
  81. type = DynamicModelChoiceField(
  82. queryset=CircuitType.objects.all()
  83. )
  84. comments = CommentField()
  85. tags = DynamicModelMultipleChoiceField(
  86. queryset=Tag.objects.all(),
  87. required=False
  88. )
  89. class Meta:
  90. model = Circuit
  91. fields = [
  92. 'cid', 'type', 'provider', 'status', 'install_date', 'commit_rate', 'description', 'tenant_group', 'tenant',
  93. 'comments', 'tags',
  94. ]
  95. fieldsets = (
  96. ('Circuit', ('provider', 'cid', 'type', 'status', 'install_date', 'commit_rate', 'description', 'tags')),
  97. ('Tenancy', ('tenant_group', 'tenant')),
  98. )
  99. help_texts = {
  100. 'cid': "Unique circuit ID",
  101. 'commit_rate': "Committed rate",
  102. }
  103. widgets = {
  104. 'status': StaticSelect(),
  105. 'install_date': DatePicker(),
  106. 'commit_rate': SelectSpeedWidget(),
  107. }
  108. class CircuitTerminationForm(BootstrapMixin, forms.ModelForm):
  109. region = DynamicModelChoiceField(
  110. queryset=Region.objects.all(),
  111. required=False,
  112. initial_params={
  113. 'sites': '$site'
  114. }
  115. )
  116. site_group = DynamicModelChoiceField(
  117. queryset=SiteGroup.objects.all(),
  118. required=False,
  119. initial_params={
  120. 'sites': '$site'
  121. }
  122. )
  123. site = DynamicModelChoiceField(
  124. queryset=Site.objects.all(),
  125. query_params={
  126. 'region_id': '$region',
  127. 'group_id': '$site_group',
  128. },
  129. required=False
  130. )
  131. provider_network = DynamicModelChoiceField(
  132. queryset=ProviderNetwork.objects.all(),
  133. required=False
  134. )
  135. class Meta:
  136. model = CircuitTermination
  137. fields = [
  138. 'term_side', 'region', 'site_group', 'site', 'provider_network', 'mark_connected', 'port_speed',
  139. 'upstream_speed', 'xconnect_id', 'pp_info', 'description',
  140. ]
  141. help_texts = {
  142. 'port_speed': "Physical circuit speed",
  143. 'xconnect_id': "ID of the local cross-connect",
  144. 'pp_info': "Patch panel ID and port number(s)"
  145. }
  146. widgets = {
  147. 'term_side': forms.HiddenInput(),
  148. 'port_speed': SelectSpeedWidget(),
  149. 'upstream_speed': SelectSpeedWidget(),
  150. }
  151. def __init__(self, *args, **kwargs):
  152. super().__init__(*args, **kwargs)
  153. self.fields['provider_network'].widget.add_query_param('provider_id', self.instance.circuit.provider_id)