def new_comment_activity(sender, **kwargs): comment_id = kwargs.get('comment_id', None) parent_type_id = kwargs.get('parent_type_id', None) # Get the commment. from interactions.models import Comment comment = Comment.objects.get(pk=comment_id) # Get the type of the parent of the comment from django.contrib.contenttypes.models import ContentType parent_ct = ContentType.objects.get(pk=parent_type_id) if parent_ct.name == 'resource': import oer.ResourceUtilities as ru (resource_root_type, resource_root) = ru.get_resource_root(comment.parent) if resource_root_type.name == 'project': new_activity = Activity( actor=comment.user, action=comment, target=comment.parent, context=resource_root ) new_activity.save() elif resource_root_type.name == 'user profile': new_activity = Activity( actor=comment.user, action=comment, target=comment.parent, context=resource_root ) new_activity.save() if parent_ct.name == 'comment' or parent_ct.name == 'project': # Get root parent of the comment from interactions.CommentUtilities import CommentUtilities (root_parent_type, root_parent, root_comment) = CommentUtilities.get_comment_root(comment) # If this is the child of a comment if parent_ct.name == 'project': new_activity = Activity( actor=comment.user, action=comment, target=comment, context=root_parent ) new_activity.save() # If this is the child of a comment elif parent_ct.name == 'comment': if root_parent_type.name == 'project': new_activity = Activity( actor=comment.user, action=comment, target=root_comment, context=root_parent ) new_activity.save() if parent_ct.name == 'project' or ( parent_ct.name == 'comment' and root_parent_type.name == 'project'): recipients = None if root_parent.visibility == 'public': # Get all people subscribed to comment creator. subscriptions = Subscription.objects.filter( subscribee=comment.user.get_profile()) if subscriptions.count() >= 1: recipients = [x.subscriber.user for x in subscriptions] elif root_parent.visibility == 'private': project_members = root_parent.confirmed_members if len(project_members) >= 1: recipients = project_members if recipients: for recipient in recipients: new_activity.recipients.add(recipient) if parent_ct.name == 'resource': recipients = None subscriptions = Subscription.objects.filter( subscribee=comment.user.get_profile()) if subscriptions.count() >= 1: recipients = [x.subscriber.user for x in subscriptions] if comment.parent.visibility == 'private': recipients = [user for user in recipients if user in comment.parent.collaborators.all()] if recipients: for recipient in recipients: new_activity.recipients.add(recipient)
def new_resource_favorite_activity(sender, **kwargs): favorite = kwargs.get('favorite', None) import oer.CollectionUtilities as cu import oer.ResourceUtilities as ru if favorite.parent_type.name == 'resource': from django.core.exceptions import MultipleObjectsReturned try: collection = Collection.objects.get(resources__id=favorite.parent.id) except MultipleObjectsReturned: collection = Collection.objects.filter( resources__id=favorite.parent.id)[0] else: collection = favorite.parent.host (collection_host_type, collection_host) = ru.get_resource_root( favorite.parent) new_activity = Activity( actor=favorite.user, action=favorite, target=favorite.parent, context=collection_host ) new_activity.save() recipients = None # Get all people subscribed to resource creator. subscriptions = Subscription.objects.filter( subscribee=favorite.user.get_profile()) if subscriptions.count() >= 1: if (collection_host_type.name == 'project' and ( collection_host.visibility == 'public') and ( favorite.parent.visibility != 'private')): recipients = [x.subscriber.user for x in subscriptions] elif (collection_host_type.name == 'project' and ( collection_host.visibility == 'private')): if favorite.parent.visibility == 'project': recipients = [x.subscriber.user for x in subscriptions if x in collection_host.confirmed_members] elif favorite.parent.visibility == 'private': recipients = [x.subscriber.user for x in subscriptions if x in favorite.parent.collaborators.all()] elif favorite.parent.visibility == 'collection': root_private_collection = cu.get_root_private_collection(collection) recipients = [x.subscriber.user for x in subscriptions if x in root_private_collection.collaborators.all()] elif favorite.parent.visibility == 'public': recipients = [x.subscriber.user for x in subscriptions] elif favorite.parent.visibility == 'private': recipients = [x.subscriber.user for x in subscriptions if x in favorite.parent.collaborators.all()] elif favorite.parent.visibility == 'collection': root_private_collection = cu.get_root_private_collection(collection) recipients = [x.subscriber.user for x in subscriptions if x in root_private_collection.collaborators.all()] if recipients: for recipient in recipients: new_activity.recipients.add(recipient)
def add_comment_notification(sender, **kwargs): comment_id = kwargs.get('comment_id', None) parent_type_id = kwargs.get('parent_type_id', None) host = kwargs.get('host', None) # Get the commment. from interactions.models import Comment, CommentReference comment = Comment.objects.get(pk=comment_id) # Get the type of the parent of the comment from django.contrib.contenttypes.models import ContentType parent_ct = ContentType.objects.get(pk=parent_type_id) if parent_ct.name == 'comment reference': # Notify a consolidated list of (1) the collaborators on the resource, # (2) the previous commentors on the document element reference item. from oer.models import Document, DocumentElement, ResourceRevision from django.contrib.contenttypes.models import ContentType document_content_type = ContentType.objects.get_for_model(Document) document_element_content_type = ContentType.objects.get_for_model(DocumentElement) # Get the resource on whose (document element) this comment was created. document = comment.parent.owner.document resourcerevision = ResourceRevision.objects.get( content_id=document.id, content_type=document_content_type) # Get all other comments that have been made on document element reference item. # Exclude the current comment reference (crashed anyways because comment not assigned). comment_references = CommentReference.objects.filter( owner_type=document_element_content_type, owner_id=comment.parent.owner.id, reference=comment.parent.reference ).exclude(pk=comment.parent.id) comment_users = map(lambda x: x.comment.user, comment_references) users_to_notify = set(resourcerevision.resource.collaborators.all()) | set(comment_users) try: # Remove the creator of the comment from the list of users to notify if he/she # is a collaborator. users_to_notify.remove(comment.user) except: pass for user in users_to_notify: notification = Notification() notification.user = user notification.url = reverse( 'read', kwargs={ 'resource_id': resourcerevision.resource.id, 'resource_slug': resourcerevision.resource.slug } ) notification.description = "%s commented on the contents of %s: \"%s\"" % ( comment.user.get_full_name(), resourcerevision.resource.title, comment.body_markdown[:100]) notification.save() # Send an email about this notification. nu.notify_by_email(notification, host) elif parent_ct.name == 'comment' or parent_ct.name == 'resource' or parent_ct.name == 'article revision': # Determine whether this is an ArticleRevision, resource, etc. and the # user who created it. asset = comment.parent user_to_notify = asset.user # If the commentor is not the same as the creator of the original post if user_to_notify != comment.user: notification = Notification() notification.user = user_to_notify # If this is the child of a comment if parent_ct.name == 'comment': # Get root parent of the comment from interactions.CommentUtilities import CommentUtilities (root_parent_type, root_parent, root_comment) = CommentUtilities.get_comment_root(comment) else: root_parent = comment.parent root_parent_type = parent_ct root_comment = comment if root_parent_type.name == 'article revision': from articles import views breadcrumb = views.fetch_cached_breadcrumb(asset) category_slug = [x.slug for x in breadcrumb[1:]] notification.url = reverse( 'articles:reader', kwargs={'category_slug': '/'.join(category_slug)} ) + "?q=%s&revision=%s" % (asset.article.slug, str(asset.id)) notification.description = "%s commented on %s: \"%s\"" % ( comment.user.get_full_name(), asset.title, comment.body_markdown[:100]) elif root_parent_type.name == 'project': notification.url = reverse( 'projects:project_discussion', kwargs={ 'project_slug': root_parent.slug, 'discussion_id': root_comment.id } ) notification.description = "%s commented on your post in %s: \"%s\"" % ( comment.user.get_full_name(), root_parent.title, comment.body_markdown[:100]) elif root_parent_type.name == 'resource': import oer.ResourceUtilities as ru (resource_root_type, resource_root) = ru.get_resource_root(root_parent) if resource_root_type.name == 'project': notification.url = reverse( 'projects:read_project_resource', kwargs={ 'project_slug': resource_root.slug, 'resource_id': root_parent.id, 'resource_slug': root_parent.slug } ) elif resource_root_type.name == 'user profile': notification.url = reverse( 'read', kwargs={ 'resource_id': root_parent.id, 'resource_slug': root_parent.slug } ) notification.description = "%s commented on your post in %s: \"%s\"" % ( comment.user.get_full_name(), root_parent.title, comment.body_markdown[:100]) notification.save() # Send an email about this notification. nu.notify_by_email(notification, host) elif parent_ct.name == 'project': project_members = comment.parent.members.all().exclude(pk=comment.user.id) for member in project_members: notification = Notification() notification.user = member notification.url = reverse( 'projects:project_discussion', kwargs={ 'project_slug': comment.parent.slug, 'discussion_id': comment.id } ) notification.description = "%s wrote a new post in %s: \"%s\"" % ( comment.user.get_full_name(), comment.parent.title, comment.body_markdown[:100]) notification.save()