diff --git a/.github/workflows/ci.yml b/.github/workflows/ci.yml index 0514d24..e47de0a 100644 --- a/.github/workflows/ci.yml +++ b/.github/workflows/ci.yml @@ -1,13 +1,18 @@ name: CI on: + push: + branches: + - 'develop' + - 'master' pull_request: types: - opened + - synchronize - reopened branches: - 'master' concurrency: - group: ci-${{ github.actor }}-${{ github.ref }} + group: ci-${{ github.action }}-${{ github.ref }}-${{ github.actor }} cancel-in-progress: true permissions: contents: read diff --git a/netbox_lifecycle/constants/__init__.py b/netbox_lifecycle/constants/__init__.py new file mode 100644 index 0000000..3c457a6 --- /dev/null +++ b/netbox_lifecycle/constants/__init__.py @@ -0,0 +1,5 @@ +from netbox_lifecycle.constants.hardware import HARDWARE_LIFECYCLE_MODELS + +__all__ = ( + 'HARDWARE_LIFECYCLE_MODELS', +) diff --git a/netbox_lifecycle/constants/hardware.py b/netbox_lifecycle/constants/hardware.py new file mode 100644 index 0000000..9321c2f --- /dev/null +++ b/netbox_lifecycle/constants/hardware.py @@ -0,0 +1,7 @@ +from django.db.models import Q + +__all__ = ( + 'HARDWARE_LIFECYCLE_MODELS', +) + +HARDWARE_LIFECYCLE_MODELS = Q(app_label='dcim', model__in=('moduletype', 'devicetype',)) diff --git a/netbox_lifecycle/graphql/types.py b/netbox_lifecycle/graphql/types.py index 926d3ee..6379001 100644 --- a/netbox_lifecycle/graphql/types.py +++ b/netbox_lifecycle/graphql/types.py @@ -101,8 +101,8 @@ class HardwareLifecycleType(NetBoxObjectType): assigned_object_id: int assigned_object: Annotated[Union[ Annotated["DeviceType", strawberry.lazy('dcim.graphql.types')], - Annotated["ModuleType", strawberry.lazy('dcim.graphql.types')], - ], strawberry.union("HardwareLifecycleObjectTypes")] | None + Annotated["ModuleType", strawberry.lazy('dcim.graphql.types')],], + strawberry.union("HardwareLifecycleObjectTypes")] | None end_of_sale: str end_of_maintenance: str | None end_of_security: str | None diff --git a/netbox_lifecycle/migrations/0013_fix_hardware_lifecycle_model.py b/netbox_lifecycle/migrations/0013_fix_hardware_lifecycle_model.py new file mode 100644 index 0000000..bf635a4 --- /dev/null +++ b/netbox_lifecycle/migrations/0013_fix_hardware_lifecycle_model.py @@ -0,0 +1,29 @@ +# Generated by Django 5.0.8 on 2024-09-19 03:35 + +import django.db.models.deletion +from django.db import migrations, models + + +class Migration(migrations.Migration): + + dependencies = [ + ("contenttypes", "0002_remove_content_type_name"), + ("netbox_lifecycle", "0012_primarymodels"), + ] + + operations = [ + migrations.AlterField( + model_name="hardwarelifecycle", + name="assigned_object_type", + field=models.ForeignKey( + blank=True, + limit_choices_to=models.Q( + ("app_label", "dcim"), ("model__in", ("moduletype", "devicetype")) + ), + null=True, + on_delete=django.db.models.deletion.PROTECT, + related_name="+", + to="contenttypes.contenttype", + ), + ), + ] diff --git a/netbox_lifecycle/models/hardware.py b/netbox_lifecycle/models/hardware.py index 54868b5..1dce056 100644 --- a/netbox_lifecycle/models/hardware.py +++ b/netbox_lifecycle/models/hardware.py @@ -1,12 +1,13 @@ from django.contrib.contenttypes.fields import GenericForeignKey from django.contrib.contenttypes.models import ContentType from django.db import models -from django.db.models import Q from django.urls import reverse from dcim.models import DeviceType, ModuleType, Device, Module from netbox.models import PrimaryModel +from netbox_lifecycle.constants import HARDWARE_LIFECYCLE_MODELS + __all__ = ( 'HardwareLifecycle', @@ -16,7 +17,7 @@ class HardwareLifecycle(PrimaryModel): assigned_object_type = models.ForeignKey( to=ContentType, - limit_choices_to=Q(app_label='dcim', model__in=('moduletype', 'devicetype',)), + limit_choices_to=HARDWARE_LIFECYCLE_MODELS, on_delete=models.PROTECT, related_name='+', blank=True, diff --git a/netbox_lifecycle/tests/test_api.py b/netbox_lifecycle/tests/test_api.py index 28c4eab..94824d1 100644 --- a/netbox_lifecycle/tests/test_api.py +++ b/netbox_lifecycle/tests/test_api.py @@ -196,7 +196,6 @@ def setUpTestData(cls): ] SupportContract.objects.bulk_create(supportcontract) - cls.create_data = [ { 'contract_id': 'NB1000-4', @@ -299,10 +298,10 @@ def setUpTestData(cls): assigned_object=device_types[0], end_of_sale='2030-01-01', end_of_support='2030-01-01' ), HardwareLifecycle( - assigned_object=device_types[1], end_of_sale='2030-01-01', end_of_support='2040-01-01' + assigned_object=device_types[1], end_of_sale='2030-01-01', end_of_support='2040-01-01' ), HardwareLifecycle( - assigned_object=device_types[2], end_of_sale='2030-01-01', end_of_support='2050-01-01' + assigned_object=device_types[2], end_of_sale='2030-01-01', end_of_support='2050-01-01' ), ] HardwareLifecycle.objects.bulk_create(hardware_lifecycles) diff --git a/netbox_lifecycle/tests/test_filtersets.py b/netbox_lifecycle/tests/test_filtersets.py index b84aac1..498c53d 100644 --- a/netbox_lifecycle/tests/test_filtersets.py +++ b/netbox_lifecycle/tests/test_filtersets.py @@ -297,9 +297,23 @@ def setUpTestData(cls): HardwareLifecycle.objects.bulk_create(lifecycles) def test_q(self): - params = {'q': 'License 1'} + params = {'q': 'Device Type 1'} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 1) - def test_name(self): - params = {'name': ['License 1', 'License 2']} + def test_device_type(self): + assigned_objects = DeviceType.objects.all()[0:2] + + params = {'device_type_id': [assigned_objects[0].pk, assigned_objects[1].pk]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + params = {'device_type': [assigned_objects[0].model, assigned_objects[1].model]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + def test_module_type(self): + assigned_objects = ModuleType.objects.all()[0:2] + + params = {'module_type_id': [assigned_objects[0].pk, assigned_objects[1].pk]} + self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) + + params = {'module_type': [assigned_objects[0].model, assigned_objects[1].model]} self.assertEqual(self.filterset(params, self.queryset).qs.count(), 2) diff --git a/netbox_lifecycle/utilities/gfk_mixins.py b/netbox_lifecycle/utilities/gfk_mixins.py index 58ccaca..9a109df 100644 --- a/netbox_lifecycle/utilities/gfk_mixins.py +++ b/netbox_lifecycle/utilities/gfk_mixins.py @@ -21,4 +21,4 @@ def model_to_dict(self, instance, fields, api=False): model_dict[key] = str(value) elif key in ['device_type', 'module_type'] and isinstance(value, object): model_dict[key] = value.first().pk - return model_dict \ No newline at end of file + return model_dict diff --git a/setup.py b/setup.py index abbbf18..3e33760 100644 --- a/setup.py +++ b/setup.py @@ -2,7 +2,7 @@ setup( name='netbox-lifecycle', - version='1.1.2', + version='1.1.3-beta1', description='NetBox Lifecycle', long_description='NetBox Support Contract and EOL/EOS management', url='https://github.com/dansheps/netbox-lifecycle/',