Skip to content

Commit

Permalink
changelog extension model v1
Browse files Browse the repository at this point in the history
  • Loading branch information
ITJamie committed Oct 28, 2024
1 parent dd810be commit a53208f
Show file tree
Hide file tree
Showing 2 changed files with 71 additions and 0 deletions.
47 changes: 47 additions & 0 deletions netbox_changelog_diff_plugin/commands.py
Original file line number Diff line number Diff line change
@@ -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')
)
24 changes: 24 additions & 0 deletions netbox_changelog_diff_plugin/models.py
Original file line number Diff line number Diff line change
@@ -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])

0 comments on commit a53208f

Please sign in to comment.