forked from retoor/devplacepy
Quiiz system
This commit is contained in:
@@ -107,6 +107,106 @@ def cmd_devii_lessons_prune(args):
|
||||
print(f"Pruned {pruned} lesson(s) (active before: {active_before}, now: {_active_count()})")
|
||||
|
||||
|
||||
def _task_rows(enabled_only: bool) -> list:
|
||||
from devplacepy.services.devii.tasks.store import TABLE
|
||||
|
||||
if TABLE not in db.tables:
|
||||
return []
|
||||
criteria = {"deleted_at": None}
|
||||
if enabled_only:
|
||||
criteria["enabled"] = True
|
||||
rows = list(db[TABLE].find(**criteria))
|
||||
rows.sort(key=lambda row: int(row.get("run_count") or 0), reverse=True)
|
||||
return rows
|
||||
|
||||
|
||||
def _owner_name(owner_id: str) -> str:
|
||||
user = get_table("users").find_one(uid=owner_id)
|
||||
return user["username"] if user else owner_id
|
||||
|
||||
|
||||
def cmd_devii_tasks_list(args):
|
||||
rows = _task_rows(not args.all)
|
||||
if not rows:
|
||||
print("No tasks")
|
||||
return
|
||||
for row in rows:
|
||||
schedule = (
|
||||
f"every {row.get('every_seconds')}s"
|
||||
if row.get("kind") == "interval"
|
||||
else (row.get("cron") or row.get("run_at") or "")
|
||||
)
|
||||
print(
|
||||
f"{row.get('uid')} {_owner_name(str(row.get('owner_id') or '')):16} "
|
||||
f"{'on ' if row.get('enabled') else 'off'} {str(row.get('status')):9} "
|
||||
f"runs={row.get('run_count')}/{row.get('max_runs') or '-'} "
|
||||
f"{schedule:24} {row.get('label') or ''}"
|
||||
)
|
||||
|
||||
|
||||
def cmd_devii_tasks_disable(args):
|
||||
from devplacepy.services.devii.tasks.store import TABLE, TaskStore
|
||||
|
||||
if TABLE not in db.tables:
|
||||
print("No devii_tasks table exists")
|
||||
return
|
||||
row = db[TABLE].find_one(uid=args.uid, deleted_at=None)
|
||||
if not row:
|
||||
print(f"Task '{args.uid}' not found")
|
||||
sys.exit(1)
|
||||
store = TaskStore(db, str(row.get("owner_kind") or "user"), str(row.get("owner_id") or ""))
|
||||
store.update(
|
||||
args.uid,
|
||||
{
|
||||
"enabled": False,
|
||||
"status": "disabled",
|
||||
"next_run_at": None,
|
||||
"last_error": "disabled from the command line",
|
||||
},
|
||||
)
|
||||
_audit_cli(
|
||||
"cli.devii.task.disable",
|
||||
f"CLI disabled Devii task {args.uid}",
|
||||
metadata={"owner_id": row.get("owner_id"), "run_count": row.get("run_count")},
|
||||
target_type="task",
|
||||
target_uid=args.uid,
|
||||
target_label=row.get("label"),
|
||||
)
|
||||
print(f"Disabled task '{args.uid}'")
|
||||
|
||||
|
||||
def cmd_devii_tasks_prune(args):
|
||||
from devplacepy.services.devii.tasks.guards import automation_allowed
|
||||
from devplacepy.services.devii.tasks.store import TABLE, TaskStore
|
||||
|
||||
if TABLE not in db.tables:
|
||||
print("No devii_tasks table exists")
|
||||
return
|
||||
pruned = 0
|
||||
for row in list(db[TABLE].find(enabled=True, deleted_at=None)):
|
||||
owner_kind = str(row.get("owner_kind") or "")
|
||||
owner_id = str(row.get("owner_id") or "")
|
||||
if automation_allowed(owner_kind, owner_id):
|
||||
continue
|
||||
store = TaskStore(db, owner_kind, owner_id)
|
||||
store.update(
|
||||
row["uid"],
|
||||
{
|
||||
"enabled": False,
|
||||
"status": "disabled",
|
||||
"next_run_at": None,
|
||||
"last_error": "owner is not an administrator",
|
||||
},
|
||||
)
|
||||
pruned += 1
|
||||
_audit_cli(
|
||||
"cli.devii.task.prune",
|
||||
"CLI disabled tasks whose owner may not schedule",
|
||||
metadata={"disabled": pruned},
|
||||
)
|
||||
print(f"Disabled {pruned} task(s) whose owner is not an administrator")
|
||||
|
||||
|
||||
def register_devii(subparsers):
|
||||
devii = subparsers.add_parser("devii", help="Devii assistant management")
|
||||
devii_sub = devii.add_subparsers(title="action", dest="action")
|
||||
@@ -138,3 +238,19 @@ def register_devii(subparsers):
|
||||
lessons_clear = lessons_sub.add_parser("clear", help="Hard-delete every devii_lessons row")
|
||||
lessons_clear.add_argument("--force", action="store_true", help="Required to confirm hard deletion")
|
||||
lessons_clear.set_defaults(func=cmd_devii_lessons_clear)
|
||||
|
||||
devii_tasks = devii_sub.add_parser("tasks", help="Inspect and stop scheduled Devii tasks")
|
||||
tasks_sub = devii_tasks.add_subparsers(title="sub-action", dest="sub_action")
|
||||
|
||||
tasks_list = tasks_sub.add_parser("list", help="List scheduled tasks and their owners")
|
||||
tasks_list.add_argument("--all", action="store_true", help="Include disabled tasks")
|
||||
tasks_list.set_defaults(func=cmd_devii_tasks_list)
|
||||
|
||||
tasks_disable = tasks_sub.add_parser("disable", help="Disable one task by uid")
|
||||
tasks_disable.add_argument("uid", help="Uid of the task")
|
||||
tasks_disable.set_defaults(func=cmd_devii_tasks_disable)
|
||||
|
||||
tasks_prune = tasks_sub.add_parser(
|
||||
"prune", help="Disable every task whose owner is not an administrator"
|
||||
)
|
||||
tasks_prune.set_defaults(func=cmd_devii_tasks_prune)
|
||||
|
||||
Reference in New Issue
Block a user