From 550eb5238802e0d3050c313ee5c519cfe47fb3f5 Mon Sep 17 00:00:00 2001 From: Federico Capoano Date: Sun, 6 May 2018 21:57:17 +0200 Subject: [PATCH] [models] Added config_modified signal --- django_netjsonconfig/base/config.py | 23 +++++++++++++++++++++++ django_netjsonconfig/base/template.py | 7 ++++++- django_netjsonconfig/signals.py | 3 +++ 3 files changed, 32 insertions(+), 1 deletion(-) create mode 100644 django_netjsonconfig/signals.py diff --git a/django_netjsonconfig/base/config.py b/django_netjsonconfig/base/config.py index 6a07c20..6a32a98 100644 --- a/django_netjsonconfig/base/config.py +++ b/django_netjsonconfig/base/config.py @@ -6,6 +6,7 @@ from sortedm2m.fields import SortedManyToManyField from .. import settings as app_settings +from ..signals import config_modified from .base import BaseConfig @@ -51,6 +52,20 @@ def clean(self): self.set_status_modified(save=False) break + def save(self, *args, **kwargs): + result = super(AbstractConfig, self).save(*args, **kwargs) + if not self._state.adding and getattr(self, '_send_config_modified_after_save', False): + self._send_config_modified_signal() + return result + + def _send_config_modified_signal(self): + """ + sends signal ``config_modified`` + """ + config_modified.send(sender=self.__class__, + config=self, + device=self.device) + def _set_status(self, status, save=True): self.status = status if save: @@ -58,6 +73,12 @@ def _set_status(self, status, save=True): def set_status_modified(self, save=True): self._set_status('modified', save) + if save: + self._send_config_modified_signal() + else: + # set this attribute that will be + # checked in the save method + self._send_config_modified_after_save = True def set_status_running(self, save=True): self._set_status('running', save) @@ -208,6 +229,8 @@ def templates_changed(cls, action, instance, **kwargs): return if instance.status != 'modified': instance.set_status_modified() + else: + instance._send_config_modified_signal() @classmethod def manage_vpn_clients(cls, action, instance, pk_set, **kwargs): diff --git a/django_netjsonconfig/base/template.py b/django_netjsonconfig/base/template.py index 010d7d7..e3321ce 100644 --- a/django_netjsonconfig/base/template.py +++ b/django_netjsonconfig/base/template.py @@ -76,7 +76,12 @@ def save(self, *args, **kwargs): super(AbstractTemplate, self).save(*args, **kwargs) # update relations if update_related_config_status: - self.config_relations.update(status='modified') + self._update_related_config_status() + + def _update_related_config_status(self): + self.config_relations.update(status='modified') + for config in self.config_relations.all(): + config._send_config_modified_signal() def clean(self, *args, **kwargs): """ diff --git a/django_netjsonconfig/signals.py b/django_netjsonconfig/signals.py new file mode 100644 index 0000000..e3e91d4 --- /dev/null +++ b/django_netjsonconfig/signals.py @@ -0,0 +1,3 @@ +from django.dispatch import Signal + +config_modified = Signal(providing_args=['device', 'config'])