本文整理汇总了Python中bedrock.firefox.forms.SendToDeviceWidgetForm类的典型用法代码示例。如果您正苦于以下问题:Python SendToDeviceWidgetForm类的具体用法?Python SendToDeviceWidgetForm怎么用?Python SendToDeviceWidgetForm使用的例子?那么, 这里精选的类代码示例或许可以为您提供帮助。
在下文中一共展示了SendToDeviceWidgetForm类的3个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: send_to_device_ajax
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)
示例2: send_to_device_ajax
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)
示例3: send_to_device_ajax
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)