Skip to content

Commit

Permalink
automatic notification on image share
Browse files Browse the repository at this point in the history
  • Loading branch information
Christian Glatthard committed Aug 12, 2015
1 parent c4a732f commit eac2120
Show file tree
Hide file tree
Showing 4 changed files with 65 additions and 5 deletions.
46 changes: 43 additions & 3 deletions ipynbsrv/core/signals/container_images.py
Original file line number Diff line number Diff line change
@@ -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 *


Expand Down Expand Up @@ -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):
"""
Expand Down
20 changes: 19 additions & 1 deletion ipynbsrv/core/signals/notifications.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
)
Expand All @@ -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):
"""
Expand Down
2 changes: 2 additions & 0 deletions ipynbsrv/core/signals/signals.py
Original file line number Diff line number Diff line change
Expand Up @@ -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'])


"""
Expand Down
2 changes: 1 addition & 1 deletion ipynbsrv/web/templates/web/notifications/index.html
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ <h1>Notifications</h1>
<td>{{ n.date | date:"d.m.Y, H:i" }}</td>
<td>{{ n.notification.sender.username }}</td>
<td>{{ n.notification.message }}</td>
<td>{% if n.notification.has_related_object %}<a href="{% if n.notification.notification_type == 'container'%}{% url 'containers' %}{% elif n.notification.notification_type == 'container_image'%}{% url 'images' %}{% elif n.notification.notification_type == 'group' %}{% url 'group_manage' n.notification.group %}{% elif n.notification.notification_type == 'share' %}{% url 'share_manage' n.notification.share %}{% endif %}">{{ n.notification.notification_type }}</a>{% else %}{{ n.notification.notification_type }}{% endif %}</td>
<td>{% if n.notification.has_related_object %}<a href="{% if n.notification.notification_type == 'container'%}{% url 'containers' %}{% elif n.notification.notification_type == 'container_image'%}{% url 'image_manage' n.notification.container_image %}{% elif n.notification.notification_type == 'group' %}{% url 'group_manage' n.notification.group %}{% elif n.notification.notification_type == 'share' %}{% url 'share_manage' n.notification.share %}{% endif %}">{{ n.notification.notification_type }}</a>{% else %}{{ n.notification.notification_type }}{% endif %}</td>
<td>{{ n.read }}</td>
<td>
<form action="{% url 'notification_mark_as_read' %}" method="POST" role="form" class="form-action pull-right">
Expand Down

0 comments on commit eac2120

Please sign in to comment.