Ejemplo n.º 1
0
def subscribe_user_to_global_notifications(user):
    notification_type = 'email_transactional'
    user_events = constants.USER_SUBSCRIPTIONS_AVAILABLE
    for user_event in user_events:
        user_event_id = to_subscription_key(user._id, user_event)

        subscription = NotificationSubscription(_id=user_event_id, owner=user, event_name=user_event)
        subscription.add_user_to_subscription(user, notification_type)
        subscription.save()
Ejemplo n.º 2
0
def subscribe_user_to_global_notifications(user):
    notification_type = 'email_transactional'
    user_events = constants.USER_SUBSCRIPTIONS_AVAILABLE
    for user_event in user_events:
        user_event_id = to_subscription_key(user._id, user_event)

        subscription = NotificationSubscription(_id=user_event_id,
                                                owner=user,
                                                event_name=user_event)
        subscription.add_user_to_subscription(user, notification_type)
        subscription.save()
Ejemplo n.º 3
0
def subscribe_user_to_notifications(node, user):
    """ Update the notification settings for the creator or contributors

    :param user: User to subscribe to notifications
    """

    if node.institution_id:
        raise InvalidSubscriptionError(
            'Institutions are invalid targets for subscriptions')

    if node.is_collection:
        raise InvalidSubscriptionError(
            'Collections are invalid targets for subscriptions')

    if node.is_deleted:
        raise InvalidSubscriptionError(
            'Deleted Nodes are invalid targets for subscriptions')

    events = constants.NODE_SUBSCRIPTIONS_AVAILABLE
    notification_type = 'email_transactional'
    target_id = node._id

    if user.is_registered:
        for event in events:
            event_id = to_subscription_key(target_id, event)
            global_event_id = to_subscription_key(user._id, 'global_' + event)
            global_subscription = NotificationSubscription.load(
                global_event_id)

            subscription = NotificationSubscription.load(event_id)

            # If no subscription for component and creator is the user, do not create subscription
            # If no subscription exists for the component, this means that it should adopt its
            # parent's settings
            if not (node and node.parent_node and not subscription
                    and node.creator == user):
                if not subscription:
                    subscription = NotificationSubscription(_id=event_id,
                                                            owner=node,
                                                            event_name=event)
                if global_subscription:
                    global_notification_type = get_global_notification_type(
                        global_subscription, user)
                    subscription.add_user_to_subscription(
                        user, global_notification_type)
                else:
                    subscription.add_user_to_subscription(
                        user, notification_type)
                subscription.save()
def add_global_subscriptions():

    notification_type = 'email_transactional'
    user_events = constants.USER_SUBSCRIPTIONS_AVAILABLE

    for user in models.User.find():
        if user.is_active and user.is_registered:
            for user_event in user_events:
                user_event_id = to_subscription_key(user._id, user_event)

                subscription = NotificationSubscription.load(user_event_id)
                if not subscription:
                    subscription = NotificationSubscription(_id=user_event_id, owner=user, event_name=user_event)
                    subscription.add_user_to_subscription(user, notification_type)
                    subscription.save()
                    logger.info('No subscription found. {} created.'.format(subscription))
                else:
                    logger.info('Subscription {} found.'.format(subscription))
Ejemplo n.º 5
0
def add_global_subscriptions():

    notification_type = 'email_transactional'
    user_events = constants.USER_SUBSCRIPTIONS_AVAILABLE

    for user in models.User.find():
        if user.is_active and user.is_registered:
            for user_event in user_events:
                user_event_id = to_subscription_key(user._id, user_event)

                subscription = NotificationSubscription.load(user_event_id)
                if not subscription:
                    subscription = NotificationSubscription(
                        _id=user_event_id, owner=user, event_name=user_event)
                    subscription.add_user_to_subscription(
                        user, notification_type)
                    subscription.save()
                    logger.info('No subscription found. {} created.'.format(
                        subscription))
                else:
                    logger.info('Subscription {} found.'.format(subscription))
Ejemplo n.º 6
0
def subscribe_user_to_notifications(node, user):
    """ Update the notification settings for the creator or contributors

    :param user: User to subscribe to notifications
    """

    if node.institution_id:
        raise InvalidSubscriptionError('Institutions are invalid targets for subscriptions')

    if node.is_collection:
        raise InvalidSubscriptionError('Collections are invalid targets for subscriptions')

    if node.is_deleted:
        raise InvalidSubscriptionError('Deleted Nodes are invalid targets for subscriptions')

    events = constants.NODE_SUBSCRIPTIONS_AVAILABLE
    notification_type = 'email_transactional'
    target_id = node._id

    if user.is_registered:
        for event in events:
            event_id = to_subscription_key(target_id, event)
            global_event_id = to_subscription_key(user._id, 'global_' + event)
            global_subscription = NotificationSubscription.load(global_event_id)

            subscription = NotificationSubscription.load(event_id)

            # If no subscription for component and creator is the user, do not create subscription
            # If no subscription exists for the component, this means that it should adopt its
            # parent's settings
            if not(node and node.parent_node and not subscription and node.creator == user):
                if not subscription:
                    subscription = NotificationSubscription(_id=event_id, owner=node, event_name=event)
                if global_subscription:
                    global_notification_type = get_global_notification_type(global_subscription, user)
                    subscription.add_user_to_subscription(user, global_notification_type)
                else:
                    subscription.add_user_to_subscription(user, notification_type)
                subscription.save()
