|
30 | 30 |
|
31 | 31 |
|
32 | 32 | def _extract_m2o_id(v):
|
33 |
| - """Normalize many2one values to an integer id or False. |
34 |
| - Accepts: int, (id, ...) tuple/list, or dict with id-ish keys. |
35 |
| - """ |
| 33 | + """Normalize many2one values to an integer id or False.""" |
36 | 34 | if isinstance(v, int):
|
37 | 35 | return v
|
38 | 36 | if isinstance(v, (list, tuple)) and v and isinstance(v[0], int):
|
39 | 37 | return v[0]
|
40 | 38 | if isinstance(v, dict):
|
41 |
| - data = v.get("data") or {} |
42 |
| - return v.get("res_id") or data.get("id") or v.get("id") or v.get("ref") or False |
| 39 | + m2o_id = v.get("res_id") or v.get("id") |
| 40 | + if isinstance(m2o_id, int): |
| 41 | + return m2o_id |
43 | 42 | return False
|
44 | 43 |
|
45 | 44 |
|
| 45 | +def _m2m_items(value): |
| 46 | + if isinstance(value, (list, tuple)): |
| 47 | + return value |
| 48 | + if isinstance(value, dict): |
| 49 | + if isinstance(value.get("res_ids"), (list, tuple)): |
| 50 | + return value["res_ids"] |
| 51 | + if isinstance(value.get("data"), (list, tuple)): |
| 52 | + return value["data"] |
| 53 | + return None |
| 54 | + |
| 55 | + |
| 56 | +def _to_int_id(e): |
| 57 | + if isinstance(e, int): |
| 58 | + return e |
| 59 | + if isinstance(e, str) and e.isdigit(): |
| 60 | + return int(e) |
| 61 | + if isinstance(e, dict): |
| 62 | + rid = e.get("res_id") |
| 63 | + if isinstance(rid, int): |
| 64 | + return rid |
| 65 | + iid = e.get("id") |
| 66 | + if isinstance(iid, int): |
| 67 | + return iid |
| 68 | + return None |
| 69 | + |
| 70 | + |
46 | 71 | def _sanitize_field(field, value):
|
47 | 72 | """Return sanitized value for a single field, or None to skip."""
|
48 | 73 | if not field:
|
49 | 74 | return None
|
50 | 75 | if field.type == "many2one":
|
51 | 76 | return _extract_m2o_id(value)
|
| 77 | + if field.type == "many2many": |
| 78 | + items = _m2m_items(value) |
| 79 | + if items is None: |
| 80 | + return None |
| 81 | + ids = [i for i in (_to_int_id(e) for e in items) if i is not None] |
| 82 | + # Always return a command, even when empty, to reflect clearing the relation |
| 83 | + return [(6, 0, ids)] |
52 | 84 | if field.type in _SIMPLE_FIELD_TYPES:
|
53 | 85 | return value
|
54 |
| - return None # skip x2many/reference/others |
| 86 | + return None # skip one2many/reference/others |
55 | 87 |
|
56 | 88 |
|
57 | 89 | class WebFormBannerRule(models.Model):
|
|
0 commit comments