Esempio n. 1
0
def send_to_device_ajax(request):
    locale = l10n_utils.get_locale(request)
    phone_or_email = request.POST.get('phone-or-email')
    if not phone_or_email:
        return HttpResponseJSON({'success': False, 'errors': ['phone-or-email']})

    data = {
        'platform': request.POST.get('platform'),
    }

    data_type = 'email' if '@' in phone_or_email else 'number'
    data[data_type] = phone_or_email
    form = SendToDeviceWidgetForm(data)

    if form.is_valid():
        phone_or_email = form.cleaned_data.get(data_type)
        platform = form.cleaned_data.get('platform')

        # check for customized widget and update email/sms
        # message if conditions match
        send_to_device_basket_id = request.POST.get('send-to-device-basket-id')
        if (platform == 'android' and
                send_to_device_basket_id == 'android-embed'):

            platform = 'android-embed'

        if data_type == 'number':
            if platform in SMS_MESSAGES:
                try:
                    basket.send_sms(phone_or_email, SMS_MESSAGES[platform])
                except basket.BasketException:
                    return HttpResponseJSON({'success': False, 'errors': ['system']},
                                            status=400)
            else:
                # TODO define all platforms in SMS_MESSAGES
                return HttpResponseJSON({'success': False, 'errors': ['platform']})
        else:  # email
            if platform in EMAIL_MESSAGES:
                try:
                    basket.subscribe(phone_or_email, EMAIL_MESSAGES[platform],
                                     source_url=request.POST.get('source-url'),
                                     lang=locale)
                except basket.BasketException:
                    return HttpResponseJSON({'success': False, 'errors': ['system']},
                                            status=400)
            else:
                # TODO define all platforms in EMAIL_MESSAGES
                return HttpResponseJSON({'success': False, 'errors': ['platform']})

        resp_data = {'success': True}
    else:
        resp_data = {
            'success': False,
            'errors': form.errors.keys(),
        }

    return HttpResponseJSON(resp_data)
Esempio n. 2
0
def send_to_device_ajax(request):
    locale = l10n_utils.get_locale(request)
    phone_or_email = request.POST.get("phone-or-email")
    if not phone_or_email:
        return HttpResponseJSON({"success": False, "errors": ["phone-or-email"]})

    data = {"platform": request.POST.get("platform")}

    data_type = "email" if "@" in phone_or_email else "number"
    data[data_type] = phone_or_email
    form = SendToDeviceWidgetForm(data)

    if form.is_valid():
        phone_or_email = form.cleaned_data.get(data_type)
        platform = form.cleaned_data.get("platform")

        # check for customized widget and update email/sms
        # message if conditions match
        send_to_device_basket_id = request.POST.get("send-to-device-basket-id")
        if platform == "android" and send_to_device_basket_id == "android-embed":

            platform = "android-embed"

        if data_type == "number":
            if platform in SMS_MESSAGES:
                try:
                    basket.send_sms(phone_or_email, SMS_MESSAGES[platform])
                except basket.BasketException:
                    return HttpResponseJSON({"success": False, "errors": ["system"]}, status=400)
            else:
                # TODO define all platforms in SMS_MESSAGES
                return HttpResponseJSON({"success": False, "errors": ["platform"]})
        else:  # email
            if platform in EMAIL_MESSAGES:
                try:
                    basket.subscribe(
                        phone_or_email, EMAIL_MESSAGES[platform], source_url=request.POST.get("source-url"), lang=locale
                    )
                except basket.BasketException:
                    return HttpResponseJSON({"success": False, "errors": ["system"]}, status=400)
            else:
                # TODO define all platforms in EMAIL_MESSAGES
                return HttpResponseJSON({"success": False, "errors": ["platform"]})

        resp_data = {"success": True}
    else:
        resp_data = {"success": False, "errors": form.errors.keys()}

    return HttpResponseJSON(resp_data)
