-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
2 changed files
with
71 additions
and
0 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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') | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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]) |