#!/usr/bin/env python3 # -*- coding: utf-8 -*- """Reject an approval task""" import json, time result = {'widgettype': 'Message', 'options': {'title': 'Error', 'message': 'Invalid request', 'type': 'error'}} try: task_id = params_kw.get('id', '') decision = params_kw.get('decision', '') if not task_id: result['options'] = {'title': 'Error', 'message': '任务ID不能为空', 'type': 'error'} else: dbname = get_module_dbname('workflow_approval') now = time.strftime('%Y-%m-%d %H:%M:%S') async with DBPools().sqlorContext(dbname) as sor: tasks = await sor.sqlExe("SELECT * FROM approval_task WHERE id=${id}$ AND status='pending'", {'id': task_id}) if not tasks: result['options'] = {'title': 'Error', 'message': '任务不存在或已处理', 'type': 'error'} return json.dumps(result, ensure_ascii=False) task = tasks[0] instance_id = task['instance_id'] await sor.sqlExe(""" UPDATE approval_task SET status='rejected', comment=${comment}$, completed_at=${completed_at}$ WHERE id=${id}$ """, {'id': task_id, 'comment': decision, 'completed_at': now}) await sor.sqlExe(""" UPDATE approval_instance SET status='rejected', completed_at=${completed_at}$ WHERE id=${instance_id}$ """, {'completed_at': now, 'instance_id': instance_id}) result = {'widgettype': 'Message', 'options': {'title': 'Success', 'message': '已拒绝', 'type': 'success'}} except Exception as e: result['options'] = {'title': 'Error', 'message': f'拒绝失败: {str(e)}', 'type': 'error'} return json.dumps(result, ensure_ascii=False)