Exemplo n.º 1
0
def categorize_users(user, source_event, source_node, event, node):
    """Categorize users from a file subscription into three categories.

    Puts users in one of three bins:
     - Moved: User has permissions on both nodes, subscribed to both
     - Warned: User has permissions on both, not subscribed to destination
     - Removed: Does not have permission on destination node
    :param user: User instance who started the event
    :param source_event: <guid>_event_name
    :param source_node: node from where the event happened
    :param event: new guid event name
    :param node: node where event ends up
    :return: Moved, to be warned, and removed users.
    """
    remove = utils.users_to_remove(source_event, source_node, node)
    source_node_subs = compile_subscriptions(
        source_node, utils.find_subscription_type(source_event))
    new_subs = compile_subscriptions(
        node, utils.find_subscription_type(source_event), event)

    # Moves users into the warn bucket or the move bucket
    move = subscriptions_users_union(source_node_subs, new_subs)
    warn = subscriptions_users_difference(source_node_subs, new_subs)

    # Removes users without permissions
    warn, remove = subscriptions_node_permissions(node, warn, remove)

    # Remove duplicates
    warn = subscriptions_users_remove_duplicates(warn,
                                                 new_subs,
                                                 remove_same=False)
    move = subscriptions_users_remove_duplicates(move,
                                                 new_subs,
                                                 remove_same=False)

    # Remove duplicates between move and warn; and move and remove
    move = subscriptions_users_remove_duplicates(move, warn, remove_same=True)
    move = subscriptions_users_remove_duplicates(move,
                                                 remove,
                                                 remove_same=True)

    for notifications in constants.NOTIFICATION_TYPES:
        # Remove the user who started this whole thing.
        user_id = user._id
        if user_id in warn[notifications]:
            warn[notifications].remove(user_id)
        if user_id in move[notifications]:
            move[notifications].remove(user_id)
        if user_id in remove[notifications]:
            remove[notifications].remove(user_id)

    return move, warn, remove
Exemplo n.º 2
0
    def _send_preprint_confirmation(self, auth):
        # Send creator confirmation email
        recipient = self.creator
        event_type = utils.find_subscription_type('global_reviews')
        user_subscriptions = get_user_subscriptions(recipient, event_type)
        if self.provider._id == 'osf':
            logo = settings.OSF_PREPRINTS_LOGO
        else:
            logo = self.provider._id

        context = {
            'domain': settings.DOMAIN,
            'reviewable': self,
            'workflow': self.provider.reviews_workflow,
            'provider_url': '{domain}preprints/{provider_id}'.format(
                            domain=self.provider.domain or settings.DOMAIN,
                            provider_id=self.provider._id if not self.provider.domain else '').strip('/'),
            'provider_contact_email': self.provider.email_contact or settings.OSF_CONTACT_EMAIL,
            'provider_support_email': self.provider.email_support or settings.OSF_SUPPORT_EMAIL,
            'no_future_emails': user_subscriptions['none'],
            'is_creator': True,
            'provider_name': 'OSF Preprints' if self.provider.name == 'Open Science Framework' else self.provider.name,
            'logo': logo,
            'document_type': self.provider.preprint_word
        }

        mails.send_mail(
            recipient.username,
            mails.REVIEWS_SUBMISSION_CONFIRMATION,
            user=recipient,
            **context
        )
Exemplo n.º 3
0
def notify(event, user, node, timestamp, **context):
    """Retrieve appropriate ***subscription*** and passe user list

    :param event: event that triggered the notification
    :param user: user who triggered notification
    :param node: instance of Node
    :param timestamp: time event happened
    :param context: optional variables specific to templates
        target_user: used with comment_replies
    :return: List of user ids notifications were sent to
    """
    event_type = utils.find_subscription_type(event)
    subscriptions = compile_subscriptions(node, event_type, event)
    sent_users = []
    target_user = context.get('target_user', None)
    if target_user:
        target_user_id = target_user._id
        if event_type in constants.USER_SUBSCRIPTIONS_AVAILABLE:
            subscriptions = get_user_subscriptions(target_user, event_type)
    for notification_type in subscriptions:
        if notification_type != 'none' and subscriptions[notification_type]:
            if user in subscriptions[notification_type]:
                subscriptions[notification_type].pop(subscriptions[notification_type].index(user))
            if target_user and target_user_id in subscriptions[notification_type]:
                subscriptions[notification_type].pop(subscriptions[notification_type].index(target_user_id))
                if target_user_id != user._id:
                    store_emails([target_user_id], notification_type, 'comment_replies', user, node,
                                 timestamp, **context)
                    sent_users.append(target_user_id)
            if subscriptions[notification_type]:
                store_emails(subscriptions[notification_type], notification_type, event_type, user, node,
                             timestamp, **context)
                sent_users.extend(subscriptions[notification_type])
    return sent_users
