Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

replace GenericRelation fields with appropriate model mixins #155

Merged
merged 4 commits into from
Apr 18, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion .github/workflows/test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@ jobs:
strategy:
fail-fast: false
matrix:
netbox-version: ["v3.7.2"]
netbox-version: ["v3.7.5"]
services:
redis:
image: redis
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ The idea is that an external system uses some assets stored in netbox_inventory,

## Compatibility

This plugin requires netbox version 3.5.x to work. Older versions of the plugin
This plugin requires netbox version 3.7.x to work. Older versions of the plugin
support older netbox version as per table below:

| NetBox Version | Plugin Version |
Expand All @@ -65,6 +65,7 @@ support older netbox version as per table below:
| 3.4 | 1.2.x |
| 3.5 | 1.3.x & 1.4.x |
| 3.6 | 1.5.x |
| 3.7 | 1.6.x |

## Installing

Expand Down
2 changes: 1 addition & 1 deletion netbox_inventory/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,7 @@ class NetBoxInventoryConfig(PluginConfig):
author = 'Matej Vadnjal'
author_email = '[email protected]'
base_url = 'inventory'
min_version = '3.6.0'
min_version = '3.7.0'
default_settings = {
'top_level_menu': True,
'used_status_name': 'used',
Expand Down
13 changes: 3 additions & 10 deletions netbox_inventory/models.py
Original file line number Diff line number Diff line change
@@ -1,12 +1,11 @@
from datetime import date

from django.contrib.contenttypes.fields import GenericRelation
from django.db import models
from django.forms import ValidationError
from django.urls import reverse

from netbox.models import NetBoxModel, NestedGroupModel
from netbox.models.features import ImageAttachmentsMixin
from netbox.models.features import ContactsMixin, ImageAttachmentsMixin
from .choices import HardwareKindChoices, AssetStatusChoices, PurchaseStatusChoices
from .utils import asset_clear_old_hw, asset_set_new_hw, get_prechange_field, get_plugin_setting, get_status_for

Expand Down Expand Up @@ -385,7 +384,7 @@ class Meta:
)


class Supplier(NetBoxModel):
class Supplier(NetBoxModel, ContactsMixin):
"""
Supplier is a legal entity that sold some assets that we keep track of.
This can be the same entity as Manufacturer or a separate one. However
Expand All @@ -403,9 +402,6 @@ class Supplier(NetBoxModel):
max_length=200,
blank=True
)
contacts = GenericRelation(
to='tenancy.ContactAssignment'
)
comments = models.TextField(
blank=True
)
Expand Down Expand Up @@ -533,7 +529,7 @@ def get_absolute_url(self):
return reverse('plugins:netbox_inventory:delivery', args=[self.pk])


class InventoryItemType(NetBoxModel):
class InventoryItemType(NetBoxModel, ImageAttachmentsMixin):
"""
Inventory Item Type is a model (make, part number) of an Inventory Item. In
that it is simmilar to Device Type or Module Type.
Expand Down Expand Up @@ -566,9 +562,6 @@ class InventoryItemType(NetBoxModel):
comments = models.TextField(
blank=True
)
images = GenericRelation(
to='extras.ImageAttachment'
)

clone_fields = [
'manufacturer',
Expand Down
92 changes: 91 additions & 1 deletion netbox_inventory/tests/supplier/test_views.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,8 @@
from utilities.testing import ViewTestCases
from django.contrib.contenttypes.models import ContentType
from django.urls import reverse
from utilities.testing import ViewTestCases, create_tags
from tenancy.choices import ContactPriorityChoices
from tenancy.models import Contact, ContactRole, ContactAssignment

from netbox_inventory.tests.custom import ModelViewTestCase
from netbox_inventory.models import Supplier
Expand Down Expand Up @@ -46,3 +50,89 @@ def setUpTestData(cls):
f'{supplier2.pk},description 2',
f'{supplier3.pk},description 3',
)


