示例#1
0
文件: models.py 项目: varunarora/OC
    def my_resource_favorited_notification(sender, **kwargs):
        favorite = kwargs.get('favorite', None)
        host = kwargs.get('host', None)

        notification = Notification()

        if favorite.parent_type.name == 'resource':
            notification.user = favorite.parent.user
            notification.url = reverse(
                'read', kwargs={
                    'resource_id': favorite.parent.id,
                    'resource_slug': favorite.parent.slug
                }
            )
        else:
            notification.user = favorite.parent.creator
            notification.url = reverse(
                'user:list_collection', kwargs={
                    'username': favorite.parent.creator,
                    'collection_slug': favorite.parent.slug
                }
            )

        notification.description = '%s favorited "%s"' % (
            favorite.user.get_full_name(), favorite.parent.title)

        notification.save()

        # Send an email about this notification.
        nu.notify_by_email(notification, host)
示例#2
0
文件: models.py 项目: varunarora/OC
    def collaborator_add_notification(sender, **kwargs):
        collection = kwargs.get('collection', None)
        user = kwargs.get('user', None)
        request = kwargs.get('request', None)

        notification = Notification()
        notification.user = user

        # Get root host of the collection.
        import oer.CollectionUtilities as cu 
        (collection_root_type, collection_root) = cu.get_collection_root(collection)

        if collection_root_type.name == 'project':
            notification.url = reverse(
                'projects:list_collection', kwargs={
                    'project_slug': collection_root.slug,
                    'collection_slug': collection.slug
                }
            )

        notification.description = 'You have been added as a collaborator on the collection "%s"' % (
            collection.title)

        notification.save()

        # Send an email about this notification.
        nu.notify_by_email(notification, request.get_host())
示例#3
0
文件: models.py 项目: varunarora/OC
    def turn_member_into_admin_notification(sender, **kwargs):
        project = kwargs.get('project', None)
        user = kwargs.get('user', None)
        request = kwargs.get('request', None)

        notification = Notification()
        notification.user = user

        notification.url = reverse(
            'projects:project_home', kwargs={
                'project_slug': project.slug,
            }
        )

        notification.description = "You have been assigned as an administrator in %s" % (
            project.title)

        notification.save()

        # Send an email about this notification.
        nu.notify_by_email(notification, request.get_host())
示例#4
0
文件: models.py 项目: varunarora/OC
    def new_subscription_notification(sender, **kwargs):
        subscription = kwargs.get('subscription', None)
        host = kwargs.get('host', None)

        notification = Notification()
        notification.user = subscription.subscribee.user

        notification.url = reverse(
            'user:user_profile', kwargs={
                'username': subscription.subscriber.user,
            }
        )

        notification.description = '%s subscribed to you' % (
            subscription.subscriber.user.get_full_name())

        notification.save()

        if subscription.subscribee.user.get_profile().digests['subscription']:
            # Send an email about this notification.
            nu.notify_subscription_by_email(
                notification, host, subscription.subscriber.user)
示例#5
0
文件: models.py 项目: varunarora/OC
    def accept_project_invite_notification(sender, **kwargs):
        membership_id = kwargs.get('membership_id', None)
        request = kwargs.get('request', None)

        membership_request = Membership.objects.get(pk=int(membership_id))
        project = membership_request.project

        notification = Notification()
        notification.user = membership_request.user

        notification.url = reverse(
            'projects:project_home', kwargs={
                'project_slug': project.slug,
            }
        )

        notification.description = "Your request to join %s has been accepted!" % (
            project.title)

        notification.save()

        # Send an email about this notification.
        nu.notify_by_email(notification, request.get_host())
示例#6
0
文件: models.py 项目: varunarora/OC
    def add_project_invite_notification(sender, **kwargs):
        membership_id = kwargs.get('membership_id', None)
        request = kwargs.get('request', None)

        membership_request = Membership.objects.get(pk=int(membership_id))
        project = membership_request.project

        for admin in project.admins.all():
            notification = Notification()
            notification.user = admin

            notification.url = reverse(
                'projects:project_requests', kwargs={
                    'project_slug': project.slug,
                }
            )

            notification.description = "%s has requested to join %s" % (
                membership_request.user.get_full_name(), project.title)

            notification.save()

            # Send an email about this notification.
            nu.notify_by_email(notification, request.get_host())
示例#7
0
文件: models.py 项目: varunarora/OC
    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()