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

Refactor maintenance task views to improve display of deleted components #3230 #3231

Merged
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
1 change: 1 addition & 0 deletions changelog.d/3228.fixed.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Improve description of deleted maintenance components
68 changes: 68 additions & 0 deletions python/nav/django/templatetags/maintenance.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,68 @@
#
# Copyright (C) 2024 Sikt
#
# This file is part of Network Administration Visualized (NAV).
#
# NAV is free software: you can redistribute it and/or modify it under
# the terms of the GNU General Public License version 3 as published by
# the Free Software Foundation.
#
# This program is distributed in the hope that it will be useful, but WITHOUT
# ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
# FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
# more details. You should have received a copy of the GNU General Public
# License along with NAV. If not, see <http://www.gnu.org/licenses/>.
#
"""Template tags for the maintenance tool"""
from django import template

from nav.web.maintenance.utils import MissingComponent

register = template.Library()


@register.filter
def model_verbose_name(model):
"""Returns the capitalized verbose name of a model"""
if not model:
return
name = model._meta.verbose_name
# Keep original capitalization, if any, otherwise apply our own
# e.g. don't turn "IP Device" into "Ip device", but do turn "room" into "Room"
return name if name[0].isupper() else name.capitalize()
lunkwill42 marked this conversation as resolved.
Show resolved Hide resolved


@register.filter
def component_name(component):
"""Returns an identifying name of a model object used as a maintenance component"""
if not component:
return ""
if hasattr(component, "sysname"):
return component.sysname
if hasattr(component, "handler"):
return component.handler
if isinstance(component, MissingComponent):
return str(component)
return component.pk


@register.filter
def component_description(component):
"""Returns a description of a component useful as a link title tooltip. Returns
an empty string if there is no further description than the component's name.
"""
if isinstance(component, MissingComponent):
return str(component)
if hasattr(component, "ip"):
return str(component.ip)
return getattr(component, "description", "")


@register.filter
def component_db_table(component):
"""Returns the database table name of a model object used as a maintenance
component.
"""
if not component:
return ""
return component._meta.db_table
5 changes: 3 additions & 2 deletions python/nav/models/fields.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,7 @@
import json
from datetime import datetime
from decimal import Decimal
from typing import Optional

from django import forms
from django.db import models
Expand Down Expand Up @@ -234,11 +235,11 @@ def __set__(self, instance, value):
setattr(instance, self.cache_attr, value)

@staticmethod
def get_model_name(obj):
def get_model_name(obj) -> str:
return obj._meta.db_table

@staticmethod
def get_model_class(table_name):
def get_model_class(table_name) -> Optional[models.Model]:
"""Returns a Model class based on a database table name"""
classmap = {model._meta.db_table: model for model in apps.get_models()}
if table_name in classmap:
Expand Down
7 changes: 5 additions & 2 deletions python/nav/models/manage.py
Original file line number Diff line number Diff line change
Expand Up @@ -282,8 +282,8 @@ class Netbox(models.Model):

class Meta(object):
db_table = 'netbox'
verbose_name = 'ip device'
verbose_name_plural = 'ip devices'
verbose_name = 'IP Device'
verbose_name_plural = 'IP Devices'
ordering = ('sysname',)

def __str__(self):
Expand Down Expand Up @@ -1155,6 +1155,9 @@ def get_all_rooms(self):
locations = self.get_descendants(True)
return Room.objects.filter(location__in=locations)

def get_absolute_url(self):
return reverse('location-info', kwargs={'locationid': self.pk})


class Organization(models.Model, TreeMixin):
"""From NAV Wiki: The org table defines an organization which is in charge
Expand Down
5 changes: 5 additions & 0 deletions python/nav/models/msgmaint.py
Original file line number Diff line number Diff line change
Expand Up @@ -181,6 +181,7 @@ class MaintenanceComponent(models.Model):
)
key = VarcharField()
value = VarcharField()
description = VarcharField(null=True, blank=True)
component = LegacyGenericForeignKey('key', 'value')

class Meta(object):
Expand All @@ -190,6 +191,10 @@ class Meta(object):
def __str__(self):
return u'%s=%s' % (self.key, self.value)

def get_component_class(self) -> models.Model:
"""Returns a Model class based on the database table name stored in key"""
return LegacyGenericForeignKey.get_model_class(self.key)


class MessageToMaintenanceTask(models.Model):
"""From NAV Wiki: The connection between messages and related maintenance
Expand Down
13 changes: 13 additions & 0 deletions python/nav/models/sql/changes/sc.05.12.0010.sql
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
-- Add column to maint_component table to keep descriptions of components that can no longer referenced
ALTER TABLE maint_component ADD COLUMN description VARCHAR;

UPDATE maint_component c
SET description = n.sysname
FROM netbox n
WHERE c.key = 'netbox' AND c.value = n.netboxid::text;

UPDATE maint_component c
SET description = s.handler || ' at ' || n.sysname
FROM service s
JOIN netbox n ON s.netboxid = n.netboxid
WHERE c.key = 'service' AND c.value = s.serviceid::text;
Loading
Loading