Exemplo n.º 4
0
def notify_global_event(event,
                        sender_user,
                        node,
                        timestamp,
                        recipients,
                        template=None,
                        context=None):
    event_type = utils.find_subscription_type(event)
    sent_users = []

    for recipient in recipients:
        subscriptions = get_user_subscriptions(recipient, event_type)
        context['is_creator'] = recipient == node.creator
        for notification_type in subscriptions:
            if (notification_type != 'none'
                    and subscriptions[notification_type]
                    and recipient._id in subscriptions[notification_type]):
                store_emails([recipient._id],
                             notification_type,
                             event,
                             sender_user,
                             node,
                             timestamp,
                             template=template,
                             **context)
                sent_users.append(recipient._id)

    return sent_users
Exemplo n.º 5
0
def reviews_submit_notification(self, recipients, context):
    # Avoid AppRegistryNotReady error
    from website.notifications.emails import get_user_subscriptions

    event_type = utils.find_subscription_type('global_reviews')

    provider = context['reviewable'].provider
    if provider._id == 'osf':
        if provider.type == 'osf.preprintprovider':
            context['logo'] = OSF_PREPRINTS_LOGO
        elif provider.type == 'osf.registrationprovider':
            context['logo'] = OSF_REGISTRIES_LOGO
        else:
            raise NotImplementedError()
    else:
        context['logo'] = context['reviewable'].provider._id

    for recipient in recipients:
        user_subscriptions = get_user_subscriptions(recipient, event_type)
        context['no_future_emails'] = user_subscriptions['none']
        context['is_creator'] = recipient == context['reviewable'].creator
        context['provider_name'] = context['reviewable'].provider.name
        mails.send_mail(recipient.username,
                        mails.REVIEWS_SUBMISSION_CONFIRMATION,
                        mimetype='html',
                        user=recipient,
                        **context)
Exemplo n.º 6
0
    def _send_preprint_confirmation(self, auth):
        # Send creator confirmation email
        recipient = self.creator
        event_type = utils.find_subscription_type('global_reviews')
        user_subscriptions = get_user_subscriptions(recipient, event_type)
        if self.provider._id == 'osf':
            logo = settings.OSF_PREPRINTS_LOGO
        else:
            logo = self.provider._id

        context = {
            'domain': settings.DOMAIN,
            'reviewable': self,
            'workflow': self.provider.reviews_workflow,
            'provider_url': '{domain}preprints/{provider_id}'.format(
                            domain=self.provider.domain or settings.DOMAIN,
                            provider_id=self.provider._id if not self.provider.domain else '').strip('/'),
            'provider_contact_email': self.provider.email_contact or settings.OSF_CONTACT_EMAIL,
            'provider_support_email': self.provider.email_support or settings.OSF_SUPPORT_EMAIL,
            'no_future_emails': user_subscriptions['none'],
            'is_creator': True,
            'provider_name': 'OSF Preprints' if self.provider.name == 'Open Science Framework' else self.provider.name,
            'logo': logo,
        }

        mails.send_mail(
            recipient.username,
            mails.REVIEWS_SUBMISSION_CONFIRMATION,
            mimetype='html',
            user=recipient,
            **context
        )
