1
0
forked from erp-dev/erp
Files
erpnew/printing/management/commands/wecom_notify_printing_job_latest_state.py

49 lines
1.8 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
from __future__ import annotations
from django.core.management.base import BaseCommand, CommandError
from printing.services import send_printing_job_latest_state_wecom
class Command(BaseCommand):
help = "发送指定 PrintingJob 的“最新状态”到企业微信机器人markdown"
def add_arguments(self, parser):
parser.add_argument("printing_job_id", type=int, help="PrintingJob.id")
parser.add_argument("--key", type=str, default=None, help="可选:临时覆盖 settings.WECOM_WEBHOOK_KEY")
parser.add_argument("--timeout", type=float, default=10.0, help="HTTP 超时(秒)")
parser.add_argument(
"--dry-run",
action="store_true",
help="仅输出将要发送的 markdown 内容,不实际调用企业微信 webhook适用于无 https 出网环境)",
)
def handle(self, *args, **options):
printing_job_id = int(options["printing_job_id"])
key = options.get("key")
timeout = float(options.get("timeout") or 10.0)
dry_run = bool(options.get("dry_run"))
try:
result = send_printing_job_latest_state_wecom(
printing_job_id=printing_job_id,
sender_label="系统自动发送",
key=key,
timeout_seconds=timeout,
dry_run=dry_run,
)
except Exception as exc:
raise CommandError(str(exc)) from exc
if dry_run:
self.stdout.write(result["message"])
return
if not bool(result.get("ok")):
raise CommandError(
f"WeCom webhook 返回失败errcode={result.get('errcode')}, errmsg={result.get('errmsg')}, raw={result.get('wecom')}"
)
self.stdout.write(self.style.SUCCESS(f"OK: {result.get('wecom')}"))