Esempio n. 3
0
def send_to_device_ajax(request):
    locale = l10n_utils.get_locale(request)
    phone_or_email = request.POST.get('phone-or-email')

    # ensure a value was entered in phone or email field
    if not phone_or_email:
        return HttpResponseJSON({'success': False, 'errors': ['phone-or-email']})

    # pull message set from POST (not part of form, so wont be in cleaned_data)
    message_set = request.POST.get('message-set', 'default')

    # begin collecting data to pass to form constructor
    data = {
        'platform': request.POST.get('platform'),
    }

    # determine if email or phone number was submitted
    data_type = 'email' if '@' in phone_or_email else 'number'

    # populate data type in form data dict
    data[data_type] = phone_or_email

    # instantiate the form with processed POST data
    form = SendToDeviceWidgetForm(data)

    if form.is_valid():
        phone_or_email = form.cleaned_data.get(data_type)
        platform = form.cleaned_data.get('platform')

        # if no platform specified, default to 'all'
        if not platform:
            platform = 'all'

        # ensure we have a valid message set. if not, fall back to default
        if message_set not in SEND_TO_DEVICE_MESSAGE_SETS:
            MESSAGES = SEND_TO_DEVICE_MESSAGE_SETS['default']
        else:
            MESSAGES = SEND_TO_DEVICE_MESSAGE_SETS[message_set]

        if data_type == 'number':
            if platform in MESSAGES['sms']:
                data = {
                    'mobile_number': phone_or_email,
                    'msg_name': MESSAGES['sms'][platform],
                    'lang': locale,
                }
                country = request.POST.get('country')
                if country and re.match(r'^[a-z]{2}$', country, flags=re.I):
                    data['country'] = country

                try:
                    basket.request('post', 'subscribe_sms', data=data)
                except basket.BasketException as e:
                    if e.desc == 'mobile_number is invalid':
                        return HttpResponseJSON({'success': False, 'errors': ['number']})
                    else:
                        return HttpResponseJSON({'success': False, 'errors': ['system']},
                                                status=400)
            else:
                return HttpResponseJSON({'success': False, 'errors': ['platform']})
        else:  # email
            if platform in MESSAGES['email']:
                try:
                    basket.subscribe(phone_or_email, MESSAGES['email'][platform],
                                     source_url=request.POST.get('source-url'),
                                     lang=locale)
                except basket.BasketException:
                    return HttpResponseJSON({'success': False, 'errors': ['system']},
                                            status=400)
            else:
                return HttpResponseJSON({'success': False, 'errors': ['platform']})

        resp_data = {'success': True}
    else:
        resp_data = {
            'success': False,
            'errors': form.errors.keys(),
        }

    return HttpResponseJSON(resp_data)