class ContactAssignmentTestCase(
ViewTestCases.CreateObjectViewTestCase,
ViewTestCases.EditObjectViewTestCase,
ViewTestCases.DeleteObjectViewTestCase,
ViewTestCases.ListObjectsViewTestCase,
ViewTestCases.BulkEditObjectsViewTestCase,
ViewTestCases.BulkDeleteObjectsViewTestCase
):
model = ContactAssignment

@classmethod
def setUpTestData(cls):

suppliers = (
Supplier(name='Supplier 1', slug='supplier-1'),
Supplier(name='Supplier 2', slug='supplier-2'),
Supplier(name='Supplier 3', slug='supplier-3'),
Supplier(name='Supplier 4', slug='supplier-4'),
)
Supplier.objects.bulk_create(suppliers)

contacts = (
Contact(name='Contact 1'),
Contact(name='Contact 2'),
Contact(name='Contact 3'),
Contact(name='Contact 4'),
)
Contact.objects.bulk_create(contacts)

contact_roles = (
ContactRole(name='Contact Role 1', slug='contact-role-1'),
ContactRole(name='Contact Role 2', slug='contact-role-2'),
ContactRole(name='Contact Role 3', slug='contact-role-3'),
ContactRole(name='Contact Role 4', slug='contact-role-4'),
)
ContactRole.objects.bulk_create(contact_roles)

assignments = (
ContactAssignment(
object=suppliers[0],
contact=contacts[0],
role=contact_roles[0],
priority=ContactPriorityChoices.PRIORITY_PRIMARY
),
ContactAssignment(
object=suppliers[1],
contact=contacts[1],
role=contact_roles[1],
priority=ContactPriorityChoices.PRIORITY_SECONDARY
),
ContactAssignment(
object=suppliers[2],
contact=contacts[2],
role=contact_roles[2],
priority=ContactPriorityChoices.PRIORITY_TERTIARY
),
)
ContactAssignment.objects.bulk_create(assignments)

tags = create_tags('Alpha', 'Bravo', 'Charlie')

cls.form_data = {
'content_type': ContentType.objects.get_for_model(Supplier).pk,
'object_id': suppliers[3].pk,
'contact': contacts[3].pk,
'role': contact_roles[3].pk,
'priority': ContactPriorityChoices.PRIORITY_INACTIVE,
'tags': [t.pk for t in tags],
}

cls.bulk_edit_data = {
'role': contact_roles[3].pk,
'priority': ContactPriorityChoices.PRIORITY_INACTIVE,
}

def _get_url(self, action, instance=None):
# Override creation URL to append content_type & object_id parameters
if action == 'add':
url = reverse('tenancy:contactassignment_add')
content_type = ContentType.objects.get_for_model(Supplier).pk
object_id = Supplier.objects.first().pk
return f"{url}?content_type={content_type}&object_id={object_id}"

return super()._get_url(action, instance=instance)
4 changes: 2 additions & 2 deletions netbox_inventory/tests/test_load.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,13 +5,13 @@
from netbox_inventory.tests.custom import APITestCase


class NetboxDnsVersionTestCase(SimpleTestCase):
class NetboxInventoryVersionTestCase(SimpleTestCase):
"""
Test for netbox_inventory package
"""

def test_version(self):
assert __version__ == "1.5.2"
assert __version__ == "1.6.0"


class AppTest(APITestCase):
Expand Down
2 changes: 1 addition & 1 deletion netbox_inventory/version.py
Original file line number Diff line number Diff line change
@@ -1 +1 @@
__version__ = '1.5.2'
__version__ = '1.6.0'
2 changes: 1 addition & 1 deletion pyproject.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ build-backend = "setuptools.build_meta"

[project]
name = "netbox-inventory"
version = "1.5.2"
version = "1.6.0"
authors = [
{ name="Matej Vadnjal", email="[email protected]" },
]
Expand Down