Skip to content

Commit

Permalink
#9047 - Fully resolve test issues. Move model to look more like reque…
Browse files Browse the repository at this point in the history
…sted initial model.
  • Loading branch information
DanSheps committed Mar 22, 2023
1 parent 9e129b0 commit 22fdd61
Show file tree
Hide file tree
Showing 8 changed files with 54 additions and 36 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -51,22 +51,22 @@ class Migration(migrations.Migration):
('custom_field_data', models.JSONField(blank=True, default=dict, encoder=utilities.json.CustomFieldJSONEncoder)),
('description', models.CharField(blank=True, max_length=200)),
('comments', models.TextField(blank=True)),
('name', models.CharField(max_length=100)),
('account', models.CharField(blank=True, max_length=30)),
('account', models.CharField(max_length=30)),
('name', models.CharField(blank=True, max_length=100)),
('provider', models.ForeignKey(on_delete=django.db.models.deletion.PROTECT, related_name='accounts', to='circuits.provider')),
('tags', taggit.managers.TaggableManager(through='extras.TaggedItem', to='extras.Tag')),
],
options={
'ordering': ('provider', 'name'),
'ordering': ('provider', 'account'),
},
),
migrations.AddConstraint(
model_name='provideraccount',
constraint=models.UniqueConstraint(fields=('provider', 'name'), name='circuits_provideraccount_unique_provider_name'),
constraint=models.UniqueConstraint(condition=models.Q(('account', ''), _negated=True), fields=('provider', 'name'), name='circuits_provideraccount_unique_provider_name'),
),
migrations.AddConstraint(
model_name='provideraccount',
constraint=models.UniqueConstraint(condition=models.Q(('account', ''), _negated=True), fields=('provider', 'account'), name='circuits_provideraccount_unique_provider_account'),
constraint=models.UniqueConstraint(fields=('provider', 'account'), name='circuits_provideraccount_unique_provider_account'),
),
migrations.RunPython(
create_provideraccounts_from_providers, revert_provideraccounts_from_providers
Expand Down
25 changes: 13 additions & 12 deletions netbox/circuits/models/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -52,19 +52,19 @@ class ProviderAccount(PrimaryModel):
"""
This represents a provider account
"""
account = models.CharField(
max_length=30,
verbose_name='Account number'
)
name = models.CharField(
max_length=100
max_length=100,
blank=True
)
provider = models.ForeignKey(
to='circuits.Provider',
on_delete=models.PROTECT,
related_name='accounts'
)
account = models.CharField(
max_length=30,
blank=True,
verbose_name='Account number'
)

# Generic relations
contacts = GenericRelation(
Expand All @@ -74,21 +74,22 @@ class ProviderAccount(PrimaryModel):
clone_fields = ('provider', )

class Meta:
ordering = ('provider', 'name')
ordering = ('provider', 'account')
constraints = (
models.UniqueConstraint(
fields=('provider', 'name'),
name='%(app_label)s_%(class)s_unique_provider_name'
fields=('provider', 'account'),
name='%(app_label)s_%(class)s_unique_provider_account'
),
models.UniqueConstraint(
fields=('provider', 'account'),
name='%(app_label)s_%(class)s_unique_provider_account',
fields=('provider', 'name'),
name='%(app_label)s_%(class)s_unique_provider_name',
condition=~Q(account="")
),
)

def __str__(self):
return self.name
if self.name:
return f'{self.account} ({self.name})'

def get_absolute_url(self):
return reverse('circuits:provideraccount', args=[self.pk])
Expand Down
4 changes: 2 additions & 2 deletions netbox/circuits/tables/providers.py
Original file line number Diff line number Diff line change
Expand Up @@ -72,9 +72,9 @@ class ProviderAccountTable(ContactsColumnMixin, NetBoxTable):
class Meta(NetBoxTable.Meta):
model = ProviderAccount
fields = (
'pk', 'id', 'name', 'account', 'provider', 'comments', 'contacts', 'tags', 'created', 'last_updated',
'pk', 'id', 'account', 'name', 'provider', 'comments', 'contacts', 'tags', 'created', 'last_updated',
)
default_columns = ('pk', 'name', 'account', 'provider')
default_columns = ('pk', 'account', 'name', 'provider')


class ProviderNetworkTable(NetBoxTable):
Expand Down
9 changes: 6 additions & 3 deletions netbox/circuits/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -210,24 +210,27 @@ def setUpTestData(cls):
Provider.objects.bulk_create(providers)

provider_accounts = (
ProviderAccount(name='Provider Account 1', provider=providers[0]),
ProviderAccount(name='Provider Account 2', provider=providers[0]),
ProviderAccount(name='Provider Account 3', provider=providers[0]),
ProviderAccount(name='Provider Account 1', provider=providers[0], account='1234'),
ProviderAccount(name='Provider Account 2', provider=providers[0], account='2345'),
ProviderAccount(name='Provider Account 3', provider=providers[0], account='3456'),
)
ProviderAccount.objects.bulk_create(provider_accounts)

cls.create_data = [
{
'name': 'Provider Account 4',
'provider': providers[0].pk,
'account': '4567',
},
{
'name': 'Provider Account 5',
'provider': providers[0].pk,
'account': '5678',
},
{
'name': 'Provider Account 6',
'provider': providers[0].pk,
'account': '6789',
},
]

Expand Down
10 changes: 7 additions & 3 deletions netbox/circuits/tests/test_filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -458,16 +458,20 @@ def setUpTestData(cls):
Provider.objects.bulk_create(providers)

provider_accounts = (
ProviderAccount(name='Provider Account 1', provider=providers[0], description='foobar1'),
ProviderAccount(name='Provider Account 2', provider=providers[1], description='foobar2'),
ProviderAccount(name='Provider Account 3', provider=providers[2]),
ProviderAccount(name='Provider Account 1', provider=providers[0], description='foobar1', account='1234'),
ProviderAccount(name='Provider Account 2', provider=providers[1], description='foobar2', account='2345'),
ProviderAccount(name='Provider Account 3', provider=providers[2], account='3456'),
)
ProviderAccount.objects.bulk_create(provider_accounts)

def test_name(self):
params = {'name': ['Provider Account 1', 'Provider Account 2']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)

def test_account(self):
params = {'account': ['1234', '3456']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)

def test_description(self):
params = {'description': ['foobar1', 'foobar2']}
self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2)
Expand Down
23 changes: 12 additions & 11 deletions netbox/circuits/tests/test_views.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,9 +190,9 @@ def setUpTestData(cls):
Provider.objects.bulk_create(providers)

provider_accounts = (
ProviderAccount(name='Provider Account 1', provider=providers[0]),
ProviderAccount(name='Provider Account 2', provider=providers[0]),
ProviderAccount(name='Provider Account 3', provider=providers[0]),
ProviderAccount(name='Provider Account 1', provider=providers[0], account='1234'),
ProviderAccount(name='Provider Account 2', provider=providers[0], account='2345'),
ProviderAccount(name='Provider Account 3', provider=providers[0], account='3456'),
)

ProviderAccount.objects.bulk_create(provider_accounts)
Expand All @@ -202,23 +202,24 @@ def setUpTestData(cls):
cls.form_data = {
'name': 'Provider Account X',
'provider': providers[1].pk,
'account': 'XXXX',
'description': 'A new provider network',
'comments': 'Longer description goes here',
'tags': [t.pk for t in tags],
}

cls.csv_data = (
"name,provider,description",
"Provider Account 4,Provider 1,Foo",
"Provider Account 5,Provider 1,Bar",
"Provider Account 6,Provider 1,Baz",
"name,provider,account,description",
"Provider Account 4,Provider 1,4567,Foo",
"Provider Account 5,Provider 1,5678,Bar",
"Provider Account 6,Provider 1,6789,Baz",
)

cls.csv_update_data = (
"id,name,description",
f"{provider_accounts[0].pk},Provider Network 7,New description7",
f"{provider_accounts[1].pk},Provider Network 8,New description8",
f"{provider_accounts[2].pk},Provider Network 9,New description9",
"id,name,account,description",
f"{provider_accounts[0].pk},Provider Network 7,7890,New description7",
f"{provider_accounts[1].pk},Provider Network 8,8901,New description8",
f"{provider_accounts[2].pk},Provider Network 9,9012,New description9",
)

cls.bulk_edit_data = {
Expand Down
1 change: 1 addition & 0 deletions netbox/circuits/views.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@ class ProviderView(generic.ObjectView):

def get_extra_context(self, request, instance):
related_models = (
(ProviderAccount.objects.restrict(request.user, 'view').filter(provider=instance), 'provider_id'),
(Circuit.objects.restrict(request.user, 'view').filter(provider=instance), 'provider_id'),
)

Expand Down
8 changes: 8 additions & 0 deletions netbox/templates/circuits/provider.html
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,14 @@ <h5 class="card-header">Provider</h5>
</div>
</div>
<div class="row mb-3">
<div class="col col-md-12">
<div class="card">
<h5 class="card-header">Provider Accounts</h5>
<div class="card-body htmx-container table-responsive"
hx-get="{% url 'circuits:provideraccount_list' %}?provider_id={{ object.pk }}"
hx-trigger="load"
></div>
</div>
<div class="col col-md-12">
<div class="card">
<h5 class="card-header">Circuits</h5>
Expand Down

0 comments on commit 22fdd61

Please sign in to comment.