本文整理汇总了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)