splash365

task_tree_2025

Dec 2nd, 2025
42
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.34 KB | None | 0 0
  1. from types import prepare_class
  2. from typing import List, Dict, Any
  3.  
  4. def get_tasks_list(project_id: int = 1) -> List[Dict[str, Any]]:
  5.     return [
  6.         # --- Top-level tasks ---
  7.         {"id": 10, "project_id": project_id, "name": "Setup",    "status": "done",         "assignee_employee_id": 201, "parent_task_id": None},
  8.         {"id": 20, "project_id": project_id, "name": "Backend",  "status": "in_progress",  "assignee_employee_id": 202, "parent_task_id": None},
  9.         {"id": 30, "project_id": project_id, "name": "Frontend", "status": "planned",      "assignee_employee_id": 203, "parent_task_id": None},
  10.  
  11.         # --- Subtasks of Setup (10) ---
  12.         {"id": 101, "project_id": project_id, "name": "Create repo",  "status": "done",        "assignee_employee_id": 201, "parent_task_id": 10},
  13.         {"id": 102, "project_id": project_id, "name": "Basic CI",     "status": "in_progress", "assignee_employee_id": 202, "parent_task_id": 10},
  14.  
  15.         # --- Subtasks of Backend (20) ---
  16.         {"id": 201, "project_id": project_id, "name": "Auth endpoints","status": "in_progress","assignee_employee_id": 202, "parent_task_id": 20},
  17.         {"id": 202, "project_id": project_id, "name": "Health check", "status": "planned",     "assignee_employee_id": 203, "parent_task_id": 20},
  18.  
  19.         # --- Subtasks of Frontend (30) ---
  20.         {"id": 301, "project_id": project_id, "name": "Login page",   "status": "planned",     "assignee_employee_id": 203, "parent_task_id": 201},
  21.         {"id": 302, "project_id": project_id, "name": "Dashboard shell","status": "planned",   "assignee_employee_id": 204, "parent_task_id": 201},
  22.        
  23.     ]
  24.  
  25.  
  26.  
  27. tasks = get_tasks_list()
  28.  
  29. def build_tree(task_id):
  30.     task_tree = []
  31.     for task in tasks:
  32.         if (task['parent_task_id'] == task_id):
  33.             task_tree.append({
  34.                 'id' : task['id'],
  35.                 'sub_tasks': build_tree(task['id'])
  36.             })
  37.     return task_tree
  38.  
  39. def get_task_tree():
  40.     task_trees = []
  41.     for task in tasks:
  42.         tree = build_tree(task['id'])
  43.         task_tree = {
  44.             'id': task['id'],
  45.             'sub_tasks': tree
  46.         }
  47.         task_trees.append(task_tree)
  48.     return task_trees
  49.        
  50.  
  51. # tree = build_tree(20)
  52. # print(tree)
  53.  
  54. import json
  55.  
  56. task_tree = get_task_tree()
  57.  
  58. print(json.dumps(task_tree, indent=2))
  59.  
  60.  
  61.  
  62. '''''
  63. Output:
  64.  
  65. [
  66.  {
  67.    "id": 10,
  68.    "sub_tasks": [
  69.      {
  70.        "id": 101,
  71.        "sub_tasks": []
  72.      },
  73.      {
  74.        "id": 102,
  75.        "sub_tasks": []
  76.      }
  77.    ]
  78.  },
  79.  {
  80.    "id": 20,
  81.    "sub_tasks": [
  82.      {
  83.        "id": 201,
  84.        "sub_tasks": [
  85.          {
  86.            "id": 301,
  87.            "sub_tasks": []
  88.          },
  89.          {
  90.            "id": 302,
  91.            "sub_tasks": []
  92.          }
  93.        ]
  94.      },
  95.      {
  96.        "id": 202,
  97.        "sub_tasks": []
  98.      }
  99.    ]
  100.  },
  101.  {
  102.    "id": 30,
  103.    "sub_tasks": []
  104.  },
  105.  {
  106.    "id": 101,
  107.    "sub_tasks": []
  108.  },
  109.  {
  110.    "id": 102,
  111.    "sub_tasks": []
  112.  },
  113.  {
  114.    "id": 201,
  115.    "sub_tasks": [
  116.      {
  117.        "id": 301,
  118.        "sub_tasks": []
  119.      },
  120.      {
  121.        "id": 302,
  122.        "sub_tasks": []
  123.      }
  124.    ]
  125.  },
  126.  {
  127.    "id": 202,
  128.    "sub_tasks": []
  129.  },
  130.  {
  131.    "id": 301,
  132.    "sub_tasks": []
  133.  },
  134.  {
  135.    "id": 302,
  136.    "sub_tasks": []
  137.  }
  138. ]
  139. '''''
Advertisement
Add Comment
Please, Sign In to add comment