check availabilities before dispatching
This commit is contained in:
parent
47a3eed98b
commit
d3abdb10c4
12
src/leave.py
Normal file
12
src/leave.py
Normal file
@ -0,0 +1,12 @@
|
|||||||
|
from dataclasses import dataclass
|
||||||
|
from model import OdooModel
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
|
@dataclass
|
||||||
|
class Leave(OdooModel):
|
||||||
|
_name = "hr.leave.report.calendar"
|
||||||
|
|
||||||
|
start_datetime: datetime
|
||||||
|
stop_datetime: datetime
|
||||||
|
employee_id: str
|
||||||
38
src/main.py
38
src/main.py
@ -8,6 +8,8 @@ from collections import defaultdict
|
|||||||
from typing import Dict, List, Optional
|
from typing import Dict, List, Optional
|
||||||
from config import vfyd_tags, team
|
from config import vfyd_tags, team
|
||||||
from tasks import Task
|
from tasks import Task
|
||||||
|
from leave import Leave
|
||||||
|
import re
|
||||||
|
|
||||||
console = console.Console()
|
console = console.Console()
|
||||||
|
|
||||||
@ -35,21 +37,51 @@ def cli() -> None:
|
|||||||
def dispatch():
|
def dispatch():
|
||||||
"""Randomly distribute unassigned tasks among team members."""
|
"""Randomly distribute unassigned tasks among team members."""
|
||||||
|
|
||||||
|
client = odoo_client.OdooClient()
|
||||||
|
today = datetime.now().strftime("%Y-%m-%d")
|
||||||
|
domain = [
|
||||||
|
("start_datetime", "<=", today),
|
||||||
|
("stop_datetime", ">=", today),
|
||||||
|
]
|
||||||
|
domain.extend(["|"] * (len(team) - 1))
|
||||||
|
domain.extend(
|
||||||
|
[("employee_id.name", "=ilike", f"%({employee})") for employee in team]
|
||||||
|
)
|
||||||
|
leaves = client.web_search_read(Leave, domain)
|
||||||
|
|
||||||
|
team_availability = dict()
|
||||||
|
for employee in team:
|
||||||
|
team_availability[employee] = (True, True)
|
||||||
|
|
||||||
|
for leave in leaves:
|
||||||
|
employee = re.search(r"\((.*?)\)", leave.employee_id).group(1).lower()
|
||||||
|
is_morning = leave.start_datetime.hour < 11
|
||||||
|
is_afternoon = leave.stop_datetime.hour >= 14
|
||||||
|
if is_morning:
|
||||||
|
team_availability[employee] = (False, team_availability[employee][1])
|
||||||
|
if is_afternoon:
|
||||||
|
team_availability[employee] = (team_availability[employee][0], False)
|
||||||
|
|
||||||
|
available_employees = [
|
||||||
|
employee
|
||||||
|
for employee, availability in team_availability.items()
|
||||||
|
if availability[0] or availability[1]
|
||||||
|
]
|
||||||
|
|
||||||
domain = [
|
domain = [
|
||||||
("stage_id", "=", 194),
|
("stage_id", "=", 194),
|
||||||
("user_ids", "=", False),
|
("user_ids", "=", False),
|
||||||
("project_id", "=", 49),
|
("project_id", "=", 49),
|
||||||
("tag_ids", "not in", vfyd_tags),
|
("tag_ids", "not in", vfyd_tags),
|
||||||
]
|
]
|
||||||
client = odoo_client.OdooClient()
|
|
||||||
records = client.web_search_read(Task, domain)
|
records = client.web_search_read(Task, domain)
|
||||||
|
|
||||||
random.shuffle(records)
|
random.shuffle(records)
|
||||||
dispatch = defaultdict(list)
|
dispatch = defaultdict(list)
|
||||||
out = defaultdict(list)
|
out = defaultdict(list)
|
||||||
for idx, task in enumerate(records):
|
for idx, task in enumerate(records):
|
||||||
dispatch[team[idx % len(team)]].append(task.id)
|
dispatch[available_employees[idx % len(available_employees)]].append(task.id)
|
||||||
out[team[idx % len(team)]].append(task)
|
out[available_employees[idx % len(available_employees)]].append(task)
|
||||||
|
|
||||||
save_dispatch(dispatch)
|
save_dispatch(dispatch)
|
||||||
for key, tasks in out.items():
|
for key, tasks in out.items():
|
||||||
|
|||||||
@ -1,5 +1,6 @@
|
|||||||
from typing import Dict, Self, Any, get_origin
|
from typing import Dict, Self, Any, get_origin
|
||||||
from dataclasses import fields, Field
|
from dataclasses import fields, Field
|
||||||
|
from datetime import datetime
|
||||||
|
|
||||||
|
|
||||||
class OdooModel:
|
class OdooModel:
|
||||||
@ -15,7 +16,7 @@ class OdooModel:
|
|||||||
},
|
},
|
||||||
},
|
},
|
||||||
}
|
}
|
||||||
elif field_type in {str, int, bool}:
|
elif field_type in {str, int, bool, datetime}:
|
||||||
return {f.name: {}}
|
return {f.name: {}}
|
||||||
else:
|
else:
|
||||||
raise NotImplementedError(f"Unsupported field type: {field_type}")
|
raise NotImplementedError(f"Unsupported field type: {field_type}")
|
||||||
@ -34,6 +35,9 @@ class OdooModel:
|
|||||||
if f.init and f.name in record:
|
if f.init and f.name in record:
|
||||||
value = record[f.name]
|
value = record[f.name]
|
||||||
|
|
||||||
|
if f.type is datetime:
|
||||||
|
value = datetime.strptime(value, "%Y-%m-%d %H:%M:%S")
|
||||||
|
|
||||||
if f.name.endswith("_id"):
|
if f.name.endswith("_id"):
|
||||||
if f.type is str:
|
if f.type is str:
|
||||||
value = value["display_name"]
|
value = value["display_name"]
|
||||||
|
|||||||
Loading…
x
Reference in New Issue
Block a user