def post_sendgrid(url, data_params=None):
    """Make a sendgrid HTTPS post to a sendgrid url with some optional data.

    @param url: The sendgrid url to post to.
    @type url: str
    @param data_params: Any additional data parameters to send. User credentials
        are not required.
    @type data_params: dict
    @return: The response from sendgrid.
    @rtype: requests.models.Response
    """
    data = {
        'api_user': util.get_app_config()['SENDGRID_API_USERNAME'],
        'api_key': util.get_app_config()['SENDGRID_API_KEY']
    }
    if data_params:
        data.update(data_params)

    sendgrid_url = SENDGRID_BASE_API_URL % url
    response = requests.post(
        sendgrid_url,
        data=data
    )

    if response.status_code != 200:
        print "Sendgrid %d. url: %s, data_params: %s, reason: %s" % (
            response.status_code,
            sendgrid_url,
            str(data_params),
            response.text
        )
    return response
def get_db():
    """Get the local application database or test database.

    @return: The database, or the test database if the FAKE_MONGO config is True
    @type: flask_pymongo.wrappers.Database
    """
    if util.get_app_config()['FAKE_MONGO']:
        return FakeMongoDB.get_instance()

    return AppMongoKeeper.get_instance().get_mongo().db
Exemple #3
0
def get_db():
    """Get the local application database or test database.

    @return: The database, or the test database if the FAKE_MONGO config is True
    @type: flask_pymongo.wrappers.Database
    """
    if util.get_app_config()['FAKE_MONGO']:
        return FakeMongoDB.get_instance()

    return AppMongoKeeper.get_instance().get_mongo().db
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 get_lists():
    """Get all lists that are available for subscription.

    @return: Array of mailing lists.
    @rtype: iterable over str
    """
    if util.get_app_config()['FAKE_SENDGRID']:
        return FakeSendGrid.get_lists()

    response = post_sendgrid(SENDGRID_ACTION_URLS['GET_LISTS'])
    if response.status_code == 200:
        data = response.json()
        return [x['list'] for x in data]

    else:
        return []
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
def get_user_subscriptions(email):
    """Get all lists the specified user email is subscribed to.

    Get all lists the specified user email is subscribed to from the real
    SendGrid service or the fake SendGrid service if the FAKE_SENDGRID config is
    True.

    @param email: The user email for which to return list subscriptions.
    @type email: str
    @return: Array of mailing lists
    @rtype: iterable over str
    """
    if util.get_app_config()['FAKE_SENDGRID']:
        return FakeSendGrid.get_subscriptions(email)

    lists = get_lists()
    subscriptions = []
    for item in lists:
        if email in list_emails_subscribed_to_list(item):
            subscriptions.append(item)

    return subscriptions