Introduce pre_sync & post_sync signals

This commit is contained in:
jeremystretch 2023-02-01 15:26:25 -05:00
parent ee378cd0aa
commit 0613571002
2 changed files with 17 additions and 0 deletions

View File

@ -20,6 +20,7 @@ from utilities.files import sha256_hash
from utilities.querysets import RestrictedQuerySet from utilities.querysets import RestrictedQuerySet
from ..choices import * from ..choices import *
from ..exceptions import SyncError from ..exceptions import SyncError
from ..signals import post_sync, pre_sync
__all__ = ( __all__ = (
'DataSource', 'DataSource',
@ -137,6 +138,9 @@ class DataSource(PrimaryModel):
if not self.ready_for_sync: if not self.ready_for_sync:
raise SyncError(f"Cannot initiate sync; data source not ready/enabled") raise SyncError(f"Cannot initiate sync; data source not ready/enabled")
# Emit the pre_sync signal
pre_sync.send(sender=self.__class__, instance=self)
self.status = DataSourceStatusChoices.SYNCING self.status = DataSourceStatusChoices.SYNCING
DataSource.objects.filter(pk=self.pk).update(status=self.status) DataSource.objects.filter(pk=self.pk).update(status=self.status)
@ -188,6 +192,9 @@ class DataSource(PrimaryModel):
self.last_synced = timezone.now() self.last_synced = timezone.now()
DataSource.objects.filter(pk=self.pk).update(status=self.status, last_synced=self.last_synced) DataSource.objects.filter(pk=self.pk).update(status=self.status, last_synced=self.last_synced)
# Emit the post_sync signal
post_sync.send(sender=self.__class__, instance=self)
def _walk(self, root): def _walk(self, root):
""" """
Return a set of all non-excluded files within the root path. Return a set of all non-excluded files within the root path.

10
netbox/core/signals.py Normal file
View File

@ -0,0 +1,10 @@
import django.dispatch
__all__ = (
'post_sync',
'pre_sync',
)
# DataSource signals
pre_sync = django.dispatch.Signal()
post_sync = django.dispatch.Signal()