Coverage for circuits/models/providers.py: 84%

42 statements  

« prev     ^ index     » next       coverage.py v7.15.2, created at 2026-10-10 18:35 +0000

1from django.db import models 

2from django.db.models import Q 

3from django.utils.translation import gettext_lazy as _ 

4 

5from netbox.models import PrimaryModel 

6from netbox.models.features import ContactsMixin 

7 

8__all__ = ( 

9 'Provider', 

10 'ProviderAccount', 

11 'ProviderNetwork', 

12) 

13 

14 

15class Provider(ContactsMixin, PrimaryModel): 

16 """ 

17 Each Circuit belongs to a Provider. This is usually a telecommunications company or similar organization. This model 

18 stores information pertinent to the user's relationship with the Provider. 

19 """ 

20 name = models.CharField( 

21 verbose_name=_('name'), 

22 max_length=100, 

23 unique=True, 

24 help_text=_('Full name of the provider'), 

25 db_collation="natural_sort" 

26 ) 

27 slug = models.SlugField( 

28 verbose_name=_('slug'), 

29 max_length=100, 

30 unique=True 

31 ) 

32 asns = models.ManyToManyField( 

33 to='ipam.ASN', 

34 related_name='providers', 

35 blank=True 

36 ) 

37 

38 clone_fields = () 

39 

40 class Meta: 

41 ordering = ['name'] 

42 verbose_name = _('provider') 

43 verbose_name_plural = _('providers') 

44 

45 def __str__(self): 

46 return self.name 

47 

48 

49class ProviderAccount(ContactsMixin, PrimaryModel): 

50 """ 

51 This is a discrete account within a provider. Each Circuit belongs to a Provider Account. 

52 """ 

53 provider = models.ForeignKey( 

54 to='circuits.Provider', 

55 on_delete=models.PROTECT, 

56 related_name='accounts' 

57 ) 

58 account = models.CharField( 

59 max_length=100, 

60 verbose_name=_('account ID') 

61 ) 

62 name = models.CharField( 

63 verbose_name=_('name'), 

64 max_length=100, 

65 blank=True 

66 ) 

67 

68 clone_fields = ('provider', ) 

69 

70 class Meta: 

71 ordering = ('provider', 'account') 

72 constraints = ( 

73 models.UniqueConstraint( 

74 fields=('provider', 'account'), 

75 name='%(app_label)s_%(class)s_unique_provider_account' 

76 ), 

77 models.UniqueConstraint( 

78 fields=('provider', 'name'), 

79 name='%(app_label)s_%(class)s_unique_provider_name', 

80 condition=~Q(name="") 

81 ), 

82 ) 

83 verbose_name = _('provider account') 

84 verbose_name_plural = _('provider accounts') 

85 

86 def __str__(self): 

87 if self.name: 

88 return f'{self.account} ({self.name})' 

89 return f'{self.account}' 

90 

91 

92class ProviderNetwork(PrimaryModel): 

93 """ 

94 This represents a provider network which exists outside of NetBox, the details of which are unknown or 

95 unimportant to the user. 

96 """ 

97 name = models.CharField( 

98 verbose_name=_('name'), 

99 max_length=100, 

100 db_collation="natural_sort" 

101 ) 

102 provider = models.ForeignKey( 

103 to='circuits.Provider', 

104 on_delete=models.PROTECT, 

105 related_name='networks' 

106 ) 

107 service_id = models.CharField( 

108 max_length=100, 

109 blank=True, 

110 verbose_name=_('service ID') 

111 ) 

112 

113 class Meta: 

114 ordering = ('provider', 'name') 

115 constraints = ( 

116 models.UniqueConstraint( 

117 fields=('provider', 'name'), 

118 name='%(app_label)s_%(class)s_unique_provider_name' 

119 ), 

120 ) 

121 verbose_name = _('provider network') 

122 verbose_name_plural = _('provider networks') 

123 

124 def __str__(self): 

125 return self.name