Skip to content

Commit

Permalink
[IMP] project_workload: Improve workload sync inheritance
Browse files Browse the repository at this point in the history
  • Loading branch information
paradoxxxzero committed Feb 7, 2024
1 parent bf65b18 commit eb29080
Show file tree
Hide file tree
Showing 3 changed files with 56 additions and 33 deletions.
47 changes: 35 additions & 12 deletions project_workload/models/project_task.py
Original file line number Diff line number Diff line change
Expand Up @@ -46,26 +46,49 @@ def _compute_workload_ids(self):
):
continue

vals = record._prepare_workload()
# Handle only one workload in automatic
if record.workload_ids:
# Remove other workloads and update the first workload values
record.workload_ids = [
(1, record.workload_ids[0].id, vals),
*[(2, workload_id.id) for workload_id in record.workload_ids[1:]],
]
else:
# Create the workload
record.workload_ids = [(0, 0, vals)]
record.workload_ids = record._get_workload_sync()

def _prepare_workload(self):
def _prepare_workload(self, **extra):
return {
"date_start": self.date_start,
"date_end": self.date_end,
"hours": self.planned_hours,
"user_id": self.user_id.id,
**extra,
}

def _get_workload_sync(self):
self.ensure_one()
return [
*[(0, 0, vals) for vals in self._get_new_workloads()],
*[
(1, workload_id.id, vals)
for workload_id, vals in self._get_updated_workloads()
],
*[(2, workload_id.id) for workload_id in self._get_obsolete_workloads()],
]

def _get_new_workloads(self):
self.ensure_one()
# Handle only one workload in automatic
if not self.workload_ids:
return [self._prepare_workload()]
return []

def _get_updated_workloads(self):
self.ensure_one()
# Remove other workloads and update the first workload values
if self.workload_ids:
return [(self.workload_ids[0], self._prepare_workload())]
return []

def _get_obsolete_workloads(self):
self.ensure_one()
# Remove other workloads and update the first workload values
if len(self.workload_ids) > 1:
return self.workload_ids[1:]
return []

@api.depends("workload_ids.unit_ids")
def _compute_workload_unit_ids(self):
# related doesn't retrieve all the data so we need to compute it
Expand Down
4 changes: 2 additions & 2 deletions project_workload/models/project_task_workload.py
Original file line number Diff line number Diff line change
Expand Up @@ -103,9 +103,9 @@ def name_get(self):
for task in self:
if not task.date_start or not task.date_end:
continue
week_start = f"Load {week_name(task.date_start)}"
week_start = week_name(task.date_start)
week_end = week_name(task.date_end - timedelta(days=7))
name = f"{week_start}"
name = f"{_('Load')} {week_start}"
if week_end > week_start:
name += f" - {week_end}"
name += f": {task.hours}h"
Expand Down
38 changes: 19 additions & 19 deletions project_workload/views/project_task_view.xml
Original file line number Diff line number Diff line change
Expand Up @@ -17,26 +17,26 @@
<field name="use_workload" invisible="1" />
<field name="config_workload_manually" />
</group>
<field
name="workload_ids"
attrs="{'readonly': [('config_workload_manually', '=', False)]}"
>
<tree editable="bottom">
<field name="date_start" />
<field name="date_end" />
<field name="user_id" />
<field name="hours" />
</tree>
</field>
<field name="workload_unit_ids" groups="base.group_no_one">
<tree>
<field name="week" />
<field name="workload_id" />
<field name="user_id" />
<field name="hours" />
</tree>
</field>
</group>
<field
name="workload_ids"
attrs="{'readonly': [('config_workload_manually', '=', False)]}"
>
<tree editable="bottom">
<field name="date_start" />
<field name="date_end" />
<field name="user_id" />
<field name="hours" />
</tree>
</field>
<field name="workload_unit_ids" groups="base.group_no_one">
<tree>
<field name="week" />
<field name="workload_id" />
<field name="user_id" />
<field name="hours" />
</tree>
</field>
</page>
</notebook>
</field>
Expand Down

0 comments on commit eb29080

Please sign in to comment.