From eac212080c54c4f4fe1debb3a6d4625d9bba1b1e Mon Sep 17 00:00:00 2001 From: Christian Glatthard Date: Wed, 12 Aug 2015 22:32:32 +0200 Subject: [PATCH] automatic notification on image share --- ipynbsrv/core/signals/container_images.py | 46 +++++++++++++++++-- ipynbsrv/core/signals/notifications.py | 20 +++++++- ipynbsrv/core/signals/signals.py | 2 + .../templates/web/notifications/index.html | 2 +- 4 files changed, 65 insertions(+), 5 deletions(-) diff --git a/ipynbsrv/core/signals/container_images.py b/ipynbsrv/core/signals/container_images.py index 9a488fe..93dd8f2 100644 --- a/ipynbsrv/core/signals/container_images.py +++ b/ipynbsrv/core/signals/container_images.py @@ -1,8 +1,8 @@ -from django.db.models.signals import post_delete, post_save +from django.db.models.signals import m2m_changed, post_delete, post_save from django.dispatch import receiver from ipynbsrv.contract.backends import ContainerBackend -from ipynbsrv.contract.errors import ConnectionError, ContainerBackendError, ContainerImageNotFoundError -from ipynbsrv.core.models import Container, ContainerImage, Server +from ipynbsrv.contract.errors import ContainerBackendError, ContainerImageNotFoundError +from ipynbsrv.core.models import CollaborationGroup, ContainerImage, Server from ipynbsrv.core.signals.signals import * @@ -51,6 +51,46 @@ def delete_on_server(sender, image, **kwargs): pass +@receiver(m2m_changed, sender=ContainerImage.access_groups.through) +def m2m_changed_handler(sender, instance, **kwargs): + """ + Method to map Django m2m_changed model signals to custom ones. + """ + action = kwargs.get('action') + if isinstance(instance, ContainerImage): + if 'pk_set' in kwargs: # access groups + # get the group objects + groups = [] + if kwargs.get('pk_set') is not None: + for group_pk in kwargs.get('pk_set'): + groups.append(CollaborationGroup.objects.get(pk=group_pk)) + # trigger the signals + if action == 'post_add': + for group in groups: + container_image_access_group_added.send( + sender=sender, + image=instance, + group=group, + kwargs=kwargs + ) + elif action == 'pre_clear': + for group in instance.access_groups.all(): + container_image_access_group_removed.send( + sender=sender, + image=instance, + group=group, + kwargs=kwargs + ) + elif action == 'post_remove': + for group in groups: + container_image_access_group_removed.send( + sender=sender, + image=instance, + group=group, + kwargs=kwargs + ) + + @receiver(post_delete, sender=ContainerImage) def post_delete_handler(sender, instance, **kwargs): """ diff --git a/ipynbsrv/core/signals/notifications.py b/ipynbsrv/core/signals/notifications.py index 48f5376..25ee776 100644 --- a/ipynbsrv/core/signals/notifications.py +++ b/ipynbsrv/core/signals/notifications.py @@ -63,7 +63,7 @@ def create_member_share_removed(sender, share, group, **kwargs): if share is not None and group is not None: try: notification = Notification( - message='Group %s has been removed to share.' % group, + message='Group %s has been removed from share.' % group, notification_type=Notification.SHARE, share=share ) @@ -73,6 +73,24 @@ def create_member_share_removed(sender, share, group, **kwargs): print('create_member_share_removed error: %s' % e) # just for debugging purposes +@receiver(container_image_access_group_added) +def create_member_image_added(sender, image, group, **kwargs): + """ + Create an image notification if a new access_group gets added. + """ + if image is not None and group is not None: + try: + notification = Notification( + message='%s has shared a container image with you.' % image.owner, + notification_type=Notification.CONTAINER_IMAGE, + container_image=image + ) + notification.save() + notification.receiver_groups.add(group) + except Exception as e: + print('create_member_image_added error: %s' % e) # just for debugging purposes + + @receiver(notification_receiver_group_added) def create_notificationlogs_for_receivers(sender, notification, group, **kwargs): """ diff --git a/ipynbsrv/core/signals/signals.py b/ipynbsrv/core/signals/signals.py index 87bd9fd..c55660c 100644 --- a/ipynbsrv/core/signals/signals.py +++ b/ipynbsrv/core/signals/signals.py @@ -55,6 +55,8 @@ container_image_created = Signal(providing_args=['image']) container_image_deleted = Signal(providing_args=['image']) container_image_modified = Signal(providing_args=['image']) +container_image_access_group_added = Signal(providing_args=['image', 'group']) +container_image_access_group_removed = Signal(providing_args=['image', 'group']) """ diff --git a/ipynbsrv/web/templates/web/notifications/index.html b/ipynbsrv/web/templates/web/notifications/index.html index 58e3357..4d9f2ed 100644 --- a/ipynbsrv/web/templates/web/notifications/index.html +++ b/ipynbsrv/web/templates/web/notifications/index.html @@ -44,7 +44,7 @@

Notifications

{{ n.date | date:"d.m.Y, H:i" }} {{ n.notification.sender.username }} {{ n.notification.message }} - {% if n.notification.has_related_object %}{{ n.notification.notification_type }}{% else %}{{ n.notification.notification_type }}{% endif %} + {% if n.notification.has_related_object %}{{ n.notification.notification_type }}{% else %}{{ n.notification.notification_type }}{% endif %} {{ n.read }}