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

Fix hardware lifecycle missing migration #39

Merged
merged 5 commits into from
Sep 19, 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
7 changes: 6 additions & 1 deletion .github/workflows/ci.yml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
5 changes: 5 additions & 0 deletions netbox_lifecycle/constants/__init__.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@
from netbox_lifecycle.constants.hardware import HARDWARE_LIFECYCLE_MODELS

__all__ = (
'HARDWARE_LIFECYCLE_MODELS',
)
7 changes: 7 additions & 0 deletions netbox_lifecycle/constants/hardware.py
Original file line number Diff line number Diff line change
@@ -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',))
4 changes: 2 additions & 2 deletions netbox_lifecycle/graphql/types.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
29 changes: 29 additions & 0 deletions netbox_lifecycle/migrations/0013_fix_hardware_lifecycle_model.py
Original file line number Diff line number Diff line change
@@ -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",
),
),
]
5 changes: 3 additions & 2 deletions netbox_lifecycle/models/hardware.py
Original file line number Diff line number Diff line change
@@ -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',
Expand All @@ -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,
Expand Down
5 changes: 2 additions & 3 deletions netbox_lifecycle/tests/test_api.py
Original file line number Diff line number Diff line change
Expand Up @@ -196,7 +196,6 @@ def setUpTestData(cls):
]
SupportContract.objects.bulk_create(supportcontract)


cls.create_data = [
{
'contract_id': 'NB1000-4',
Expand Down Expand Up @@ -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)
Expand Down
20 changes: 17 additions & 3 deletions netbox_lifecycle/tests/test_filtersets.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
2 changes: 1 addition & 1 deletion netbox_lifecycle/utilities/gfk_mixins.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
return model_dict
2 changes: 1 addition & 1 deletion setup.py
Original file line number Diff line number Diff line change
Expand Up @@ -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/',
Expand Down
Loading