1
0
mirror of https://github.com/netbox-community/netbox.git synced 2024-05-10 07:54:54 +00:00

Implemented changelog retention setting, automatic purging

This commit is contained in:
Jeremy Stretch
2018-06-22 16:18:41 -04:00
parent 4e6f73e452
commit 3bdfe9c249
4 changed files with 27 additions and 0 deletions

View File

@@ -1,11 +1,17 @@
from __future__ import unicode_literals
from datetime import timedelta
import logging
import random
import uuid
from django.conf import settings
from django.db.models.signals import post_delete, post_save
from django.utils import timezone
from django.utils.functional import curry, SimpleLazyObject
from .constants import OBJECTCHANGE_ACTION_CREATE, OBJECTCHANGE_ACTION_DELETE, OBJECTCHANGE_ACTION_UPDATE
from .models import ObjectChange
def record_object_change(user, request_id, instance, **kwargs):
@@ -24,6 +30,15 @@ def record_object_change(user, request_id, instance, **kwargs):
instance.log_change(user, request_id, action)
# 1% chance of clearing out expired ObjectChanges
if settings.CHANGELOG_RETENTION and random.randint(1, 100):
cutoff = timezone.now() - timedelta(days=settings.CHANGELOG_RETENTION)
purged_count, _ = ObjectChange.objects.filter(
time__lt=cutoff
).delete()
logger = logging.getLogger('django')
logger.info("Automatically purged {} changes past the retention period".format(purged_count))
class ChangeLoggingMiddleware(object):