original_docstring: Subscribes a user to basket newsletters.
  
      The email to subscribe is provided either from the result of the lookup task
      or from the profile of the user.
      
docstring: Subscribes a user to basket newsletters.
  The email to subscribe is provided either from the result of the lookup task
  or from the profile of the user.
docstring_tokens: Subscribes a user to basket newsletters . The email to subscribe is provided either from the result of the lookup task or from the profile of the user .
code: def subscribe_user_task(self, result, email='', newsletters=[], sync='N', optin='Y'):
      """Subscribes a user to basket newsletters.
  
      The email to subscribe is provided either from the result of the lookup task
      or from the profile of the user.
      """
  
      if not result and not email:
          return None
  
      from mozillians.users.models import UserProfile
      profile = get_object_or_none(UserProfile, user__email=email)
  
      newsletters_to_subscribe = []
      if result.get('status') == 'ok':
          # This is used when we want to subscribe a different email
          # than the one in the lookup (eg when a user changes emails)
          if not email:
              email = result.get('email')
  
          if newsletters:
              newsletters_to_subscribe = list(set(newsletters) - set(result['newsletters']))
          else:
              # This case is used when a user changes email.
              # The lookup task will provide the newsletters that the user was registered.
              # We need to find the common with the mozillians newsletters and
              # subscribe the email provided as an argument.
              newsletters_to_subscribe = list(set(MOZILLIANS_NEWSLETTERS)
                                              .intersection(result['newsletters']))
  
      # The lookup failed because the user does not exist. We have a new user!
      if (result.get('status') == 'error'
              and result.get('desc') == u'User not found' and newsletters):
          newsletters_to_subscribe = newsletters
  
      if newsletters_to_subscribe:
          try:
              kwargs = {
                  'sync': sync,
                  'source_url': MOZILLIANS_URL,
                  'optin': optin,
                  'api_key': BASKET_API_KEY
              }
  
              if profile and profile.country:
                  kwargs['country'] = profile.country.code2
  
              subscribe_result = basket.subscribe(email,
                                                  newsletters_to_subscribe,
                                                  **kwargs)
          except MaxRetriesExceededError as exc:
              raise exc
          except basket.BasketException as exc:
              raise self.retry(exc=exc)
          return subscribe_result
      return None
code_tokens: def subscribe_user_task ( self , result , email = '' , newsletters = [ ] , sync = 'N' , optin = 'Y' ) : if not result and not email : return None from mozillians . users . models import UserProfile profile = get_object_or_none ( UserProfile , user__email = email ) newsletters_to_subscribe = [ ] if result . get ( 'status' ) == 'ok' : if not email : email = result . get ( 'email' ) if newsletters : newsletters_to_subscribe = list ( set ( newsletters ) - set ( result [ 'newsletters' ] ) ) else : newsletters_to_subscribe = list ( set ( MOZILLIANS_NEWSLETTERS ) . intersection ( result [ 'newsletters' ] ) ) if ( result . get ( 'status' ) == 'error' and result . get ( 'desc' ) == u'User not found' and newsletters ) : newsletters_to_subscribe = newsletters if newsletters_to_subscribe : try : kwargs = { 'sync' : sync , 'source_url' : MOZILLIANS_URL , 'optin' : optin , 'api_key' : BASKET_API_KEY } if profile and profile . country : kwargs [ 'country' ] = profile . country . code2 subscribe_result = basket . subscribe ( email , newsletters_to_subscribe , ** kwargs ) except MaxRetriesExceededError as exc : raise exc except basket . BasketException as exc : raise self . retry ( exc = exc ) return subscribe_result return None
short_docstring: Subscribes a user to basket newsletters.
short_docstring_tokens: Subscribes a user to basket newsletters .
comment: """Subscribes a user to basket newsletters.
  
      The email to subscribe is provided either from the result of the lookup task
      or from the profile of the user.
      """$SEP$# This is used when we want to subscribe a different email$SEP$# than the one in the lookup (eg when a user changes emails)$SEP$# This case is used when a user changes email.$SEP$# The lookup task will provide the newsletters that the user was registered.$SEP$# We need to find the common with the mozillians newsletters and$SEP$# subscribe the email provided as an argument.$SEP$# The lookup failed because the user does not exist. We have a new user!