Skip to content
This repository has been archived by the owner on Jul 9, 2020. It is now read-only.

Commit

Permalink
[models] Added config_modified signal
Browse files Browse the repository at this point in the history
  • Loading branch information
nemesifier committed May 6, 2018
1 parent 24bd70b commit 550eb52
Show file tree
Hide file tree
Showing 3 changed files with 32 additions and 1 deletion.
23 changes: 23 additions & 0 deletions django_netjsonconfig/base/config.py
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@
from sortedm2m.fields import SortedManyToManyField

from .. import settings as app_settings
from ..signals import config_modified
from .base import BaseConfig


Expand Down Expand Up @@ -51,13 +52,33 @@ 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:
self.save()

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)
Expand Down Expand Up @@ -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):
Expand Down
7 changes: 6 additions & 1 deletion django_netjsonconfig/base/template.py
Original file line number Diff line number Diff line change
Expand Up @@ -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):
"""
Expand Down
3 changes: 3 additions & 0 deletions django_netjsonconfig/signals.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
from django.dispatch import Signal

config_modified = Signal(providing_args=['device', 'config'])

0 comments on commit 550eb52

Please sign in to comment.