Skip to content

Commit 69d60c6

Browse files
committed
Enhance datetime field detection with multi-layered approach for reliable ISO 8601 formatting
1 parent 1556f05 commit 69d60c6

File tree

1 file changed

+62
-15
lines changed

1 file changed

+62
-15
lines changed

mcp_server_odoo/tools.py

Lines changed: 62 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -77,24 +77,71 @@ def _format_datetime(self, value: str) -> str:
7777

7878
def _process_record_dates(self, record: Dict[str, Any], model: str) -> Dict[str, Any]:
7979
"""Process datetime fields in a record to ensure proper formatting."""
80-
# Get field metadata if available
80+
# Common datetime field names in Odoo
81+
known_datetime_fields = {
82+
"create_date",
83+
"write_date",
84+
"date",
85+
"datetime",
86+
"date_start",
87+
"date_end",
88+
"date_from",
89+
"date_to",
90+
"date_order",
91+
"date_invoice",
92+
"date_due",
93+
"last_update",
94+
"last_activity",
95+
"activity_date_deadline",
96+
}
97+
98+
# First try to get field metadata
99+
fields_info = None
81100
try:
82101
fields_info = self.connection.fields_get(model)
83-
for field_name, field_value in record.items():
84-
if field_name in fields_info:
85-
field_type = fields_info[field_name].get("type")
86-
if field_type == "datetime" and isinstance(field_value, str):
87-
record[field_name] = self._format_datetime(field_value)
88102
except Exception:
89-
# If we can't get field info, try to detect datetime fields by pattern
90-
for field_name, field_value in record.items():
91-
if isinstance(field_value, str) and (
92-
(len(field_value) == 17 and "T" in field_value and "-" not in field_value)
93-
or (len(field_value) == 19 and " " in field_value)
94-
):
95-
formatted = self._format_datetime(field_value)
96-
if formatted != field_value:
97-
record[field_name] = formatted
103+
# Field metadata unavailable, will use fallback detection
104+
pass
105+
106+
# Process each field in the record
107+
for field_name, field_value in record.items():
108+
if not isinstance(field_value, str):
109+
continue
110+
111+
should_format = False
112+
113+
# Check if field is identified as datetime from metadata
114+
if fields_info and isinstance(fields_info, dict) and field_name in fields_info:
115+
field_type = fields_info[field_name].get("type")
116+
if field_type == "datetime":
117+
should_format = True
118+
119+
# Check if field name suggests it's a datetime field
120+
if not should_format and field_name in known_datetime_fields:
121+
should_format = True
122+
123+
# Check if field name ends with common datetime suffixes
124+
if not should_format and any(
125+
field_name.endswith(suffix) for suffix in ["_date", "_datetime", "_time"]
126+
):
127+
should_format = True
128+
129+
# Pattern-based detection for datetime-like strings
130+
if not should_format and (
131+
(
132+
len(field_value) == 17 and "T" in field_value and "-" not in field_value
133+
) # 20250607T21:55:52
134+
or (
135+
len(field_value) == 19 and " " in field_value and field_value.count("-") == 2
136+
) # 2025-06-07 21:55:52
137+
):
138+
should_format = True
139+
140+
# Apply formatting if needed
141+
if should_format:
142+
formatted = self._format_datetime(field_value)
143+
if formatted != field_value:
144+
record[field_name] = formatted
98145

99146
return record
100147

0 commit comments

Comments
 (0)