From a53208faa68016bb0446fff3df30845c657a60d0 Mon Sep 17 00:00:00 2001 From: "Jamie (Bear) Murphy" <1613241+ITJamie@users.noreply.github.com> Date: Mon, 28 Oct 2024 21:09:42 +0000 Subject: [PATCH] changelog extension model v1 --- netbox_changelog_diff_plugin/commands.py | 47 ++++++++++++++++++++++++ netbox_changelog_diff_plugin/models.py | 24 ++++++++++++ 2 files changed, 71 insertions(+) create mode 100644 netbox_changelog_diff_plugin/commands.py diff --git a/netbox_changelog_diff_plugin/commands.py b/netbox_changelog_diff_plugin/commands.py new file mode 100644 index 0000000..2238b37 --- /dev/null +++ b/netbox_changelog_diff_plugin/commands.py @@ -0,0 +1,47 @@ +from django.core.management.base import BaseCommand +from extras.models import ObjectChange +from .models import ChangeLogSummary + +class Command(BaseCommand): + help = 'Creates ChangeLogSummary objects for ObjectChanges that do not have one' + + def handle(self, *args, **options): + # Get all ObjectChanges that don't have a summary and have action='update' + object_changes = ObjectChange.objects.filter( + action='update' + ).exclude( + id__in=ChangeLogSummary.objects.values_list('changelog_id', flat=True) + ) + + count = 0 + for change in object_changes: + summary = [] + if change.prechange_data and change.postchange_data: + pre_keys = set(change.prechange_data.keys()) + post_keys = set(change.postchange_data.keys()) + + # Find added keys + added_keys = post_keys - pre_keys + for key in added_keys: + summary.append(f"Added {key}") + + # Find removed keys + removed_keys = pre_keys - post_keys + for key in removed_keys: + summary.append(f"Removed {key}") + + # Check for updated values in common keys + common_keys = pre_keys & post_keys + for key in common_keys: + if change.prechange_data[key] != change.postchange_data[key]: + summary.append(f"Updated {key}") + + ChangeLogSummary.objects.create( + changelog=change, + summary=", ".join(summary) if summary else "No changes detected" + ) + count += 1 + + self.stdout.write( + self.style.SUCCESS(f'Successfully created {count} new changelog summaries') + ) diff --git a/netbox_changelog_diff_plugin/models.py b/netbox_changelog_diff_plugin/models.py index 7bbc4b1..21ba258 100644 --- a/netbox_changelog_diff_plugin/models.py +++ b/netbox_changelog_diff_plugin/models.py @@ -1,3 +1,27 @@ from django.db import models from django.urls import reverse from netbox.models import NetBoxModel + +class ChangeLogSummary(NetBoxModel): + """Model to store human-readable summaries of changelogs""" + + changelog = models.ForeignKey( + to='extras.ObjectChange', + on_delete=models.CASCADE, + related_name='summaries' + ) + + summary = models.TextField( + help_text="Human readable summary of the changes made" + ) + + class Meta: + verbose_name = "Changelog Summary" + verbose_name_plural = "Changelog Summaries" + ordering = ['-changelog__time'] + + def __str__(self): + return f"Summary for change {self.changelog.id}" + + def get_absolute_url(self): + return reverse('plugins:netbox_changelog_diff_plugin:changelogsummary', args=[self.pk])