Python Flask 任务管理系统:设计支持子任务和孙任务的任务表
///'from flask import Flask, jsonify, request//n//napp = Flask(name) //n//ntasks = []//n//nclass Task://n def init(self, id, title, parent_id=None)://n self.id = id//n self.title = title//n self.parent_id = parent_id//n self.children = []//n//n def add_child(self, child_task)://n self.children.append(child_task)//n//ndef find_task_by_id(task_id, tasks)://n for task in tasks://n if task.id == task_id://n return task//n elif task.children://n child_task = find_task_by_id(task_id, task.children)//n if child_task://n return child_task//n return None//n//n@app.route('/tasks', methods=['GET'])//n def get_all_tasks()://n return jsonify(tasks)//n//n@app.route('/tasks/int:task_id', methods=['GET'])//n def get_task(task_id)://n task = find_task_by_id(task_id, tasks)//n if task://n return jsonify(task.dict)//n else://n return jsonify({'error': 'Task not found'}), 404//n//n@app.route('/tasks', methods=['POST'])//n def create_task()://n task_id = len(tasks) + 1//n title = request.json.get('title')//n parent_id = request.json.get('parent_id')//n//n if parent_id://n parent_task = find_task_by_id(parent_id, tasks)//n if not parent_task://n return jsonify({'error': 'Parent task not found'}), 400//n task = Task(task_id, title, parent_id)//n parent_task.add_child(task)//n else://n task = Task(task_id, title)//n tasks.append(task)//n//n return jsonify(task.dict), 201//n//nif name == 'main'://n app.run()//n//n///'This example code demonstrates how to create a task management system using Python Flask, allowing you to create, retrieve, and manage tasks with subtasks and sub-subtasks. The code defines a Task class, demonstrates how to add subtasks, recursively find tasks, and handles task creation and retrieval using Flask routes./
原文地址: https://www.cveoy.top/t/topic/peRu 著作权归作者所有。请勿转载和采集!