Skip to content

Commit

Permalink
[IMP] simplify filtering out of inactive projects
Browse files Browse the repository at this point in the history
use a related field on hr_timesheet.sheet.prefill, which solves the
problem elegantly and also ensures that inactive projects are hidden in
the prefill projects of employees.
  • Loading branch information
huguesdk committed Sep 27, 2024
1 parent 07548fc commit 4345167
Show file tree
Hide file tree
Showing 3 changed files with 13 additions and 20 deletions.
6 changes: 2 additions & 4 deletions hr_timesheet_sheet_prefill/models/hr_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -12,11 +12,9 @@ class Employee(models.Model):
comodel_name="project.project",
relation="hr_employee_project_project_rel",
string="Projects",
domain=[("active", "=", True)],
)

# This exists solely extension in hr_timesheet_sheet_prefill_multi, and also
# to filter out inactive projects.
# This exists solely for extension in hr_timesheet_sheet_prefill_multi.
def all_prefill_projects(self):
self.ensure_one()
return self.project_ids.filtered(lambda project: project.active)
return self.project_ids
25 changes: 10 additions & 15 deletions hr_timesheet_sheet_prefill_multi/models/hr_employee.py
Original file line number Diff line number Diff line change
Expand Up @@ -16,18 +16,13 @@ class HrEmployee(models.Model):
)

def all_prefill_projects(self):
projects = super().all_prefill_projects()
# By searching, we get prefills sorted by sequence.
prefills = self.env["hr_timesheet.sheet.prefill"].search(
[
("project_project_id", "in", projects.ids),
("hr_employee_id", "=", self.id),
]
)
# Instead of doing `prefills.mapped("project_project_id")`, we manually
# create the recordset. This is because the recordset MAY contain
# duplicates, and `mapped()` removes duplicates.
result = self.env["project.project"]
for prefill in prefills:
result += prefill.project_project_id
return result
self.ensure_one()
# The only purpose of the below code is to sort the projects according
# to the sequence of the prefill records. Instead of doing
# `self.timesheet_prefill_ids.mapped("project_project_id")`, we
# manually create the recordset. This is because the recordset MAY
# contain duplicates, and `mapped()` removes duplicates.
projects = self.env["project.project"].browse()
for prefill in self.timesheet_prefill_ids:
projects += prefill.project_project_id
return projects
Original file line number Diff line number Diff line change
Expand Up @@ -30,8 +30,8 @@ class HrTimesheetSheetPrefill(models.Model):
ondelete="cascade",
required=True,
)

sequence = fields.Integer(string="Sequence", default=10)
active = fields.Boolean(related="project_project_id.active")

@api.model_cr
def init(self):
Expand Down

0 comments on commit 4345167

Please sign in to comment.