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