Exemplo n.º 7
0
def reviews_notification(self, context):
    timestamp = timezone.now()
    event_type = utils.find_subscription_type('global_reviews')
    template = context['template'] + '.html.mako'
    for user_id in context['email_recipients']:
        user = OSFUser.load(user_id)
        subscriptions = get_user_subscriptions(user, event_type)
        for notification_type in subscriptions:
            check_user_subscribe = subscriptions[
                notification_type] and user_id in subscriptions[
                    notification_type] and notification_type != 'none'  # check if user is subscribed to this type of notifications
            if check_user_subscribe:
                node_lineage_ids = get_node_lineage(
                    context.get('reviewable').node) if context.get(
                        'reviewable').node else []
                context['user'] = user
                context['no_future_emails'] = notification_type == 'none'
                send_type = notification_type if notification_type != 'none' else 'email_transactional'
                message = mails.render_message(template, **context)
                digest = NotificationDigest(user=user,
                                            timestamp=timestamp,
                                            send_type=send_type,
                                            event='global_reviews',
                                            message=message,
                                            node_lineage=node_lineage_ids)
                digest.save()
Exemplo n.º 8
0
def notify(event, user, node, timestamp, **context):
    """Retrieve appropriate ***subscription*** and passe user list

    :param event: event that triggered the notification
    :param user: user who triggered notification
    :param node: instance of Node
    :param timestamp: time event happened
    :param context: optional variables specific to templates
        target_user: used with comment_replies
    :return: List of user ids notifications were sent to
    """
    event_type = utils.find_subscription_type(event)
    subscriptions = compile_subscriptions(node, event_type, event)
    sent_users = []
    target_user = context.get('target_user', None)
    if target_user:
        target_user_id = target_user._id
    for notification_type in subscriptions:
        if notification_type != 'none' and subscriptions[notification_type]:
            if user in subscriptions[notification_type]:
                subscriptions[notification_type].pop(subscriptions[notification_type].index(user))
            if target_user and target_user_id in subscriptions[notification_type]:
                subscriptions[notification_type].pop(subscriptions[notification_type].index(target_user_id))
                if target_user_id != user._id:
                    store_emails([target_user_id], notification_type, 'comment_replies', user, node,
                                 timestamp, **context)
                    sent_users.append(target_user_id)
            if subscriptions[notification_type]:
                store_emails(subscriptions[notification_type], notification_type, event_type, user, node,
                             timestamp, **context)
                sent_users.extend(subscriptions[notification_type])
    return sent_users
Exemplo n.º 9
0
def categorize_users(user, source_event, source_node, event, node):
    """Categorize users from a file subscription into three categories.

    Puts users in one of three bins:
     - Moved: User has permissions on both nodes, subscribed to both
     - Warned: User has permissions on both, not subscribed to destination
     - Removed: Does not have permission on destination node
    :param user: User instance who started the event
    :param source_event: <guid>_event_name
    :param source_node: node from where the event happened
    :param event: new guid event name
    :param node: node where event ends up
    :return: Moved, to be warned, and removed users.
    """
    remove = utils.users_to_remove(source_event, source_node, node)
    source_node_subs = compile_subscriptions(source_node, utils.find_subscription_type(source_event))
    new_subs = compile_subscriptions(node, utils.find_subscription_type(source_event), event)

    # Moves users into the warn bucket or the move bucket
    move = subscriptions_users_union(source_node_subs, new_subs)
    warn = subscriptions_users_difference(source_node_subs, new_subs)

    # Removes users without permissions
    warn, remove = subscriptions_node_permissions(node, warn, remove)

    # Remove duplicates
    warn = subscriptions_users_remove_duplicates(warn, new_subs, remove_same=False)
    move = subscriptions_users_remove_duplicates(move, new_subs, remove_same=False)

    # Remove duplicates between move and warn; and move and remove
    move = subscriptions_users_remove_duplicates(move, warn, remove_same=True)
    move = subscriptions_users_remove_duplicates(move, remove, remove_same=True)

    for notifications in constants.NOTIFICATION_TYPES:
        # Remove the user who started this whole thing.
        user_id = user._id
        if user_id in warn[notifications]:
            warn[notifications].remove(user_id)
        if user_id in move[notifications]:
            move[notifications].remove(user_id)
        if user_id in remove[notifications]:
            remove[notifications].remove(user_id)

    return move, warn, remove
