|
7 | 7 |
|
8 | 8 |
|
9 | 9 | def _to_datetime(value): |
10 | | - """Convert string to datetime if needed.""" |
| 10 | + """Convert string to datetime if needed. |
| 11 | +
|
| 12 | + Note: When accessing a Datetime field from a record (e.g., |
| 13 | + post.scheduled_publication_date), Odoo automatically returns a Python |
| 14 | + datetime object. However, when working with vals dictionaries (from forms, |
| 15 | + wizards, or API calls), the value may come as a string and needs to be |
| 16 | + converted to datetime for proper comparison operations. |
| 17 | + """ |
11 | 18 | if not value: |
12 | 19 | return False |
13 | 20 | if isinstance(value, str): |
@@ -91,7 +98,12 @@ def _check_for_publication(self, vals): |
91 | 98 | return super(BlogPost, posts_to_notify.sudo())._check_for_publication(vals) |
92 | 99 |
|
93 | 100 | def _has_future_scheduled_date(self, post, vals, now): |
94 | | - """Check if post has a future scheduled date.""" |
| 101 | + """Check if post has a future scheduled date. |
| 102 | +
|
| 103 | + Note: post.scheduled_publication_date is already a datetime object |
| 104 | + (Odoo converts it automatically), but vals.get() may return a string |
| 105 | + that needs conversion via _to_datetime(). |
| 106 | + """ |
95 | 107 | if post.scheduled_publication_date and post.scheduled_publication_date > now: |
96 | 108 | return True |
97 | 109 | scheduled_date = _to_datetime(vals.get("scheduled_publication_date")) |
@@ -150,7 +162,12 @@ def _process_write_vals(self, record, vals, now): |
150 | 162 | return record_vals |
151 | 163 |
|
152 | 164 | def _get_effective_scheduled_date(self, record, vals): |
153 | | - """Get the scheduled date that will be effective after write.""" |
| 165 | + """Get the scheduled date that will be effective after write. |
| 166 | +
|
| 167 | + Note: Uses _to_datetime() for both cases to ensure consistent |
| 168 | + datetime objects, even though record.scheduled_publication_date |
| 169 | + is already a datetime (defensive programming). |
| 170 | + """ |
154 | 171 | if "scheduled_publication_date" in vals: |
155 | 172 | return _to_datetime(vals.get("scheduled_publication_date")) |
156 | 173 | return _to_datetime(record.scheduled_publication_date) |
|
0 commit comments