Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
16 changes: 7 additions & 9 deletions spp_api_v2_change_request/services/change_request_service.py
Original file line number Diff line number Diff line change
Expand Up @@ -204,10 +204,17 @@ def _serialize_detail(self, detail) -> dict[str, Any]:
# Get fields from the model
model_fields = detail._fields

# Field types that are skipped entirely — check BEFORE reading the
# value so that computed fields of these types are never triggered.
_SKIP_FIELD_TYPES = {"many2many", "one2many", "binary"}
Comment on lines +207 to +209

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

medium

To improve maintainability and avoid code duplication, it's better to reuse the SKIP_FIELD_TYPES constant that is already defined in odoo.addons.spp_api_v2.services.schema_builder and used elsewhere in this file (in _validate_detail_input).

I suggest you remove this local definition and instead:

  1. Add from odoo.addons.spp_api_v2.services.schema_builder import SKIP_FIELD_TYPES to the file's top-level imports.
  2. Use SKIP_FIELD_TYPES on line 215.
  3. Remove the now-redundant local import from _validate_detail_input.


for field_name, field in model_fields.items():
if field_name.startswith("_") or field_name in DETAIL_SKIP_FIELDS:
continue

if field.type in _SKIP_FIELD_TYPES:
continue

value = getattr(detail, field_name)

if field.type == "many2one":
Expand All @@ -230,19 +237,10 @@ def _serialize_detail(self, detail) -> dict[str, Any]:
}
else:
result[field_name] = value.name if hasattr(value, "name") else str(value.id)
elif field.type == "many2many":
# Skip many2many for now
continue
elif field.type == "one2many":
# Skip one2many for now
continue
elif field.type == "date":
result[field_name] = value.isoformat() if value else None
elif field.type == "datetime":
result[field_name] = value.isoformat() if value else None
elif field.type == "binary":
# Skip binary fields in API
continue
else:
result[field_name] = value

Expand Down
9 changes: 7 additions & 2 deletions spp_change_request_v2/models/change_request.py
Original file line number Diff line number Diff line change
Expand Up @@ -713,10 +713,15 @@ def _create_dms_directory(self):
# ══════════════════════════════════════════════════════════════════════════

def get_detail(self):
"""Get the detail record for this CR."""
"""Get the detail record for this CR.

Uses with_prefetch() to isolate from _ensure_detail's sudo()
prefetch set — without this, non-stored computed fields can
trigger record-rule checks against the wrong user.
"""
self.ensure_one()
if self.detail_res_model and self.detail_res_id:
return self.env[self.detail_res_model].browse(self.detail_res_id)
return self.env[self.detail_res_model].browse(self.detail_res_id).with_prefetch()
return None

def _ensure_detail(self):
Expand Down
Loading