Exemplo n.º 10
0
def reviews_submit_notification(self, recipients, context):
    event_type = utils.find_subscription_type('global_reviews')
    for recipient in recipients:
        user_subscriptions = get_user_subscriptions(recipient, event_type)
        context['no_future_emails'] = user_subscriptions['none']
        context['is_creator'] = recipient == context['reviewable'].node.creator
        context['provider_name'] = context['reviewable'].provider.name
        mails.send_mail(recipient.username,
                        mails.REVIEWS_SUBMISSION_CONFIRMATION,
                        mimetype='html',
                        user=recipient,
                        **context)
Exemplo n.º 11
0
def notify_mentions(event, user, node, timestamp, **context):
    event_type = utils.find_subscription_type(event)
    sent_users = []
    new_mentions = context.get('new_mentions', [])
    for m in new_mentions:
        mentioned_user = website_models.User.load(m)
        subscriptions = get_user_subscriptions(mentioned_user, event_type)
        for notification_type in subscriptions:
            if notification_type != 'none' and subscriptions[notification_type] and m in subscriptions[notification_type]:
                store_emails([m], notification_type, 'mentions', user, node,
                                 timestamp, **context)
                sent_users.extend([m])
    return sent_users
Exemplo n.º 12
0
def notify_global_event(event, sender_user, node, timestamp, recipients, template=None, context=None):
    event_type = utils.find_subscription_type(event)
    sent_users = []

    for recipient in recipients:
        subscriptions = get_user_subscriptions(recipient, event_type)
        context['is_creator'] = recipient == node.creator
        for notification_type in subscriptions:
            if (notification_type != 'none' and subscriptions[notification_type] and recipient._id in subscriptions[notification_type]):
                store_emails([recipient._id], notification_type, event, sender_user, node, timestamp, template=template, **context)
                sent_users.append(recipient._id)

    return sent_users
Exemplo n.º 13
0
def notify_mentions(event, user, node, timestamp, **context):
    event_type = utils.find_subscription_type(event)
    sent_users = []
    new_mentions = context.get('new_mentions', [])
    for m in new_mentions:
        mentioned_user = OSFUser.load(m)
        subscriptions = get_user_subscriptions(mentioned_user, event_type)
        for notification_type in subscriptions:
            if (notification_type != 'none'
                    and subscriptions[notification_type]
                    and m in subscriptions[notification_type]):
                store_emails([m], notification_type, 'mentions', user, node,
                             timestamp, **context)
                sent_users.extend([m])
    return sent_users
Exemplo n.º 14
0
def reviews_submit_notification(self, recipients, context):
    # Avoid AppRegistryNotReady error
    from website.notifications.emails import get_user_subscriptions
    event_type = utils.find_subscription_type('global_reviews')
    for recipient in recipients:
        user_subscriptions = get_user_subscriptions(recipient, event_type)
        context['no_future_emails'] = user_subscriptions['none']
        context['is_creator'] = recipient == context['reviewable'].node.creator
        context['provider_name'] = context['reviewable'].provider.name
        mails.send_mail(
            recipient.username,
            mails.REVIEWS_SUBMISSION_CONFIRMATION,
            mimetype='html',
            user=recipient,
            **context
        )
Exemplo n.º 15
0
def notify(event, user, node, timestamp, **context):
    """Retrieve appropriate ***subscription*** and passe user list

    :param event: event that triggered the notification
    :param user: user who triggered notification
    :param node: instance of Node
    :param timestamp: time event happened
    :param context: optional variables specific to templates
        target_user: used with comment_replies
    :return: List of user ids notifications were sent to
    """
    sent_users = []
    # The user who the current comment is a reply to
    target_user = context.get('target_user', None)
    exclude = context.get('exclude', [])
    # do not notify user who initiated the emails
    exclude.append(user._id)

    event_type = utils.find_subscription_type(event)
    if target_user and event_type in constants.USER_SUBSCRIPTIONS_AVAILABLE:
        # global user
        subscriptions = get_user_subscriptions(target_user, event_type)
    else:
        # local project user
        subscriptions = compile_subscriptions(node, event_type, event)

    for notification_type in subscriptions:
        if notification_type == 'none' or not subscriptions[notification_type]:
            continue
        # Remove excluded ids from each notification type
        subscriptions[notification_type] = [
            guid for guid in subscriptions[notification_type]
            if guid not in exclude
        ]

        # If target, they get a reply email and are removed from the general email
        if target_user and target_user._id in subscriptions[notification_type]:
            subscriptions[notification_type].remove(target_user._id)
            store_emails([target_user._id], notification_type,
                         'comment_replies', user, node, timestamp, **context)
            sent_users.append(target_user._id)

        if subscriptions[notification_type]:
            store_emails(subscriptions[notification_type], notification_type,
                         event_type, user, node, timestamp, **context)
            sent_users.extend(subscriptions[notification_type])
    return sent_users
