providers.py 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  1. from django.contrib.contenttypes.fields import GenericRelation
  2. from django.db import models
  3. from django.db.models import Q
  4. from django.urls import reverse
  5. from django.utils.translation import gettext_lazy as _
  6. from netbox.models import PrimaryModel
  7. __all__ = (
  8. 'ProviderNetwork',
  9. 'Provider',
  10. 'ProviderAccount',
  11. )
  12. class Provider(PrimaryModel):
  13. """
  14. Each Circuit belongs to a Provider. This is usually a telecommunications company or similar organization. This model
  15. stores information pertinent to the user's relationship with the Provider.
  16. """
  17. name = models.CharField(
  18. verbose_name=_('name'),
  19. max_length=100,
  20. unique=True,
  21. help_text=_('Full name of the provider')
  22. )
  23. slug = models.SlugField(
  24. verbose_name=_('slug'),
  25. max_length=100,
  26. unique=True
  27. )
  28. asns = models.ManyToManyField(
  29. to='ipam.ASN',
  30. related_name='providers',
  31. blank=True
  32. )
  33. # Generic relations
  34. contacts = GenericRelation(
  35. to='tenancy.ContactAssignment'
  36. )
  37. clone_fields = ()
  38. class Meta:
  39. ordering = ['name']
  40. verbose_name = _('provider')
  41. verbose_name_plural = _('providers')
  42. def __str__(self):
  43. return self.name
  44. def get_absolute_url(self):
  45. return reverse('circuits:provider', args=[self.pk])
  46. class ProviderAccount(PrimaryModel):
  47. """
  48. This is a discrete account within a provider. Each Circuit belongs to a Provider Account.
  49. """
  50. provider = models.ForeignKey(
  51. to='circuits.Provider',
  52. on_delete=models.PROTECT,
  53. related_name='accounts'
  54. )
  55. account = models.CharField(
  56. max_length=100,
  57. verbose_name=_('account ID')
  58. )
  59. name = models.CharField(
  60. verbose_name=_('name'),
  61. max_length=100,
  62. blank=True
  63. )
  64. # Generic relations
  65. contacts = GenericRelation(
  66. to='tenancy.ContactAssignment'
  67. )
  68. clone_fields = ('provider', )
  69. class Meta:
  70. ordering = ('provider', 'account')
  71. constraints = (
  72. models.UniqueConstraint(
  73. fields=('provider', 'account'),
  74. name='%(app_label)s_%(class)s_unique_provider_account'
  75. ),
  76. models.UniqueConstraint(
  77. fields=('provider', 'name'),
  78. name='%(app_label)s_%(class)s_unique_provider_name',
  79. condition=~Q(name="")
  80. ),
  81. )
  82. verbose_name = _('provider account')
  83. verbose_name_plural = _('provider accounts')
  84. def __str__(self):
  85. if self.name:
  86. return f'{self.account} ({self.name})'
  87. return f'{self.account}'
  88. def get_absolute_url(self):
  89. return reverse('circuits:provideraccount', args=[self.pk])
  90. class ProviderNetwork(PrimaryModel):
  91. """
  92. This represents a provider network which exists outside of NetBox, the details of which are unknown or
  93. unimportant to the user.
  94. """
  95. name = models.CharField(
  96. verbose_name=_('name'),
  97. max_length=100
  98. )
  99. provider = models.ForeignKey(
  100. to='circuits.Provider',
  101. on_delete=models.PROTECT,
  102. related_name='networks'
  103. )
  104. service_id = models.CharField(
  105. max_length=100,
  106. blank=True,
  107. verbose_name=_('service ID')
  108. )
  109. class Meta:
  110. ordering = ('provider', 'name')
  111. constraints = (
  112. models.UniqueConstraint(
  113. fields=('provider', 'name'),
  114. name='%(app_label)s_%(class)s_unique_provider_name'
  115. ),
  116. )
  117. verbose_name = _('provider network')
  118. verbose_name_plural = _('provider networks')
  119. def __str__(self):
  120. return self.name
  121. def get_absolute_url(self):
  122. return reverse('circuits:providernetwork', args=[self.pk])