当前位置: 首页>>代码示例>>Python>>正文


Python SendToDeviceWidgetForm.is_valid方法代码示例

本文整理汇总了Python中bedrock.firefox.forms.SendToDeviceWidgetForm.is_valid方法的典型用法代码示例。如果您正苦于以下问题:Python SendToDeviceWidgetForm.is_valid方法的具体用法?Python SendToDeviceWidgetForm.is_valid怎么用?Python SendToDeviceWidgetForm.is_valid使用的例子?那么, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在bedrock.firefox.forms.SendToDeviceWidgetForm的用法示例。


在下文中一共展示了SendToDeviceWidgetForm.is_valid方法的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。

示例1: send_to_device_ajax

# 需要导入模块: from bedrock.firefox.forms import SendToDeviceWidgetForm [as 别名]
# 或者: from bedrock.firefox.forms.SendToDeviceWidgetForm import is_valid [as 别名]
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)
开发者ID:pkdevboxy,项目名称:bedrock,代码行数:59,代码来源:views.py

示例2: send_to_device_ajax

# 需要导入模块: from bedrock.firefox.forms import SendToDeviceWidgetForm [as 别名]
# 或者: from bedrock.firefox.forms.SendToDeviceWidgetForm import is_valid [as 别名]
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)
开发者ID:ashmis,项目名称:bedrock,代码行数:51,代码来源:views.py

示例3: send_to_device_ajax

# 需要导入模块: from bedrock.firefox.forms import SendToDeviceWidgetForm [as 别名]
# 或者: from bedrock.firefox.forms.SendToDeviceWidgetForm import is_valid [as 别名]
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)
开发者ID:hoosteeno,项目名称:bedrock,代码行数:82,代码来源:views.py


注:本文中的bedrock.firefox.forms.SendToDeviceWidgetForm.is_valid方法示例由纯净天空整理自Github/MSDocs等开源代码及文档管理平台,相关代码片段筛选自各路编程大神贡献的开源项目,源码版权归原作者所有,传播和使用请参考对应项目的License;未经允许,请勿转载。