Exemplo n.º 16
0
def notify_global_event(event, sender_user, node, timestamp, recipients, template=None, context=None):
    event_type = utils.find_subscription_type(event)
    sent_users = []
    if not context:
        context = {}

    for recipient in recipients:
        subscriptions = get_user_subscriptions(recipient, event_type)
        context['is_creator'] = recipient == node.creator
        if node.provider:
            context['has_psyarxiv_chronos_text'] = node.has_permission(recipient, ADMIN) and 'psyarxiv' in node.provider.name.lower()
        for notification_type in subscriptions:
            if (notification_type != 'none' and subscriptions[notification_type] and recipient._id in subscriptions[notification_type]):
                store_emails([recipient._id], notification_type, event, sender_user, node, timestamp, template=template, **context)
                sent_users.append(recipient._id)

    return sent_users
Exemplo n.º 17
0
def notify(event, user, node, timestamp, **context):
    """Retrieve appropriate ***subscription*** and passe user list

    :param event: event that triggered the notification
    :param user: user who triggered notification
    :param node: instance of Node
    :param timestamp: time event happened
    :param context: optional variables specific to templates
        target_user: used with comment_replies
    :return: List of user ids notifications were sent to
    """
    sent_users = []
    # The user who the current comment is a reply to
    target_user = context.get('target_user', None)
    exclude = context.get('exclude', [])
    # do not notify user who initiated the emails
    exclude.append(user._id)

    event_type = utils.find_subscription_type(event)
    if target_user and event_type in constants.USER_SUBSCRIPTIONS_AVAILABLE:
        # global user
        subscriptions = get_user_subscriptions(target_user, event_type)
    else:
        # local project user
        subscriptions = compile_subscriptions(node, event_type, event)

    for notification_type in subscriptions:
        if notification_type == 'none' or not subscriptions[notification_type]:
            continue
        # Remove excluded ids from each notification type
        subscriptions[notification_type] = [guid for guid in subscriptions[notification_type] if guid not in exclude]

        # If target, they get a reply email and are removed from the general email
        if target_user and target_user._id in subscriptions[notification_type]:
            subscriptions[notification_type].remove(target_user._id)
            store_emails([target_user._id], notification_type, 'comment_replies', user, node, timestamp, **context)
            sent_users.append(target_user._id)

        if subscriptions[notification_type]:
            store_emails(subscriptions[notification_type], notification_type, event_type, user, node, timestamp, **context)
            sent_users.extend(subscriptions[notification_type])
    return sent_users
Exemplo n.º 18
0
def reviews_submit_notification(self, recipients, context):
    # Avoid AppRegistryNotReady error
    from website.notifications.emails import get_user_subscriptions
    from website import settings

    event_type = utils.find_subscription_type('global_reviews')
    if context['reviewable'].provider._id == 'osf':
        context['logo'] = settings.OSF_PREPRINTS_LOGO
    else:
        context['logo'] = context['reviewable'].provider._id

    for recipient in recipients:
        user_subscriptions = get_user_subscriptions(recipient, event_type)
        context['no_future_emails'] = user_subscriptions['none']
        context['is_creator'] = recipient == context['reviewable'].node.creator
        context['provider_name'] = context['reviewable'].provider.name
        mails.send_mail(recipient.username,
                        mails.REVIEWS_SUBMISSION_CONFIRMATION,
                        mimetype='html',
                        user=recipient,
                        **context)