Ejemplo n.º 7
0
def add_global_subscriptions(dry=True):
    OSFUser = apps.get_model('osf.OSFUser')
    notification_type = 'email_transactional'
    user_events = constants.USER_SUBSCRIPTIONS_AVAILABLE

    count = 0

    with transaction.atomic():
        for user in OSFUser.objects.filter(is_registered=True,
                                           date_confirmed__isnull=False):
            changed = False
            if not user.is_active:
                continue
            for user_event in user_events:
                user_event_id = to_subscription_key(user._id, user_event)

                subscription = NotificationSubscription.load(user_event_id)
                if not subscription:
                    logger.info(
                        'No {} subscription found for user {}. Subscribing...'.
                        format(user_event, user._id))
                    subscription = NotificationSubscription(
                        _id=user_event_id, owner=user, event_name=user_event)
                    subscription.save(
                    )  # Need to save in order to access m2m fields
                    subscription.add_user_to_subscription(
                        user, notification_type)
                    subscription.save()
                    changed = True
                else:
                    logger.info('User {} already has a {} subscription'.format(
                        user._id, user_event))
            if changed:
                count += 1

        logger.info('Added subscriptions for {} users'.format(count))
        if dry:
            raise RuntimeError('Dry mode -- rolling back transaction')
Ejemplo n.º 8
0
def configure_subscription(auth):
    user = auth.user
    json_data = request.get_json()
    target_id = json_data.get('id')
    event = json_data.get('event')
    notification_type = json_data.get('notification_type')

    if not event or (notification_type not in NOTIFICATION_TYPES
                     and notification_type != 'adopt_parent'):
        raise HTTPError(
            http.BAD_REQUEST,
            data=dict(
                message_long=
                "Must provide an event and notification type for subscription."
            ))

    node = Node.load(target_id)
    event_id = utils.to_subscription_key(target_id, event)

    if not node:
        # if target_id is not a node it currently must be the current user
        if not target_id == user._id:
            sentry.log_message('{!r} attempted to subscribe to either a bad '
                               'id or non-node non-self id, {}'.format(
                                   user, target_id))
            raise HTTPError(http.NOT_FOUND)

        if notification_type == 'adopt_parent':
            sentry.log_message(
                '{!r} attempted to adopt_parent of a none node id, {}'.format(
                    user, target_id))
            raise HTTPError(http.BAD_REQUEST)
        owner = user
    else:
        if not node.has_permission(user, 'read'):
            sentry.log_message(
                '{!r} attempted to subscribe to private node, {}'.format(
                    user, target_id))
            raise HTTPError(http.FORBIDDEN)

        if notification_type != 'adopt_parent':
            owner = node
        else:
            parent = node.parent_node
            if not parent:
                sentry.log_message('{!r} attempted to adopt_parent of '
                                   'the parentless project, {!r}'.format(
                                       user, node))
                raise HTTPError(http.BAD_REQUEST)

            # If adopt_parent make sure that this subscription is None for the current User
            subscription = NotificationSubscription.load(event_id)
            if not subscription:
                return {}  # We're done here

            subscription.remove_user_from_subscription(user)
            return {}

    subscription = NotificationSubscription.load(event_id)

    if not subscription:
        subscription = NotificationSubscription(_id=event_id,
                                                owner=owner,
                                                event_name=event)

    subscription.add_user_to_subscription(user, notification_type)

    subscription.save()

    return {
        'message':
        'Successfully subscribed to {} list on {}'.format(
            notification_type, event_id)
    }
Ejemplo n.º 9
0
def configure_subscription(auth):
    user = auth.user
    json_data = request.get_json()
    target_id = json_data.get('id')
    event = json_data.get('event')
    notification_type = json_data.get('notification_type')

    if not event or (notification_type not in NOTIFICATION_TYPES and notification_type != 'adopt_parent'):
        raise HTTPError(http.BAD_REQUEST, data=dict(
            message_long="Must provide an event and notification type for subscription.")
        )

    node = Node.load(target_id)
    event_id = utils.to_subscription_key(target_id, event)

    if not node:
        # if target_id is not a node it currently must be the current user
        if not target_id == user._id:
            sentry.log_message(
                '{!r} attempted to subscribe to either a bad '
                'id or non-node non-self id, {}'.format(user, target_id)
            )
            raise HTTPError(http.NOT_FOUND)

        if notification_type == 'adopt_parent':
            sentry.log_message(
                '{!r} attempted to adopt_parent of a none node id, {}'.format(user, target_id)
            )
            raise HTTPError(http.BAD_REQUEST)
        owner = user
    else:
        if not node.has_permission(user, 'read'):
            sentry.log_message('{!r} attempted to subscribe to private node, {}'.format(user, target_id))
            raise HTTPError(http.FORBIDDEN)

        if notification_type != 'adopt_parent':
            owner = node
        else:
            parent = node.parent_node
            if not parent:
                sentry.log_message(
                    '{!r} attempted to adopt_parent of '
                    'the parentless project, {!r}'.format(user, node)
                )
                raise HTTPError(http.BAD_REQUEST)

            # If adopt_parent make sure that this subscription is None for the current User
            subscription = NotificationSubscription.load(event_id)
            if not subscription:
                return {}  # We're done here

            subscription.remove_user_from_subscription(user)
            return {}

    subscription = NotificationSubscription.load(event_id)

    if not subscription:
        subscription = NotificationSubscription(_id=event_id, owner=owner, event_name=event)

    subscription.add_user_to_subscription(user, notification_type)

    subscription.save()

    return {'message': 'Successfully subscribed to {} list on {}'.format(notification_type, event_id)}