Skip to content

Commit

Permalink
some notification refactoring
Browse files Browse the repository at this point in the history
  • Loading branch information
Michel Käser committed Aug 7, 2015
1 parent a629042 commit 7d6c63b
Show file tree
Hide file tree
Showing 5 changed files with 61 additions and 33 deletions.
58 changes: 25 additions & 33 deletions ipynbsrv/core/models.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
from django.contrib.auth.models import Group, User
from django.core.exceptions import ValidationError
from django.core.urlresolvers import reverse
from django.core.validators import RegexValidator
from django.db import models
from django.utils.encoding import smart_unicode
Expand Down Expand Up @@ -978,22 +977,22 @@ def clean(self):
"""
:inherit.
"""
if self.notification_type == 'container' and not self.container:
if self.notification_type == Notification.CONTAINER and not self.container:
raise ValidationError({
'notification_type': 'Related container needed for this type.',
'container': 'Related container must be choosen.'
})
elif self.notification_type == 'container_image' and not self.container_image:
elif self.notification_type == Notification.CONTAINER_IMAGE and not self.container_image:
raise ValidationError({
'notification_type': 'Related container image needed for this type.',
'container_image': 'Related container image must be choosen.'
})
elif self.notification_type == 'group' and not self.group:
elif self.notification_type == Notification.GROUP and not self.group:
raise ValidationError({
'notification_type': 'Related group needed for this type.',
'group': 'Related group must be choosen.'
})
elif self.notification_type == 'share' and not self.share:
elif self.notification_type == Notification.SHARE and not self.share:
raise ValidationError({
'notification_type': 'Related share needed for this type.',
'share': 'Related share must be choosen.'
Expand All @@ -1004,38 +1003,31 @@ def get_related_object(self):
"""
Get the object related.
"""
# try / catch in case a ressource does not exist anymore
try:
if self.container is not None:
return self.container
if self.container_image is not None:
return self.container_image
if self.group is not None:
return self.group
if self.share is not None:
return self.share
return None
except Exception:
if self.notification_type == Notification.CONTAINER:
return self.container
elif self.notification_type == Notification.CONTAINER_IMAGE:
return self.container_image
elif self.notification_type == Notification.GROUP:
return self.group
elif self.notification_type == Notification.SHARE:
return self.share
else:
return None

def get_related_object_url(self):
def has_access(self, user):
"""
Get the url slug for the related object.
Check if the `user` has access to this notification.
:param user: The user to check.
"""
obj = self.get_related_object()
if obj:
pk = obj.id
else:
return None
if self.notification_type == 'container' and self.container is not None:
return reverse('containers')
if self.notification_type == 'container_image' and self.container_image is not None:
return reverse('images')
if self.notification_type == 'group' and self.group is not None:
return reverse('group_manage', args=[pk])
if self.notification_type == 'share' and self.share is not None:
return reverse('share_manage', args=[pk])
return None
is_receiver = False
if hasattr(user, 'django_user'):
for receiver_group in self.receiver_groups.all():
if receiver_group.has_access(user):
is_receiver = True
break
user = user.django_user
return user == self.sender or is_receiver

def has_related_object(self):
"""
Expand Down
9 changes: 9 additions & 0 deletions ipynbsrv/core/signals/collaboration_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,15 @@
from ipynbsrv.core.signals.signals import *


@receiver(collaboration_group_deleted)
def delete_related_notifications(sender, group, **kwargs):
"""
Delete the groups's related notifications upon deletion.
"""
if group is not None and hasattr(group, 'related_notifications'):
group.related_notifications.all().delete()


@receiver(collaboration_group_admin_added)
def map_to_member_added_signal(sender, group, user, **kwargs):
"""
Expand Down
9 changes: 9 additions & 0 deletions ipynbsrv/core/signals/container_images.py
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,15 @@ def delete_internal_image_if_latest(sender, container, **kwargs):
pass


@receiver(container_image_deleted)
def delete_related_notifications(sender, image, **kwargs):
"""
Delete the container image's related notifications upon deletion.
"""
if image is not None and hasattr(image, 'related_notifications'):
image.related_notifications.all().delete()


@receiver(container_image_deleted)
def delete_on_server(sender, image, **kwargs):
"""
Expand Down
9 changes: 9 additions & 0 deletions ipynbsrv/core/signals/containers.py
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,15 @@ def create_on_server(sender, container, **kwargs):
container.save()


@receiver(container_deleted)
def delete_related_notifications(sender, container, **kwargs):
"""
Delete the container's related notifications upon deletion.
"""
if container is not None and hasattr(container, 'related_notifications'):
container.related_notifications.all().delete()


@receiver(container_deleted)
def delete_on_server(sender, container, **kwargs):
"""
Expand Down
9 changes: 9 additions & 0 deletions ipynbsrv/core/signals/shares.py
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,15 @@ def delete_backend_group(sender, share, **kwargs):
share.backend_group.delete()


@receiver(share_deleted)
def delete_related_notifications(sender, share, **kwargs):
"""
Delete all the share's related notifications.
"""
if share is not None and hasattr(share, 'related_notifications'):
share.related_notifications.all().delete()


@receiver(share_deleted)
def delete_share_directory(sender, share, **kwargs):
"""
Expand Down

0 comments on commit 7d6c63b

Please sign in to comment.