Esempio n. 4
0
def send_to_device_ajax(request):
    locale = l10n_utils.get_locale(request)
    phone_or_email = request.POST.get('phone-or-email')

    # ensure a value was entered in phone or email field
    if not phone_or_email:
        return JsonResponse({'success': False, 'errors': ['phone-or-email']})

    # pull message set from POST (not part of form, so wont be in cleaned_data)
    message_set = request.POST.get('message-set', 'default')

    # begin collecting data to pass to form constructor
    data = {'platform': request.POST.get('platform')}

    # determine if email or phone number was submitted
    data_type = 'email' if '@' in phone_or_email else 'number'

    # populate data type in form data dict
    data[data_type] = phone_or_email

    # instantiate the form with processed POST data
    form = SendToDeviceWidgetForm(data)

    if form.is_valid():
        phone_or_email = form.cleaned_data.get(data_type)
        platform = form.cleaned_data.get('platform')

        # if no platform specified, default to 'all'
        if not platform:
            platform = 'all'

        # ensure we have a valid message set. if not, fall back to default
        if message_set not in SEND_TO_DEVICE_MESSAGE_SETS:
            MESSAGES = SEND_TO_DEVICE_MESSAGE_SETS['default']
        else:
            MESSAGES = SEND_TO_DEVICE_MESSAGE_SETS[message_set]

        if data_type == 'number':

            # for testing purposes return success
            if phone_or_email == '5555555555':
                return JsonResponse({'success': True})

            if platform in MESSAGES['sms']:
                data = {
                    'mobile_number': phone_or_email,
                    'msg_name': MESSAGES['sms'][platform],
                    'lang': locale,
                }
                country = request.POST.get('country')
                if country and re.match(r'^[a-z]{2}$', country, flags=re.I):
                    data['country'] = country

                try:
                    basket.request('post', 'subscribe_sms', data=data)
                except basket.BasketException as e:
                    if e.desc == 'mobile_number is invalid':
                        return JsonResponse({
                            'success': False,
                            'errors': ['number']
                        })
                    else:
                        return JsonResponse(
                            {
                                'success': False,
                                'errors': ['system']
                            },
                            status=400)
            else:
                return JsonResponse({'success': False, 'errors': ['platform']})
        else:  # email
            if platform in MESSAGES['email']:
                try:
                    basket.subscribe(
                        phone_or_email,
                        MESSAGES['email'][platform],
                        source_url=request.POST.get('source-url'),
                        lang=locale,
                    )
                except basket.BasketException:
                    return JsonResponse(
                        {
                            'success': False,
                            'errors': ['system']
                        }, status=400)
            else:
                return JsonResponse({'success': False, 'errors': ['platform']})

        resp_data = {'success': True}
    else:
        resp_data = {'success': False, 'errors': list(form.errors)}

    return JsonResponse(resp_data)
Esempio n. 5
0
def send_to_device_ajax(request):
    locale = l10n_utils.get_locale(request)
    phone_or_email = request.POST.get('phone-or-email')

    # ensure a value was entered in phone or email field
    if not phone_or_email:
        return HttpResponseJSON({
            'success': False,
            'errors': ['phone-or-email']
        })

    # pull message set from POST (not part of form, so wont be in cleaned_data)
    message_set = request.POST.get('message-set', 'default')

    # begin collecting data to pass to form constructor
    data = {
        'platform': request.POST.get('platform'),
    }

    # determine if email or phone number was submitted
    data_type = 'email' if '@' in phone_or_email else 'number'

    # populate data type in form data dict
    data[data_type] = phone_or_email

    # instantiate the form with processed POST data
    form = SendToDeviceWidgetForm(data)

    if form.is_valid():
        phone_or_email = form.cleaned_data.get(data_type)
        platform = form.cleaned_data.get('platform')

        # if no platform specified, default to 'all'
        if not platform:
            platform = 'all'

        # ensure we have a valid message set. if not, fall back to default
        if message_set not in SEND_TO_DEVICE_MESSAGE_SETS:
            MESSAGES = SEND_TO_DEVICE_MESSAGE_SETS['default']
        else:
            MESSAGES = SEND_TO_DEVICE_MESSAGE_SETS[message_set]

        if data_type == 'number':
            if platform in MESSAGES['sms']:
                try:
                    basket.send_sms(phone_or_email, MESSAGES['sms'][platform])
                except basket.BasketException:
                    return HttpResponseJSON(
                        {
                            'success': False,
                            'errors': ['system']
                        }, status=400)
            else:
                return HttpResponseJSON({
                    'success': False,
                    'errors': ['platform']
                })
        else:  # email
            if platform in MESSAGES['email']:
                try:
                    basket.subscribe(phone_or_email,
                                     MESSAGES['email'][platform],
                                     source_url=request.POST.get('source-url'),
                                     lang=locale)
                except basket.BasketException:
                    return HttpResponseJSON(
                        {
                            'success': False,
                            'errors': ['system']
                        }, status=400)
            else:
                return HttpResponseJSON({
                    'success': False,
                    'errors': ['platform']
                })

        resp_data = {'success': True}
    else:
        resp_data = {
            'success': False,
            'errors': form.errors.keys(),
        }

    return HttpResponseJSON(resp_data)
