本文整理匯總了Python中corehq.apps.sms.mixin.BackendMapping.view方法的典型用法代碼示例。如果您正苦於以下問題:Python BackendMapping.view方法的具體用法?Python BackendMapping.view怎麽用?Python BackendMapping.view使用的例子?那麽, 這裏精選的方法代碼示例或許可以為您提供幫助。您也可以進一步了解該方法所在類corehq.apps.sms.mixin.BackendMapping
的用法示例。
在下文中一共展示了BackendMapping.view方法的1個代碼示例,這些例子默認根據受歡迎程度排序。您可以為喜歡或者感覺有用的代碼點讚,您的評價將有助於係統推薦出更棒的Python代碼示例。
示例1: global_backend_map
# 需要導入模塊: from corehq.apps.sms.mixin import BackendMapping [as 別名]
# 或者: from corehq.apps.sms.mixin.BackendMapping import view [as 別名]
def global_backend_map(request):
backend_classes = get_available_backends()
global_backends = SMSBackend.view(
"sms/global_backends",
classes=backend_classes,
include_docs=True,
reduce=False
).all()
current_map = {}
catchall_entry = None
for entry in BackendMapping.view("sms/backend_map", startkey=["*"], endkey=["*", {}], include_docs=True).all():
if entry.prefix == "*":
catchall_entry = entry
else:
current_map[entry.prefix] = entry
if request.method == "POST":
form = BackendMapForm(request.POST)
if form.is_valid():
new_backend_map = form.cleaned_data.get("backend_map")
new_catchall_backend_id = form.cleaned_data.get("catchall_backend_id")
for prefix, entry in current_map.items():
if prefix not in new_backend_map:
current_map[prefix].delete()
del current_map[prefix]
for prefix, backend_id in new_backend_map.items():
if prefix in current_map:
current_map[prefix].backend_id = backend_id
current_map[prefix].save()
else:
current_map[prefix] = BackendMapping(is_global=True, prefix=prefix, backend_id=backend_id)
current_map[prefix].save()
if new_catchall_backend_id is None:
if catchall_entry is not None:
catchall_entry.delete()
catchall_entry = None
else:
if catchall_entry is None:
catchall_entry = BackendMapping(is_global=True, prefix="*", backend_id=new_catchall_backend_id)
else:
catchall_entry.backend_id = new_catchall_backend_id
catchall_entry.save()
messages.success(request, _("Changes Saved."))
else:
initial = {
"catchall_backend_id" : catchall_entry.backend_id if catchall_entry is not None else None,
"backend_map" : [{"prefix" : prefix, "backend_id" : entry.backend_id} for prefix, entry in current_map.items()],
}
form = BackendMapForm(initial=initial)
context = {
"backends" : global_backends,
"form" : form,
}
return render(request, "sms/backend_map.html", context)