本文整理汇总了Python中hc.api.models.Check.to_dict方法的典型用法代码示例。如果您正苦于以下问题:Python Check.to_dict方法的具体用法?Python Check.to_dict怎么用?Python Check.to_dict使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类hc.api.models.Check
的用法示例。
在下文中一共展示了Check.to_dict方法的4个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: checks
# 需要导入模块: from hc.api.models import Check [as 别名]
# 或者: from hc.api.models.Check import to_dict [as 别名]
def checks(request):
if request.method == "GET":
q = Check.objects.filter(user=request.user)
doc = {"checks": [check.to_dict() for check in q]}
return JsonResponse(doc)
elif request.method == "POST":
check = Check(user=request.user)
check.name = str(request.json.get("name", ""))
check.tags = str(request.json.get("tags", ""))
if "timeout" in request.json:
check.timeout = td(seconds=request.json["timeout"])
if "grace" in request.json:
check.grace = td(seconds=request.json["grace"])
check.save()
# This needs to be done after saving the check, because of
# the M2M relation between checks and channels:
if request.json.get("channels") == "*":
check.assign_all_channels()
return JsonResponse(check.to_dict(), status=201)
# If request is neither GET nor POST, return "405 Method not allowed"
return HttpResponse(status=405)
示例2: checks
# 需要导入模块: from hc.api.models import Check [as 别名]
# 或者: from hc.api.models.Check import to_dict [as 别名]
def checks(request):
if request.method == "GET":
q = Check.objects.filter(user=request.user)
tags = set(request.GET.getlist("tag"))
for tag in tags:
# approximate filtering by tags
q = q.filter(tags__contains=tag)
checks = []
for check in q:
# precise, final filtering
if not tags or check.matches_tag_set(tags):
checks.append(check.to_dict())
return JsonResponse({"checks": checks})
elif request.method == "POST":
created = False
check = _lookup(request.user, request.json)
if check is None:
num_checks = Check.objects.filter(user=request.user).count()
if num_checks >= request.user.profile.check_limit:
return HttpResponseForbidden()
check = Check(user=request.user)
created = True
_update(check, request.json)
return JsonResponse(check.to_dict(), status=201 if created else 200)
# If request is neither GET nor POST, return "405 Method not allowed"
return HttpResponse(status=405)
示例3: test_next_ping_with_cron_syntax
# 需要导入模块: from hc.api.models import Check [as 别名]
# 或者: from hc.api.models.Check import to_dict [as 别名]
def test_next_ping_with_cron_syntax(self):
dt = timezone.make_aware(datetime(2000, 1, 1), timezone=timezone.utc)
# Expect ping every round hour
check = Check()
check.kind = "cron"
check.schedule = "0 * * * *"
check.status = "up"
check.last_ping = dt
d = check.to_dict()
self.assertEqual(d["next_ping"], "2000-01-01T01:00:00+00:00")
示例4: checks
# 需要导入模块: from hc.api.models import Check [as 别名]
# 或者: from hc.api.models.Check import to_dict [as 别名]
def checks(request):
if request.method == "GET":
q = Check.objects.filter(user=request.user)
doc = {"checks": [check.to_dict() for check in q]}
return JsonResponse(doc)
elif request.method == "POST":
name = str(request.json.get("name", ""))
tags = str(request.json.get("tags", ""))
timeout = DEFAULT_TIMEOUT
if "timeout" in request.json:
timeout = td(seconds=request.json["timeout"])
grace = DEFAULT_GRACE
if "grace" in request.json:
grace = td(seconds=request.json["grace"])
unique_fields = request.json.get("unique", [])
if unique_fields:
existing_checks = Check.objects.filter(user=request.user)
if "name" in unique_fields:
existing_checks = existing_checks.filter(name=name)
if "tags" in unique_fields:
existing_checks = existing_checks.filter(tags=tags)
if "timeout" in unique_fields:
existing_checks = existing_checks.filter(timeout=timeout)
if "grace" in unique_fields:
existing_checks = existing_checks.filter(grace=grace)
if existing_checks.count() > 0:
# There might be more than one matching check, return first
first_match = existing_checks.first()
return JsonResponse(first_match.to_dict(), status=200)
check = Check(user=request.user, name=name, tags=tags,
timeout=timeout, grace=grace)
check.save()
# This needs to be done after saving the check, because of
# the M2M relation between checks and channels:
if request.json.get("channels") == "*":
check.assign_all_channels()
return JsonResponse(check.to_dict(), status=201)
# If request is neither GET nor POST, return "405 Method not allowed"
return HttpResponse(status=405)