Esempio n. 6
0
def send_to_device_ajax(request):
    locale = l10n_utils.get_locale(request)
    phone_or_email = request.POST.get('phone-or-email')
    if not phone_or_email:
        return HttpResponseJSON({
            'success': False,
            'errors': ['phone-or-email']
        })

    data = {
        'platform': request.POST.get('platform'),
    }

    data_type = 'email' if '@' in phone_or_email else 'number'
    data[data_type] = phone_or_email
    form = SendToDeviceWidgetForm(data)

    if form.is_valid():
        phone_or_email = form.cleaned_data.get(data_type)
        platform = form.cleaned_data.get('platform')

        # check for android & valid send to device test value
        # update email/sms message if conditions match
        send_to_device_test = request.POST.get('android-send-to-device-test')
        if (platform == 'android' and send_to_device_test
                in ['android-test-modal', 'android-test-embed']):

            platform = send_to_device_test

        if data_type == 'number':
            if platform in SMS_MESSAGES:
                try:
                    basket.send_sms(phone_or_email, SMS_MESSAGES[platform])
                except basket.BasketException:
                    return HttpResponseJSON(
                        {
                            'success': False,
                            'errors': ['system']
                        }, status=400)
            else:
                # TODO define all platforms in SMS_MESSAGES
                return HttpResponseJSON({
                    'success': False,
                    'errors': ['platform']
                })
        else:  # email
            if platform in EMAIL_MESSAGES:
                try:
                    basket.subscribe(phone_or_email,
                                     EMAIL_MESSAGES[platform],
                                     source_url=request.POST.get('source-url'),
                                     lang=locale)
                except basket.BasketException:
                    return HttpResponseJSON(
                        {
                            'success': False,
                            'errors': ['system']
                        }, status=400)
            else:
                # TODO define all platforms in EMAIL_MESSAGES
                return HttpResponseJSON({
                    'success': False,
                    'errors': ['platform']
                })

        resp_data = {'success': True}
    else:
        resp_data = {
            'success': False,
            'errors': form.errors.keys(),
        }

    return HttpResponseJSON(resp_data)
Esempio n. 7
0
def send_to_device_ajax(request):
    locale = l10n_utils.get_locale(request)
    phone_or_email = request.POST.get('phone-or-email')
    if not phone_or_email:
        return HttpResponseJSON({
            'success': False,
            'errors': ['phone-or-email']
        })

    data = {
        'platform': request.POST.get('platform'),
    }

    data_type = 'email' if '@' in phone_or_email else 'number'
    data[data_type] = phone_or_email
    form = SendToDeviceWidgetForm(data)
    if form.is_valid():
        phone_or_email = form.cleaned_data.get(data_type)
        platform = form.cleaned_data.get('platform')
        if data_type == 'number':
            if platform in SMS_MESSAGES:
                try:
                    basket.send_sms(phone_or_email, SMS_MESSAGES[platform])
                except basket.BasketException:
                    return HttpResponseJSON(
                        {
                            'success': False,
                            'errors': ['system']
                        }, status=400)
            else:
                # TODO define all platforms in SMS_MESSAGES
                return HttpResponseJSON({
                    'success': False,
                    'errors': ['platform']
                })
        else:  # email
            if platform in EMAIL_MESSAGES:
                try:
                    basket.subscribe(phone_or_email,
                                     EMAIL_MESSAGES[platform],
                                     source_url=request.POST.get('source-url'),
                                     lang=locale)
                except basket.BasketException:
                    return HttpResponseJSON(
                        {
                            'success': False,
                            'errors': ['system']
                        }, status=400)
            else:
                # TODO define all platforms in EMAIL_MESSAGES
                return HttpResponseJSON({
                    'success': False,
                    'errors': ['platform']
                })

        resp_data = {'success': True}
    else:
        resp_data = {
            'success': False,
            'errors': form.errors.keys(),
        }

    return HttpResponseJSON(resp_data)