def update_descriptions(new_descriptions):
    """Upserts the list descriptions for the application.

    @param new_descriptions: The new list descriptions to set, of the form: {
        'listname 0': {'description': 'description text 0 ...'},
        'listname 1': {'description': 'description text 1 ...'},
        ...
    }
    @type new_descriptions: iterable over list descriptions.
    """
    old_record = get_descriptions()
    if old_record:
        the_id = old_record.get('_id', None)
    else:
        the_id = None

    if the_id:
        new_descriptions['_id'] = the_id
    descriptions = dict(
        map(
            lambda (k, v): (k.replace('.', '_dot_'), v),
            new_descriptions.items()
        )
    )
    get_db().subscriptions.save(descriptions)

    util.get_redis_connection().flushall()

    # get_user_subscriptions updates the cache
    subscriptions_service.get_user_subscriptions('*****@*****.**')
Beispiel #2
0
def update_descriptions(new_descriptions):
    """Upserts the list descriptions for the application.

    @param new_descriptions: The new list descriptions to set, of the form: {
        'listname 0': {'description': 'description text 0 ...'},
        'listname 1': {'description': 'description text 1 ...'},
        ...
    }
    @type new_descriptions: iterable over list descriptions.
    """
    old_record = get_descriptions()
    if old_record:
        the_id = old_record.get('_id', None)
    else:
        the_id = None

    if the_id:
        new_descriptions['_id'] = the_id
    descriptions = dict(
        map(lambda (k, v): (k.replace('.', '_dot_'), v),
            new_descriptions.items()))
    get_db().subscriptions.save(descriptions)

    util.get_redis_connection().flushall()

    # get_user_subscriptions updates the cache
    subscriptions_service.get_user_subscriptions('*****@*****.**')
def subscribe(email, new_subscriptions):
    """Subscribe a user for a given set of lists.

    Subscribe a user for a given set of lists using the real SendGrid service or
    the fake SendGrid service if the FAKE_SENDGRID config is True.

    @param email: email address corresponding to a user
    @type email: str
    @parm new_subscriptions: the subscriptions to subscribe the user
        from.
    @type new_subscriptions: iterable over str
    @return: The first failing response from SendGrid, the last successful
        response from SendGrid, or None if new_subscriptions is len() = 0
    @rtype: requests.models.Response
    """
    if util.get_app_config()['FAKE_SENDGRID']:
        return FakeSendGrid.subscribe(email, new_subscriptions)

    data_str = """{
        "email":"%s",
        "name":"%s"
    }""" % (
        email,
        email.split('@')[0] # Get up until the @ and send that as the name
    )
    response = None
    for subscription in new_subscriptions:
        # Subscribe via SendGrid
        response = post_sendgrid(
            SENDGRID_ACTION_URLS['ADD_USER_IN_LIST'],
            {
                'list': subscription.replace('_dot_', '.'),
                'data': data_str
            }
        )
        if response.status_code != 200:
            return response

        # Update the main cache
        redis = util.get_redis_connection()
        upsert_list(redis, 'union', 'list_emails_subscribed_to_list',
            subscription, value=subscription)

        # Upsert a per user cache
        upsert_list(redis, 'union', 'get_user_subscriptions', email,
            value=subscription)

    return response
def unsubscribe(email, cancel_subscriptions):
    """Unsubscribe a user for a given set of lists.

    Unsubscribe a user for a given set of lists using the real SendGrid service
    or the fake SendGrid service if the FAKE_SENDGRID config is True.

    @param email: email address corresponding to a user
    @type email: str
    @parm cancel_subscriptions: the subscriptions to unsubscribe the user
        from.
    @type cancel_subscriptions: iterable over str
    @return: The first failing response from SendGrid, the last successful
        response from SendGrid, or None if cancel_subscriptions is len() = 0
    @rtype: requests.models.Response
    """
    if util.get_app_config()['FAKE_SENDGRID']:
        return FakeSendGrid.unsubscribe(email, cancel_subscriptions)

    response = None
    for subscription in cancel_subscriptions:
        # Unsubscribe via SendGrid
        response = post_sendgrid(
            SENDGRID_ACTION_URLS['DELETE_USER_IN_LIST'],
            {
                'list': subscription.replace('_dot_', '.'),
                'email': email
            }
        )
        if response.status_code != 200:
            return response

        # Update the main cache
        redis = util.get_redis_connection()
        upsert_list(redis, 'difference', 'list_emails_subscribed_to_list',
            subscription, value=subscription)

        # Upsert the per user cache
        upsert_list(redis, 'difference', 'get_user_subscriptions', email,
            value=subscription)

    return response