tri/main.py
Hubert Van De Walle 4b0622451b Initial commit
2025-03-04 15:19:18 +01:00

110 lines
2.6 KiB
Python

import click
import random
import odoo_client
import json
from tasks import fetch_tasks
from rich import print, console
from datetime import datetime
from collections import defaultdict
from typing import Dict, List, Optional
from config import vfyd_tags, team
console = console.Console()
def load_dispatch(date: Optional[str] = None) -> Dict[str, List[int]]:
if date is None:
date = datetime.now().strftime("%Y-%m-%d")
with open(f"out/{date}_dispatch.json", "r") as f:
return json.load(f)
def save_dispatch(dispatch: Dict[str, List[int]], date: Optional[str] = None) -> None:
if date is None:
date = datetime.now().strftime("%Y-%m-%d")
with open(f"out/{date}_dispatch.json", "w") as f:
json.dump(dispatch, f)
@click.group()
def cli() -> None:
pass
@cli.command()
def dispatch():
"""Randomly distribute unassigned tasks among team members."""
domain = [
("stage_id", "=", 194),
("user_ids", "=", False),
("project_id", "=", 49),
("tag_ids", "not in", vfyd_tags),
]
client = odoo_client.OdooClient()
records = fetch_tasks(client, domain)
random.shuffle(records)
dispatch = defaultdict(list)
out = defaultdict(list)
for idx, task in enumerate(records):
dispatch[team[idx % len(team)]].append(task.id)
out[team[idx % len(team)]].append(task)
save_dispatch(dispatch)
for key, tasks in out.items():
print(f"**{key}:**")
for task in tasks:
print(task.url)
@cli.command()
def status():
"""Show all unassigned tasks in the backlog."""
client = odoo_client.OdooClient()
domain = [
("stage_id", "=", 194),
("user_ids", "=", False),
("project_id", "=", 49),
("tag_ids", "not in", vfyd_tags),
]
tasks = fetch_tasks(client, domain)
for task in tasks:
print(task)
@cli.command()
def check():
"""Check which previously dispatched tasks are still not completed."""
dispatch = load_dispatch()
reverse_dispatch = {v: k for k, vs in dispatch.items() for v in vs}
client = odoo_client.OdooClient()
domain = [
("id", "in", list(reverse_dispatch.keys())),
("tag_ids", "not in", vfyd_tags),
("stage_id", "=", 194),
("project_id", "=", 49),
]
tasks = fetch_tasks(client, domain)
not_done = defaultdict(list)
for task in tasks:
not_done[reverse_dispatch[task.id]].append(task)
for key, value in not_done.items():
if value:
console.rule(f"[red]{key}[/red]")
for task in value:
print(task)
if __name__ == "__main__":
cli()