Repository: mozmeao/basket

Issue:
adding a new event type to basket
For the foundation website's newsletter(s) signup functionality, we'd like to send signup events on a new event type. We're currently using `crm_petition_data` for mofo petitions, but we'd like to also have a `newsletter_signup_data` event type for dealing with (comma-separated) newsletter(s) that users can register for.


Discussion:
code that was already in place: https://github.com/mozilla/foundation.mozilla.org/blob/master/network-api/networkapi/campaign/views.py#L156-L166

new (currently inactive) code that will use the new event type: https://github.com/mozilla/foundation.mozilla.org/blob/master/network-api/networkapi/campaign/views.py#L93-L103
That's good enough for me. Thanks!
@Pomax there are some other fields that we can accept and would be nice if your system knows them. 

The full list is in the [basket docs](https://basket.readthedocs.io/newsletter_api.html#news-subscribe), but the important ones are `lang` and `format`. `country` also comes in handy if you happen to gather that info, but isn't required at all. 
Ah, good to know. 

Unfortunately, for newsletters all we have right now is email address (users are not asked to supply any additional information at the moment, so if we want to supply the additional information we will first need to figure out how to update the site component design to allow for gathering that data)

I'll file an issue on the foundation repo for us to think about whether/how we can capture that data.
@Pomax it would be best then to send the language of the page (or just hard-code `en`) or to capture the user's `accept-language` header value and send that. If no language is sent then no language is recorded and people may not receive a newsletter at all since they usually use language to segment sends.
HTML is the default for `format`, so that's okay, and I don't think `country` is used much outside of advocacy things that are country specific. 
you can see the form at https://www.mozilla.org/en-US/newsletter/ for the standard info we request at signup. 
gotcha, I've filed https://github.com/mozilla/foundation.mozilla.org/pull/2992 to make sure the `lang` and `format` fields will have a value, pending "real" form redesign

Resolving patch:
diff --git a/basket/news/management/commands/process_donations_queue.py b/basket/news/management/commands/process_donations_queue.py
--- a/basket/news/management/commands/process_donations_queue.py
+++ b/basket/news/management/commands/process_donations_queue.py
@@ -15,6 +15,7 @@
 from basket.news.tasks import (
     process_donation,
     process_donation_event,
+    process_newsletter_subscribe,
     process_petition_signature,
 )
 
@@ -22,6 +23,7 @@
 EVENT_TYPES = {
     'donation': process_donation,
     'crm_petition_data': process_petition_signature,
+    'newsletter_signup_data': process_newsletter_subscribe,
     'DEFAULT': process_donation_event,
 }
 
diff --git a/basket/news/tasks.py b/basket/news/tasks.py
--- a/basket/news/tasks.py
+++ b/basket/news/tasks.py
@@ -31,7 +31,7 @@
 from basket.news.newsletters import get_sms_vendor_id, get_transactional_message_ids, newsletter_map
 from basket.news.utils import (generate_token, get_accept_languages, get_best_language, get_user_data,
                                iso_format_unix_timestamp, parse_newsletters, parse_newsletters_csv,
-                               SUBSCRIBE, UNSUBSCRIBE)
+                               SUBSCRIBE, UNSUBSCRIBE, get_best_supported_lang)
 
 log = logging.getLogger(__name__)
 
@@ -928,6 +928,12 @@ def process_donation(data):
             raise
 
 
+@et_task
+def process_newsletter_subscribe(data):
+    data['lang'] = get_best_supported_lang(data['lang'])
+    upsert_user(SUBSCRIBE, data)
+
+
 PETITION_CONTACT_FIELDS = [
     'first_name',
     'last_name',


Test patch: