Skip to content

Commit b8ac0a2

Browse files
committed
[chores] Improvements to notifications #191
- added is_new to device_registered signal - use is_new in device_registered notification to specify if it's a new or existing device - other minor improvements to code and docs Related to #191
1 parent 11fde76 commit b8ac0a2

File tree

9 files changed

+43
-26
lines changed

9 files changed

+43
-26
lines changed

README.rst

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -60,9 +60,9 @@ Other popular building blocks that are part of the OpenWISP ecosystem are:
6060
it can be used in conjunction with openwisp-monitoring to get a better idea
6161
of the state of the network
6262
- `openwisp-ipam <https://github.com/openwisp/openwisp-ipam>`_:
63-
it allows to manage the IP address space of networks
63+
allows to manage the assignment of IP addresses used in the network
6464
- `openwisp-notifications <https://github.com/openwisp/openwisp-notifications>`_:
65-
it allows to create and manage notifications from the network.
65+
allows users to be aware of important events happening in the network.
6666

6767
.. image:: https://raw.githubusercontent.com/openwisp/openwisp2-docs/master/assets/design/openwisp-logo-black.svg
6868
:target: http://openwisp.org
@@ -886,8 +886,12 @@ It is not triggered when the device is created for the first time.
886886
**Arguments**:
887887

888888
- ``instance``: instance of ``Device`` which got registered.
889+
- ``is_new``: boolean, will be ``True`` when the device is new,
890+
``False`` when the device already exists
891+
(eg: a device which gets a factory reset will register again)
889892

890-
This signal is emitted once the device gets registered automatically through the controller.
893+
This signal is emitted when a device registers automatically through the controller
894+
HTTP API.
891895

892896
Setup (Integrate into other Apps)
893897
---------------------------------

install-dev.sh

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,9 @@
22
set -e
33

44
# TODO: remove when openwisp-notifications 0.2 is released
5-
pip install -U https://github.com/openwisp/openwisp-notifications/tarball/issues/38-disable-object-notification
5+
pip install -U https://github.com/openwisp/openwisp-notifications/tarball/dev
66

7-
# TODO: can be remove when openwisp-users 0.4.1 is released
7+
# TODO: can be removed when openwisp-users 0.4.1 is released
88
# Commit needed for failures in tests:
99
# https://github.com/openwisp/openwisp-users/commit/ef347927136ddb4676479cb54cdc7cd08049e2e5
1010
pip install -U --no-deps https://github.com/openwisp/openwisp-users/tarball/master

openwisp_controller/config/apps.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ def register_notification_types(self):
106106
' {notification.verb}'
107107
),
108108
'message': _(
109-
'A new device [{notification.target}]'
109+
'{condition} device [{notification.target}]'
110110
'({notification.target_link}) has {notification.verb}.'
111111
),
112112
},

openwisp_controller/config/controller/views.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -343,7 +343,7 @@ def post(self, request, *args, **kwargs):
343343
# add templates specified in tags
344344
self.add_tagged_templates(config, request)
345345
# emit device registered signal
346-
device_registered.send(sender=config.__class__, instance=device)
346+
device_registered.send(sender=device.__class__, instance=device, is_new=new)
347347
# prepare response
348348
s = (
349349
'registration-result: success\n'

openwisp_controller/config/handlers.py

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
11
from django.dispatch import receiver
2+
from django.utils.translation import ugettext_lazy as _
23
from openwisp_notifications.signals import notify
34
from swapper import load_model
45

@@ -22,11 +23,14 @@ def config_status_error_notification(sender, instance, **kwargs):
2223

2324

2425
@receiver(
25-
device_registered, sender=Config, dispatch_uid='device_registered_notification'
26+
device_registered, sender=Device, dispatch_uid='device_registered_notification'
2627
)
27-
def device_registered_notification(sender, instance, **kwargs):
28+
def device_registered_notification(sender, instance, is_new, **kwargs):
2829
"""
2930
Creates notification when a new device is registered automatically
3031
through controller.
3132
"""
32-
notify.send(sender=instance, type='device_registered', target=instance)
33+
condition = _('A new') if is_new else _('The existing')
34+
notify.send(
35+
sender=instance, type='device_registered', target=instance, condition=condition
36+
)

openwisp_controller/config/signals.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -5,4 +5,4 @@
55
config_status_changed = Signal(providing_args=['instance'])
66
# device and config args are maintained for backward compatibility
77
config_modified = Signal(providing_args=['instance', 'device', 'config'])
8-
device_registered = Signal(providing_args=['instance'])
8+
device_registered = Signal(providing_args=['instance', 'is_new'])

openwisp_controller/config/tests/test_controller.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1004,5 +1004,5 @@ def test_device_registered_signal(self):
10041004
with catch_signal(device_registered) as handler:
10051005
device = self.test_register()
10061006
handler.assert_called_once_with(
1007-
sender=Config, signal=device_registered, instance=device,
1007+
sender=Device, signal=device_registered, instance=device, is_new=True
10081008
)

openwisp_controller/config/tests/test_notifications.py

Lines changed: 22 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@
88

99
from ..signals import device_registered
1010

11-
Config = load_model('config', 'Config')
11+
Device = load_model('config', 'Device')
1212
Notification = load_model('openwisp_notifications', 'Notification')
1313

1414
notification_qs = Notification.objects.all()
@@ -40,18 +40,28 @@ def test_device_registered(self):
4040
# we simulate that "device_registered" signal is emitted
4141
config = self._create_config()
4242
device = config.device
43-
device_registered.send(sender=Config, instance=config.device)
4443

45-
self.assertEqual(notification_qs.count(), 1)
46-
notification = notification_qs.first()
47-
self.assertEqual(notification.actor, device)
48-
self.assertEqual(notification.target, device)
49-
self.assertEqual(notification.type, 'device_registered')
50-
self.assertEqual(
51-
notification.email_subject,
52-
f'[example.com] SUCCESS: "{device}" registered successfully',
53-
)
54-
self.assertIn('registered successfully', notification.message)
44+
with self.subTest('is_new=True'):
45+
device_registered.send(sender=Device, instance=config.device, is_new=True)
46+
self.assertEqual(notification_qs.count(), 1)
47+
notification = notification_qs.first()
48+
self.assertEqual(notification.actor, device)
49+
self.assertEqual(notification.target, device)
50+
self.assertEqual(notification.type, 'device_registered')
51+
self.assertEqual(
52+
notification.email_subject,
53+
f'[example.com] SUCCESS: "{device}" registered successfully',
54+
)
55+
self.assertIn('registered successfully', notification.message)
56+
self.assertIn('A new device', notification.message)
57+
58+
Notification.objects.all().delete()
59+
60+
with self.subTest('is_new=True'):
61+
device_registered.send(sender=Device, instance=config.device, is_new=False)
62+
self.assertEqual(notification_qs.count(), 1)
63+
notification = notification_qs.first()
64+
self.assertIn('The existing device', notification.message)
5565

5666
def test_default_notification_type_already_unregistered(self):
5767
# Simulates if 'default notification type is already unregistered

requirements.txt

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -15,5 +15,4 @@ celery>=4.4.3,<4.5.0
1515
redis>=3.4.1,<4.0.0
1616
swapper~=1.1.0
1717
django-flat-json-widget~=0.1
18-
# TODO: uncomment when openwisp-notifications is released
19-
# 'openwisp_notifications~=0.1',
18+
openwisp-notifications~=0.1

0 commit comments

Comments
 (0)