Esempio n. 1
0
def parse_newsletters(record, api_call_type, newsletters, cur_newsletters):
    """Utility function to take a list of newsletters and according
    the type of action (subscribe, unsubscribe, and set) set the
    appropriate flags in `record` which is a dict of parameters that
    will be sent to the email provider.

    Parameters are only set for the newsletters whose subscription
    status needs to change, so that we don't unnecessarily update the
    last modification timestamp of newsletters.

    :param dict record: Parameters that will be sent to ET
    :param integer api_call_type: SUBSCRIBE means add these newsletters to the
        user's subscriptions if not already there, UNSUBSCRIBE means remove
        these newsletters from the user's subscriptions if there, and SET
        means change the user's subscriptions to exactly this set of
        newsletters.
    :param list newsletters: List of the slugs of the newsletters to be
        subscribed, unsubscribed, or set.
    :param set cur_newsletters: Set of the slugs of the newsletters that
        the user is currently subscribed to. None if there was an error.
    :returns: (to_subscribe, to_unsubscribe) - lists of slugs of the
        newsletters that we will request new subscriptions to, or request
        unsubscription from, respectively.
    """

    to_subscribe = []
    to_unsubscribe = []

    if api_call_type == SUBSCRIBE:
        grouped_newsletters = set()
        for nl in newsletters:
            group_nl = newsletter_group_newsletter_slugs(nl)
            if group_nl:
                grouped_newsletters.update(group_nl)
            else:
                grouped_newsletters.add(nl)

        newsletters = list(grouped_newsletters)

    if api_call_type == SUBSCRIBE or api_call_type == SET:
        # Subscribe the user to these newsletters if not already
        for nl in newsletters:
            name = newsletter_field(nl)
            if name and (cur_newsletters is None or
                                 nl not in cur_newsletters):
                record['%s_FLG' % name] = 'Y'
                record['%s_DATE' % name] = date.today().strftime('%Y-%m-%d')
                to_subscribe.append(nl)

    if api_call_type == UNSUBSCRIBE or api_call_type == SET:
        # Unsubscribe the user to these newsletters

        if api_call_type == SET:
            # Unsubscribe from the newsletters currently subscribed to
            # but not in the new list
            if cur_newsletters is not None:
                unsubs = cur_newsletters - set(newsletters)
            else:
                subs = set(newsletters)
                all = set(newsletter_slugs())
                unsubs = all - subs
        else:  # type == UNSUBSCRIBE
            # unsubscribe from the specified newsletters
            unsubs = newsletters

        for nl in unsubs:
            # Unsubscribe from any unsubs that the user is currently subbed to
            name = newsletter_field(nl)
            if name and (cur_newsletters is None or nl in cur_newsletters):
                record['%s_FLG' % name] = 'N'
                record['%s_DATE' % name] = date.today().strftime('%Y-%m-%d')
                to_unsubscribe.append(nl)
    return to_subscribe, to_unsubscribe
Esempio n. 2
0
 def test_newsletter_group_newsletter_slugs(self):
     self.assertEqual(set(newsletters.newsletter_group_newsletter_slugs('bowling')),
                      set(['extorting', 'surfing']))
Esempio n. 3
0
def parse_newsletters(api_call_type, newsletters, cur_newsletters):
    """Utility function to take a list of newsletters and according
    the type of action (subscribe, unsubscribe, and set) set the
    appropriate flags in `record` which is a dict of parameters that
    will be sent to the email provider.

    Parameters are only set for the newsletters whose subscription
    status needs to change, so that we don't unnecessarily update the
    last modification timestamp of newsletters.

    :param integer api_call_type: SUBSCRIBE means add these newsletters to the
        user's subscriptions if not already there, UNSUBSCRIBE means remove
        these newsletters from the user's subscriptions if there, and SET
        means change the user's subscriptions to exactly this set of
        newsletters.
    :param list newsletters: List of the slugs of the newsletters to be
        subscribed, unsubscribed, or set.
    :param list cur_newsletters: List of the slugs of the newsletters that
        the user is currently subscribed to. None if there was an error.
    :returns: dict of slugs of the newsletters with boolean values: True for subscriptions,
        and False for unsubscription.
    """
    newsletter_map = {}
    newsletters = set(newsletters)
    if cur_newsletters is None:
        cur_newsletters = set()
    else:
        cur_newsletters = set(cur_newsletters)
        if api_call_type == SET:
            # don't mess with inactive newsletters on a full update
            cur_newsletters -= set(newsletter_inactive_slugs())

    if api_call_type == SUBSCRIBE:
        grouped_newsletters = set()
        for nl in newsletters:
            group_nl = newsletter_group_newsletter_slugs(nl)
            if group_nl:
                grouped_newsletters.update(group_nl)
            else:
                grouped_newsletters.add(nl)

        newsletters = grouped_newsletters

    if api_call_type == SUBSCRIBE or api_call_type == SET:
        # Subscribe the user to these newsletters if not already
        subs = newsletters - cur_newsletters
        for nl in subs:
            newsletter_map[nl] = True

    if api_call_type == UNSUBSCRIBE or api_call_type == SET:
        # Unsubscribe the user to these newsletters

        if api_call_type == SET:
            # Unsubscribe from the newsletters currently subscribed to
            # but not in the new list
            if cur_newsletters:
                unsubs = cur_newsletters - newsletters
            else:
                unsubs = []
        else:  # type == UNSUBSCRIBE
            # unsubscribe from the specified newsletters
            if cur_newsletters:
                unsubs = newsletters & cur_newsletters
            else:
                # we might not be subscribed to anything,
                # or just didn't get user data. default to everything.
                unsubs = newsletters

        for nl in unsubs:
            newsletter_map[nl] = False

    return newsletter_map
Esempio n. 4
0
 def test_newsletter_group_newsletter_slugs(self):
     self.assertEqual(
         set(newsletters.newsletter_group_newsletter_slugs('bowling')),
         {'extorting', 'surfing'})