Commit Graph
100 Commits
Author SHA1 Message Date
Rémy Voet (ryv) 76e5c6af2a [FIX] core: flush non-attachment binary fields when necessary
Non-attachment binary fields need to be flushed before reading their
size, since the latter relies on the database's binary size function.

Part-of: odoo/odoo#160708
2024-04-26 17:17:52 +00:00
Rémy Voet (ryv) b890049fda [FIX] core: cache inconsistency when assigning related resized image field
After writing or creating on a related Image field, its cache contains
the full-size image instead of the resized one (according to its
attributes max_width and max_height).  Fix the cache with the resized
image at the end of the inverse method.

Part-of: odoo/odoo#160708
2024-04-26 17:17:52 +00:00
Rémy Voet (ryv) 87a1ebb338 [FIX] core: fix create/write on binary fields
When invoking create() or write() with a binary field, the cache of the
field was incorrect if bin_size=True was in context.  Force context with
bin_size=False when putting a binary value in cache.  It is particularly
important to have coherent values in the cache for `web_save`.

Also, because an environment with bin_size=False won't return the same
context cache key as one with bin_size=None, it leads to have a cache
inconstistency when we write with bin_size=False.  Change Environment
method cache_key() to return the same cache key when bin_size is absent,
bin_size=None or bin_size=False.

Tests on binary fields have been updated to not rely on flush and
invalidate.  We also created specific tests for write() on binary
fields.

Part-of: odoo/odoo#160708
2024-04-26 17:17:52 +00:00
Rémy Voet (ryv) a0bf434960 [FIX] core: add invalidation of Environment's _cache_key.
Changing the environment in method create() to force bin_size=False
looks harmless, but it actually breaks many tests, in particular in
module account.  The reason is that company_dependent fields are read at
the wrong place in the cache.  And this is because `env._cache_key` can
be polluted with old data.

Make sure that `_cache_key` is cleared when resetting all the lazy
properties on the environment.  Only the change in res_user.py makes it
work, but let's not tempt the devil.

Side note: I hate caches.

Part-of: odoo/odoo#160708
2024-04-26 17:17:52 +00:00
Rémy Voet (ryv) 3c7db87ade [IMP] core: add warning for malformed aggregate specification
_read_group doesn't raise an error when we have an aggregate
specification like `order_id.create_date:min`, instead it silently
ignores the `.create_date` part.

We only add a warning in the stable version to avoid breaking any
change.

closes odoo/odoo#158777

Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
2024-03-22 16:49:49 +00:00
Rémy Voet (ryv) b9bfc0bb6e [FIX] core: sorted of new records + prefetch
BaseModel's sorted() has two problems:
- It breaks the prefetch of self for no reason
- When it is called without an argument, it filters out new records
because the search() used in sorted() doesn't return new records.

Keep the same prefetch as self to fix the first problem.
We partially fix/support the second issue, we just avoid filtering out
new records (but we don't actually sort them)

closes odoo/odoo#157145

X-original-commit: 0551c3b7e8e1469dabdb19d6420c544a1654fec5
Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
2024-03-12 10:34:51 +00:00
Rémy Voet (ryv) bb7f0c86ae [FIX] mail: avoid unbatched read of activity's deadline
__getitem__() of BaseModel, reset the prefetch set of the recordset.
Fix _compute_activity_date_deadline, to batched the reading of
activity's deadline.

closes odoo/odoo#154860

X-original-commit: ef7226aaa2f0ad135133fe27bdf1414b7ebf2a92
Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
2024-02-23 13:09:57 +00:00
Rémy Voet (ryv) 86dd313d10 [FIX] core: fix read_group with groupby=['id']
Use read_group with groupby=['id'] raise a Exception:
```
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 2386, in _read_group_format_result
    m2x_records = self.env[field.comodel_name].browse(ids).union()
  File "/home/odoo/Documents/dev/odoo/odoo/api.py", line 521, in __getitem__
    return self.registry[model_name](self, (), ())
  File "/home/odoo/Documents/dev/odoo/odoo/modules/registry.py", line 190, in __getitem__
    return self.models[model_name]
KeyError: None
```
Even if it doesn't make lot of sense to do that (mostly equivalent to
search), it is preferable to manage the case correctly.

closes odoo/odoo#154799

X-original-commit: 75a259365989d469972c0616dba22a56bf218bb5
Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
2024-02-21 09:41:26 +00:00
Rémy Voet (ryv) ccb1f922b2 [FIX] mrp: recursion issue after 88 work orders
Current behaviour:
When planning more than 88 work orders,
there is a recursion error.

Steps to reproduce:
1. Go to Manufacturing
2. Operations > Manufacturing Orders
3. Create a manufacturing order
4. Add 90 work orders
5. Click on Confirm
6. Click on Plan
7. Recursion error

Cause of the issue:
Maximum depth of the Python interpreter stack
The recursion limit being set at 1000
by default (with getrecursionlimit)

Fix:
Upped the limit to ~320 work orders

opw-3651494

closes odoo/odoo#152198

X-original-commit: 25081646ef0b679356ad46f62fe737fb44269baf
Signed-off-by: William Henrotin (whe) <whe@odoo.com>
Co-authored-by: Rémy Voet <ryv@odoo.com>
2024-02-01 11:20:10 +00:00
Rémy Voet (ryv) 65a2c2ebc8 [FIX] core: new record shouldn't force fetching inverse x2many fields
In e0297bdac4, the creation of a new
record always patches the inverse fields of relational fields in order
to make the cache of those inverse fields consistent.

For instance, when creating a new record like

    user = model.new({'group_ids': [Command.link(group.id)]})

The inverse of field 'group_ids' on the new record having 'group' as
origin is patched so that its value includes record.  A side effect of
this mechanism is that it fetches group.user_ids in order to patch the
value of new_group.user_ids, where 'new_group' is the new record having
'group' as origin.

The side effect described above is problematic when that inverse field
has huge cardinality, like hundreds of thousands of records, and this
performance overhead is unacceptable when the inverse field is actually
not used at all.

We address this performance issue by patching the value of x2many fields
only when they are used.  If the value of the field is not in cache yet,
the patch is applied once a value is put in cache.  If the field is not
used, the patch is simply never applied.

Part-of: odoo/odoo#149624
2024-02-01 09:49:50 +00:00
Rémy Voet (ryv) e3177646b6 [FIX] core: fix Field._update() method to take in account archived record
In e0297bd, we fixed the inverse field values of the new record during
the onchange. But we actually filter out inactive records by doing
record[self.name] in _update(). And since the XtoMany field cache
values should always contain inactive records, we need to add
with_context(active_test=False) on records.
Also remove the useless 'if value', value is always truly because it
is always a record.

Note that this solves a performance issue in our production, because
in order to filter out inactive records, we need to fetch the active
field next to every prefechable field.

closes odoo/odoo#148675

Signed-off-by: Raphael Collet <rco@odoo.com>
2024-01-10 08:51:01 +00:00
Rémy Voet (ryv) 914ac6d0b9 [FIX] tests: fix assert message of required field check
closes odoo/odoo#147400

X-original-commit: 0b9257009154eb92a723f72ad64a31cf9b577748
Signed-off-by: Raphael Collet <rco@odoo.com>
2023-12-23 17:01:38 +00:00
Rémy Voet (ryv) e0297bdac4 [FIX] core: fix onchange when create new one2many record
When we add a new record N to an one2many tree view from an existing
record X form, during the onchange() on the one2many comodel, the cache
of the N.one2many contains only the new record X (the siblings aren't in
it). Because of this, the result of compute methods may be incorrect
and the form won't be updated accordingly. See
https://github.com/odoo/enterprise/pull/52957 for a concrete example.

Technically, this is due to _update_cache() forcing the inverse field
value to the single value of the new record
("not cache.contains(inv_rec, invf)" is True), instead of also
considering the original values (which is properly done by
Field._update()).

closes odoo/odoo#146778

Related: odoo/enterprise#53298
Signed-off-by: Raphael Collet <rco@odoo.com>
2023-12-22 16:14:45 +00:00
Rémy Voet (ryv) 444dbf2001 [FIX] core: avoid quadratic complexity for partial compute methods
For a compute store field with new records (e.g. during an 'onchange'),
the compute method can be called multiple times on the same records
without changing the dependencies. Moreover, it can lead to have N² / 2
complexity for a trivial compute on N records.

With partial (where we don't always change the value) compute method:
```
@api.depends('reward')
def _compute_has_been_rewarded(self):
    for rec in self:
        if rec.reward:
            rec.has_been_rewarded = 'Yes'
```

If every `reward` of `self` (N records) is `False`, when the ORM needs
to recompute `has_been_rewarded` of `self`: the compute will be batched,
but only the first record in the batch will be set (to `False`) each
time (due to the current fallback - "fallback to null value if compute
gives nothing"). This means that we will call the compute method N
times, and the compute itself will loop on an average of N/2 records
(the prefetch set decreasing at each step).

Fix this quadratic behavior by setting the cache to `False` for every
record not set during the compute method (instead of just the current
record).

closes odoo/odoo#142162

X-original-commit: 1604ee983aadc0cbee0cd50cbea2b09572905b04
Signed-off-by: Raphael Collet <rco@odoo.com>
Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
2023-11-15 13:56:36 +00:00
Rémy Voet (ryv) c1e8a32e7d [FIX] core,*: Fix display_name computations for new records.
As of https://github.com/odoo/odoo/pull/114024, `display_name` is
implicit in every form view (see the use of addFieldDependencies in the
Form controller). Therefore, when you create a new record for this
model, it calls the first `onchange`, which will compute display_name
for a new record (id without origin). Some `_compute_display_name` don't
handle new records correctly and raise a traceback. These models are
sometimes directly accessible:

- Accounting > Account Group > New => Traceback
- Contact > Contact Tags > New => Traceback

Other models are inaccessible by default (no view to access or create a
new record), but if someone creates a view for them with studio (or
modifies an existing one to allow creation):

- `crm.iap.lead.role`
- `crm.iap.lead.seniority`
- `chatbot.script.answer`
- `payment.token`

Change the code of `_compute_display_name` on these models to be more
defensive and avoid (potential) tracebacks. Similarly, change the
`convert_to_display_name` of `fields.Datetime` to take into account
`None` value.

closes odoo/odoo#139592

Related: odoo/enterprise#49721
Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
2023-11-06 11:16:31 +00:00
Rémy Voet (ryv) 5c651a10da [FIX] core: remove the doc of Field.states
Since #104741, `Field.states` is no longer supported, remove the
documentation associated. We cannot remove the attribute
from Field because it is used in the migration script.

closes odoo/odoo#140694

Signed-off-by: Raphael Collet <rco@odoo.com>
2023-11-02 16:57:55 +00:00
Rémy Voet (ryv)andPolymorphe57 e6f501bea5 [FIX] web: fix domain_selector to handle false as display_name
Since https://github.com/odoo/odoo/pull/137098, `display_name`
can be false (from the default behavior in BaseModel). It isn't
handle correctly in domain_selector Component, which trigger a
traceback trying to `split` false.

closes odoo/odoo#139450

X-original-commit: 06f3cbb95b2e4cfb271039a19660991c743c2ba8
Signed-off-by: Mathieu Duckerts-Antoine (dam) <dam@odoo.com>
Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
Co-authored-by: Polymorphe57 <dam@odoo.com>
2023-10-26 13:41:14 +00:00
Rémy Voet (ryv) cae4e1b1cf [FIX] web: add fallback of display_name in formatMany2one.
Since https://github.com/odoo/odoo/pull/137098, we add a fallback for
false display_name in the form view, but we didn't add it for the list
view which doesn't instantiate the Many2one component. It uses
`formatMany2one` instead. Fix it to be consistent with the Many2one

closes odoo/odoo#139215

Signed-off-by: Aaron Bohy (aab) <aab@odoo.com>
Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
2023-10-20 09:35:23 +00:00
Rémy Voet (ryv) c3bbba7cdd [FIX] web: fix extraLines for false display_name
Since https://github.com/odoo/odoo/pull/137098, in
`Accounting > Bank > Tree View`, you get a traceback if one of the bank
statements in the view has a false as `display_name` (name is only
required in the view not in the model). The traceback comes from the
`extraLines` accessor in the Many2one component. The fix is the same
as in https://github.com/odoo/odoo/pull/138061 (FW of the first PR).

Part-of: odoo/odoo#139215
2023-10-20 09:35:23 +00:00
Rémy Voet (ryv) 0782ec07e7 [FIX] web: display_name "New" fallback should only target new record.
There is a fallback in the control panel
(https://github.com/odoo/odoo/blob/c22cb6bbadd38ecb050f6d4dc14672868beda20b/addons/web/static/src/search/control_panel/control_panel.xml#L137)
if the display_name of the record is empty.
But it is never actually used because there is another
fallback in the form_controller ("New"). This latter fallback
should only be used for new records, not for existing ones.

closes odoo/odoo#138061

Signed-off-by: Raphael Collet <rco@odoo.com>
Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
2023-10-10 12:35:01 +00:00
Rémy Voet (ryv) 56bd53d81f [FIX] web: take in account false as a valid display_name
The previous commit reverts the ORM fallback of `display_name`. Then the
`display_name` can legitimately be `False` again. But the `Many2one` and
`Many2XAutocomplete` components don't handle this case (and generate a
traceback see task-3424154).

This commit adds the same fallback ('Unnamed record') for these two
problematic components; one when the name_search returns `False` as
`display_name` (autocomplete for Many2Xfields) and another when we read
the many2one `display_name`. This name is not user friendly, but for
the business model it shouldn't happen anyway. It is also better than
the empty string which is confusing with no value. Note that there is
already a fallback for `display_name` that is used in the broadcrumb
(https://github.com/odoo/odoo/blob/50b2a24f22cb470bcc1a9befe677cf794211d7b2/addons/web/static/src/views/form/form_controller.js#L282).

Part-of: odoo/odoo#138061
2023-10-10 12:35:01 +00:00
Rémy Voet (ryv) b67df70b8a [REV] core: remove ORM display_name fallback
This reverts commit 4573ca0c83eb63785016f4389a5157efb21fa9a4.

Because now, `display_name` is implicitly on every form (last breadcrumb
item). Then it will be queried by `onchange` calls. When we create a
new record, `_rec_name` can be `False` and the display_name will be a
technical one: '<model_name>,<NewId0x...>' which is uglier than the
previous situation showing 'New'.

Part-of: odoo/odoo#138061
2023-10-10 12:35:01 +00:00
Rémy Voet (ryv) a9dd388a11 [IMP] *: use private _read_group for efficiency/consistency.
Since https://github.com/odoo/odoo/pull/110737, it is better to use
`_read_group` instead of `read_group` in the backend. In fact, the
public method is less efficient (it computes display_name of relational
groupby, extra order, ...) and more verbose.

This commit replaces these new uses of `read_group` with `_read_group`.

closes odoo/odoo#136381

Related: odoo/enterprise#47826
Signed-off-by: Raphael Collet <rco@odoo.com>
2023-09-29 14:48:13 +00:00
Rémy Voet (ryv) afef0b390f [FIX] mail: allow searching on message_partner_ids for portal
Because we now check groups in the domain, some searches raise
`AccessError` when using `message_partner_ids` in the domain for the
portal user:
- https://github.com/odoo/odoo/blob/4d1a1f1c99d6055b60921ce464b72f0f4d9bbfe2/addons/sale/controllers/portal.py#L35
- https://github.com/odoo/odoo/blob/4d1a1f1c99d6055b60921ce464b72f0f4d9bbfe2/addons/sale/controllers/portal.py#L41
- https://github.com/odoo/odoo/blob/ba1a5509fa49fd846739252d16083dd8cb334b53/addons/website_forum/models/forum_post.py#L831
- https://github.com/odoo/odoo/blob/86b43bcfa6c9a5b6ac1b3ac9ba0c308f1169ea3b/addons/hr_timesheet/models/hr_timesheet.py#L41

Since there are many invalid domains and it is impossible to sudo only
part of the domain, it is easier to override `_flush_search` to allow
`message_partner_ids` in search domain leafs with some restriction.

closes odoo/odoo#135111

Related: odoo/enterprise#47822
Signed-off-by: Raphael Collet <rco@odoo.com>
2023-09-29 14:48:09 +00:00
Rémy Voet (ryv) eb0ba7f79b [IMP] core: check field groups for search domain and order
In order to boost the read security of groups on field, we now
check groups of field used in search domain and search order.

Part-of: odoo/odoo#135111
2023-09-29 14:48:09 +00:00
Rémy Voet (ryv) f9e75d19a9 [FIX] *: Fix bad usage of 'like'/'ilike' operator
The 'like'/'ilike' operators automatically add the wildcard character
(`%`) at the beginning and at the end of the value. This commit fixes
the incorrect usage.

closes odoo/odoo#136007

Related: odoo/enterprise#47886
Signed-off-by: Raphael Collet <rco@odoo.com>
2023-09-27 09:11:44 +00:00
Rémy Voet (ryv) 51a795d363 [FIX] core: '=like'/'like' doesn't use unaccent anymore
The `=like`/`like` domain operators are accent-insensitive, but still
case-sensitive. This is not really coherent, since `unaccent` is a best
effort to find records from the client, and it is the same idea behind
being case-insensitive. Also, `=like`/`like` cannot be used from the web
client, and we use them in domain to search from the Python side.

Moreover, adding `unaccent` to `=like` can be very inefficient when
searching for a prefix ('prefix%'). In fact, PostgreSQL can use btree
index to find prefix matches, but because we create dy default btree
index without unaccent (when we put
`index=True/'btree'/'btree_not_null'` on the field), PostgreSQL cannot
use this index.

Part-of: odoo/odoo#136007
2023-09-27 09:11:44 +00:00
Rémy Voet (ryv) 82b3fe05ea [FIX] mrp: fix batch version of _set_dates
`_set_dates` didn't work properly with multiple records.
In fact, it only used the `date_start` and `date_finished` of the
 first record. The assumption was that `date_start`/`date_finished`
 is always the same for each record in self, because it usually
 comes from a write call (writing the same values to a batch of
 records). But inverse methods are also called by the create
 method, and then the previous assumption isn't true anymore.

This bug leads to several inconsistencies between the `date_start`
and the `leave_id`. The `test_replan_mo_without_bom` was fixed in
the previous version, but with the new onchange, it breaks again
because of these inconsistencies.

closes odoo/odoo#135635

Signed-off-by: Raphael Collet <rco@odoo.com>
2023-09-18 22:17:07 +00:00
Rémy Voet (ryv)andWilliam Henrotin a62baec7f6 [FIX] core: fix onchange first snapshot
The 'sale_ebay' module adds the `product_variant_ids` one2many field on
the `product.template` form view. The `product_variant_ids` view
contains `virtual_available` (depending on `uom_id`). When the user
changes the `uom_id` of the `product.template`, onchange is triggered,
it takes a snapshot of the previous data, and it will computes the
previous value of `virtual_available`. But the associated compute
method will fail with a traceback:

File "/data/build/odoo/addons/stock/models/product.py", line 199, in _compute_quantities_dict
res[product_id]['qty_available'] = float_round(qty_available, precision_rounding=rounding)
File "/data/build/odoo/odoo/tools/float_utils.py", line 54, in float_round
rounding_factor = _float_check_precision(precision_digits=precision_digits,
File "/data/build/odoo/odoo/tools/float_utils.py", line 29, in _float_check_precision
assert precision_rounding is None or precision_rounding > 0,\
AssertionError: precision_rounding must be positive, got 0.0

The `precision_rounding` is `0.0` because the `uom_id` of the product is
empty. It is is empty because we force the `uom_id` of the
`product.template` to be `False` in `initial_values` (before the
snapshot), and then the `uom_id` takes the value of its
`product.template` (`False`). But actually, the cache of the product
should be full with its previous values before doing the snapshot.
This was not the case because we only copy data from store fields
(see `fnames`). Then compute fields was computed after setting field
change to `False`.

opw-3334822
opw-3419392

X-original-commit: 5021e77ba52fac465a5d842ac04c0a3a22aea2dd
Part-of: odoo/odoo#135635
Co-authored-by: William Henrotin (whe) <whe@odoo.com>
2023-09-18 22:17:06 +00:00
Rémy Voet (ryv) 35f739f558 [FIX] sale: avoid displaying address
Since https://github.com/odoo/odoo/pull/122085, on the sale
order form (with website_sale installed), changing the "Customer"
changes the "Invoice Address" & "Delivery Address". But the latter
are displayed with the partner name, but also with the full
partner address. This last information makes the form uglier than
before and are useless.

The old `display_name` of `res.partner` was stored in the DB and
did not depend on the context. Also, onchange calls add the
context of the source field (the one being changed). In this case,
`'show_address': 1` is added, and then `display_name` of
`partner_invoice_id`/`partner_shipping_id` is also read with this
context.

We cannot easily fix this in saas16-4 because it was not possible
to change the read context of a particular field. With the new
specification of onchange, we can override the read context on a
particular field.

closes odoo/odoo#134304

Signed-off-by: Raphael Collet <rco@odoo.com>
2023-09-06 13:25:28 +00:00
Rémy Voet (ryv) 013332d791 [FIX] core: add display_name fallback
Since 3c62ca1eb9, if the `_rec_name` value
is False, `name_search` and `name_create` will return a tuple of
(<id>, False). This is an invalid response for the web client,
which triggers a JS traceback.

Instead of using the old behavior (returning an empty string, resulting
in a partially invisible row in the Many2one selection),
use the same fallback as when the `_rec_name` doesn't exist.

Since `display_name` should never be Falsy anymore, remove part of the
test_mail_message_values_fromto_long_name that covers the
Falsy `display_name` case.

task-3424154

closes odoo/odoo#133691

X-original-commit: 0cb9e66edd9b7142a6e56bc6ee6491e6d6047e51
Signed-off-by: Raphael Collet <rco@odoo.com>
Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
2023-08-31 09:05:02 +00:00
Rémy Voet (ryv) 1bceb01ff4 [FIX] website: fix unlink page with ir.rule
Issue
=====
The previous commit forced consistency between `check_access_rule` and
`_apply_ir_rule`. Now the parent model `ir.rule` (via inherited) is also
checked before the unlinking (`check_access_rule("unlink")`). The
`website.page` unlink override sometimes calls `unlink` on its parent
view and because view_id has `ondelete="cascade"`, it will actually
deletes the `website.page` itself. Then calling to `super().unlink()`
with self will raise a MissingError.

Fix
===
Batch the old logic and remove already unlinked record from `self`
before calling `super`.

closes odoo/odoo#125916

Signed-off-by: Raphael Collet <rco@odoo.com>
2023-08-21 19:56:56 +02:00
Rémy Voet (ryv) 160acc0200 [FIX] core: fix inconsistencies between _apply_ir_rule and check_access_rule
Issues
======
- `_apply_ir_rule` applies `ir.rule` of the current model and also
`ir.rule` from the inherited model (via inherits). But
`check_access_rule` doesn't check the later one.
- `_flush_search` doesn't flush fields coming from the `ir.rule` of
the inherited model (via inherits). Then the filtering done by
`_apply_ir_rule` may be inconsistent with cached values.

Changes
=======
Because of https://github.com/odoo/odoo/blob/6ddcb448612f5d784c8e9ebb90f19077e65be3e1/odoo/osv/expression.py#L1073-L1073,
and https://github.com/odoo/odoo/blob/00e86b1552d1e5541a8dbf9411de5cfdb8990cc4/odoo/fields.py#L2895
leaf like `('<many2one_delegate>', 'any', [<sub-domain>])`,
will be translated in the same way as `_inherits_join_add` does.
We can remove `_inherits_join_add` and its usage in `_apply_ir_rule`
and change `ir.rule._compute_domain` to also return the inherited
(via inherits) `ir.rule` domain (with the new 'any' operator).
Since `_compute_domain` is used by `_apply_ir_rule` and
`_filter_access_rules_python`, everything is consistent.

Also fix `BaseModel._flush_search` to take in account 'any'/'not any'
operators (compulsory in order to flush correctly new domain
from `ir.rule._compute_domain` generated).

Part-of: odoo/odoo#125916
2023-08-21 19:56:55 +02:00
Rémy Voet (ryv) fcb092af12 [FIX] core: add test to ensure correct call of _compute_display_name
Add test to ensure that `_compute_display_name` is called once with
the correct recordset during `read_group`. Also
fix and small typo in the documentation of `read_group`.

closes odoo/odoo#132261

X-original-commit: 60477586f11ca7eb698240a6bd3f565b5a9e3279
Signed-off-by: Julien Castiaux (juc) <juc@odoo.com>
Signed-off-by: Raphael Collet <rco@odoo.com>
Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
2023-08-18 12:17:57 +02:00
Rémy Voet (ryv) 4dc9e42a64 [FIX] *: fix usages of _read_group with NewId
In https://github.com/odoo/odoo/pull/110737, I didn't consider that
compute could be called on NewId record with `_origin`. Some compute
methods are badly refactored with the new signature of `_read_group`.
We use recordsets returning from `_read_group` to assign field value to
`self`. But if `self` contains `NewId` with `origin`, these records
don't represent `self`, it contains real record instead of the one with
NewId + origin. Then the assignations are done on records not in `self`
which may lead to generate traceback or write to other records during
an onchange.

Fix multiple compute to work correctly with NewId (origin set) record.

X-original-commit: bd22d0a5c479a72cdaf799309387e41ce692bb29
Part-of: odoo/odoo#132261
2023-08-18 12:17:56 +02:00
Rémy Voet (ryv) a74d020359 [FIX] website_hr_recruitment: fix broken display_name
With https://github.com/odoo/odoo/pull/122085,
we simplified the definition of display_name for `ir.department`:
https://github.com/odoo/odoo/commit/3c62ca1eb96d571b2b686b5caee370324c589ab4#diff-404a1cabe61e6fcb32cc3ac3d3d36336b9313f162130c3edbe1066c130b09c0dR11
but because of https://github.com/odoo/odoo/blob/0d30cc2bc9b9cc2b805d6c2d0a440f185c648da0/odoo/models.py#L235,
it overstates completely the default definition instead of
merging definition as others fields.

Fix the change.

closes odoo/odoo#131445

X-original-commit: 17eed41b81049a25db42a3e8389bd931433b0717
Related: odoo/enterprise#45579
Signed-off-by: Julien Castiaux (juc) <juc@odoo.com>
Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
2023-08-10 09:12:46 +02:00
Rémy Voet (ryv)andJulien00859 55c71cb224 [FIX] base: ir.property.search_multi with 'any'/'not any' operators
Install POS, go in the settings, add a new cash routing method.
Traceback, the field as no comodel.

The ir.property `search_multi` method wasn't compatible with the
'any'/'not any' operator.

opw-3375624

closes odoo/odoo#127750

X-original-commit: 7dd14eb0b0edb57150288361e72e407e05e67a73
Signed-off-by: Raphael Collet <rco@odoo.com>
Signed-off-by: Rémy Voet (ryv) <ryv@odoo.com>
Co-authored-by: Julien00859 <juc@odoo.com>
2023-07-07 16:42:45 +02:00
Rémy Voet (ryv) 22a0c7f9ee [FIX] core: filtered_domain with 'any'/'not any'
`filtered_domain` didn't throw an exception when it is called with a
domain containing the new 'any' and 'not any' operators. Fix it.

X-original-commit: 05713a270bfad406daa9dbf739b80cf8c4d065c0
Part-of: odoo/odoo#127750
2023-07-07 16:42:45 +02:00
Rémy Voet (ryv) 78165067a6 [FIX] core: fix semantic of 'not any' operator with many2one field.
Since 5a998694a6,
`[('<many2one>', 'not any', [<domain>])]` matches rows where the
<many2one> is set and the corresponding many2one row matches the
<domain>. This is incorrect. 'not any' should be the inverse of
the 'any' operator and domains such as
`['!', ('partner_id.name', '=', 'System')]` are incorrectly converted
to "Return every record with a partner name != 'System'"
when it should be "Return every record with a partner name != 'System'
OR without partner at all".

Fix semantic and add tests to avoid any future regressions.

X-original-commit: c8c1ef45f24482e380529daf2de55b9091338a83
Part-of: odoo/odoo#127750
2023-07-07 16:42:44 +02:00
Rémy Voet (ryv) c5cb357d90 [IMP] *: add dependencies to display_name field
The previous commit introduced an optimization to reduce the number of
queries and fields fetched when we call `name_search`. But it works much
better when the dependencies of `display_name` contain field names used
in the calculation (on the same record/model).

Then, to improve the performance and the cache coherency, add `depends`
and `depends_context` depending on the custom `_compute_display_name`.
Add only the first level of dependencies (never traverse relational
field) because only these have a positive impact on the previous
optimization and the cost is very low (see `modified`).
About `depends_context`, we don't include `lang` because (when `_` is
used by example) it is unlikely to get the same display_name in the same
request with two different lang.

closes odoo/odoo#122085

Related: odoo/documentation#4639
Related: odoo/enterprise#42599
Related: odoo/upgrade#4780
Signed-off-by: Raphael Collet <rco@odoo.com>
2023-06-28 17:41:19 +02:00
Rémy Voet (ryv) daf7ca521d [IMP] core: fetch display_name during name_search
The `name_search` makes at least 2 SQL requests, one to find the records
and one to read fields needed to compute the `display_name`. With the
default `_compute_display_name`, it only needs to fetch the `_rec_name`
field (if there is one), but the ORM perfetch field mechanism will also
fetch every prefetchable field (see `_fetch_field`).

Then, to avoid running two queries and fetching too many fields, we add
the dependency fields (with `_determine_fields_to_fetch`) to the select
clause of the `Query` returned by `_name_search`.

Part-of: odoo/odoo#122085
2023-06-28 17:41:19 +02:00
Rémy Voet (ryv) 3c62ca1eb9 [REM] core: remove name_get API
Rationale
=========

Since v8, the `display_name` field is present on all models. By default,
`display_name` uses `name_get` which has pretty much the same purpose
(return record name used by the web client). Gradually, many (backend)
developers (and the ORM: https://github.com/odoo/odoo/commit/6da1c3ac4c036eac289597602976538e243cb939)
started using `display_name` (more convenient than
`record.name_get()[0][1]`) but it still had the `name_get` override.
It becomes more complex than necessary and poeple start to misunderstand
the two (and sometimes override both, leading to inconstiencies between
`display_name`/`name_get`).

To simplify the ORM and the API, we decided to keep only one of them,
the `display_name` field:
- It is much more convenient from a backend point of view
(`record.name_get()[0][1]` vs `record.display_name`)
- It is cached during the same transaction (and invalidated if
its dependencies change)
- It can be overridden like any other compute field (override
`_compute_display_name` with any extra dependencies)
- `name_get` is replaced by `read(['display_name'])`
(API perceptive), which can actually be more efficient
(if `display_name`'s depends are correct, the ORM will only fetch the
fields it needs instead of every prefetchable field)

Changes
=======

- Deprecates `name_get` for the v17 and based the method on
`display_name` (the opposite of before)
- Converts all usage of `name_get`
- Overrides of `name_get` are now overrides of `_compute_display_name`
- For `res.partner`, rename the field store `display_name` into
`complete_name` because `display_name` context-dependent and it makes
no sense to have a compute store that is context-dependent.
- Previously, it was possible to return multiple names for the same
record with `name_get`, but it was tricky and most of the usage of
this `name_get` didn't take this into account. The only example of
this is the `name_get` of `product.product`
(now use `", ".join(<names>)`).

Part-of: odoo/odoo#122085
2023-06-28 17:41:19 +02:00
Rémy Voet (ryv) cd7a10d06d [FIX] core,account_sequence: avoid add_to_compute no-store no-compute fields
Since https://github.com/odoo/odoo/pull/121355,
add_to_compute on no-store compute field, lead to recompute the field
at the end of request and it can lead to some non deterministic bug
(compute with a bad context by example).
Avoid to add_to_compute no-store or no-compute fields.

closes odoo/odoo#122973

X-original-commit: 533193106e6c9460d741e504babc7591cb935939
Signed-off-by: Raphael Collet <rco@odoo.com>
Signed-off-by: Christophe Simonis (chs) <chs@odoo.com>
Signed-off-by: Christophe Monniez (moc) <moc@odoo.com>
2023-05-31 09:23:18 +02:00
Rémy Voet (ryv) 28d47d1977 [IMP] core: avoid extra invalidate of compute no-store field.
`tocompute` in the `Transaction` contains store field on records to
be recomputed. No-store compute fields are directly invalidated from
the cache when a dependency changes (see `BaseModel.modified`).

In fact, `_recompute_field` was actually doing too much for nothing.
Also, it may invalidate caches of compute no-store fields for no reason
(e.g., if they are searchable). Remove the part for field compute
no-store field. And prevent `_recompute_field` callers from calling it
with no-store fields.

closes odoo/odoo#122147

X-original-commit: ba9ccb07fb12558667db97b866df492fd0f5ba4d
Signed-off-by: Raphael Collet <rco@odoo.com>
Signed-off-by: Rémy Voet <ryv@odoo.com>
2023-05-25 16:29:27 +02:00
Rémy Voet (ryv) 4e0eed85d6 [FIX] core: maximum recursion because of active fields.
In specific situation, unlink can lead to raise a `RecursionError`:
- The model `A` has a many2one `b_id` field toward a model `B`.
This field is set with `ondelete='cascade'`.
- The model `A` has one **store** related field **no-sudo** named
`a_related` (`related='b_id.b_other_field`).
- With `ir.rule` on model `A` with a domain containing `a_related`

You have one record B `b_1` with 20 records A linked to it
(`a_1, ..., a_20`). When you try to unlink `b_1`:

Stack:

  File "...", line 543, in ...
    b_1.unlink()
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 3594, in unlink
    self.env.flush_all()

=> At this point, `a_1, ..., a_20` have already been deleted from the
database because of the 'cascade' deletion. But the ORM doesn't have
any information about this, and `a_related` (for `a_1, ..., a_20`) are
flagged to be recomputed (because it depends on `b_id.b_other_field`)

  File "/home/odoo/Documents/dev/odoo/odoo/api.py", line 732, in flush_all
    self._recompute_all()
  File "/home/odoo/Documents/dev/odoo/odoo/api.py", line 728, in _recompute_all
    self[field.model_name]._recompute_field(field)
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 6165, in _recompute_field
    field.recompute(records)
  File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 1348, in recompute
    self.compute_value(record)

=> `self.compute_value(recs)` raised a `MissingError` before recalling
`compute_value` with only the first `record` (but others are still in
the prefetch)

  File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 1368, in compute_value
    records._compute_field_value(self)

=> `a_related` of `record` is removed from to_compute, but only the
first record, not the rest of the records present in the prefetch set.

  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 4194, in _compute_field_value
    fields.determine(field.compute, self)
  File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 100, in determine
    return needle(records, *args)
  File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 689, in _compute_related
    values = [first(value[name]) for value in values]
  File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 689, in <listcomp>
    values = [first(value[name]) for value in values]
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 5860, in __getitem__
    return self._fields[key].__get__(self, type(self))
  File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 2772, in __get__
    return super().__get__(records, owner)
  File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 1186, in __get__
    recs._fetch_field(self)
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 3162, in _fetch_field
    self._read(fnames)

=> `_read` tries to read the first record + others from the prefetch set

  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 3215, in _read
    self.with_context(active_test=False)._flush_search([], order='id')
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 4607, in _flush_search
    self.env[model_name].flush_model(field_names)
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 5560, in flush_model
    self._recompute_model(fnames)
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 6134, in _recompute_model
    self._recompute_field(field)

=> This is where the recursion starts, record compute will move forward
one by one. But sadly, the stack grows very fast, and with only a few
(already deleted) records to recompute, the issue will be generated.

  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 6165, in _recompute_field
    field.recompute(records)
  File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 1348, in recompute
    self.compute_value(record)
  File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 1368, in compute_value
    records._compute_field_value(self)
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 4194, in _compute_field_value
    fields.determine(field.compute, self)
  File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 100, in determine
    return needle(records, *args)
  File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 689, in _compute_related
    values = [first(value[name]) for value in values]
  File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 689, in <listcomp>
    values = [first(value[name]) for value in values]
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 5860, in __getitem__
    return self._fields[key].__get__(self, type(self))
  File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 2772, in __get__
    return super().__get__(records, owner)
  File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 1186, in __get__
    recs._fetch_field(self)
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 3162, in _fetch_field
    self._read(fnames)
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 3215, in _read
    self.with_context(active_test=False)._flush_search([], order='id')
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 4607, in _flush_search
    self.env[model_name].flush_model(field_names)
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 5560, in flush_model
    self._recompute_model(fnames)
  File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 6134, in _recompute_model
    self._recompute_field(field)

How to fix it:
Move the logic of the MissingError of `_recompute_field` inside the
`recompute` directly.

X-original-commit: c2aac02ac4f8c5cc4a9324134535393bd97338ce
Part-of: odoo/odoo#122147
2023-05-25 16:29:26 +02:00
Rémy Voet (ryv) 8f600f8a4b [REV] core: handle recursion error when resolving stored fields
This reverts commit 9e71094582ec4c9b719431e77538da8f91ffa9e3.

Why?
- It is useless in 16.0, the bug it claims to fix should not exist. In the
commit explanation the sentence 'this calls `_read`, which flushes the
field we're trying to read' is wrong, since
https://github.com/odoo/odoo/pull/66938 (merged in 15.5). (It is still true
for fields that are in an ir.rule, but it sounds very unlikely to have
a recursive field in ir.rule that causes trigger the problem)
- It creates worst errors (infinite loop for recursive field computation on
missing record - Next commit).
- Also, it looks like a dangerous fix that can hide or trigger new
issues.

X-original-commit: 4a46c1049a4cdc15f0e211d228fb976ba7a0173d
Part-of: odoo/odoo#122147
2023-05-25 16:29:26 +02:00
Rémy Voet (ryv) d2c2f5bfc2 [FIX] core: invalid field should raise an Exception in domain
Since https://github.com/odoo/odoo/commit/5a998694a6f353da05d7f69e4c59b9e7dd139e27,
we don't crash anymore at https://github.com/odoo/odoo/commit/5a998694a6f353da05d7f69e4c59b9e7dd139e27#diff-fa4d9268d6e65e19aebec81c46038f0e496b91142588ed4d1c1bce7ff2338f2cL759
(at `field.auto_join`) if `len(path) > 1`, `field` is translated the
`left` is not only a field name (example: `"name.<something else>"`).
Because we trust `left` at this [point](https://github.com/odoo/odoo/blob/1bbdd77f0ee6bd632f5ade88b8b71c5576fa9053/odoo/osv/expression.py#L1339),
(we shouldn't, coming from https://github.com/odoo/odoo/pull/101115)
then SQL expression generated for translated field is unsafe
(SQL injection).

In case of translated field, check that `left` side is only a valid
field name. (if it is not, it will crash later in `__leaf_to_sql`).
In addition, use `field.name` instead of `left` when it is possible to
be more robust.

closes odoo/odoo#120565

Signed-off-by: Vincent Schippefilt (vsc) <vsc@odoo.com>
2023-05-09 18:26:52 +02:00
Rémy Voet (ryv) 302c7baa87 [REM] core,*: remove name_get_uid parameter from _name_search.
`name_get_uid` is unused (at least since v14) and the
documentation about it, is wrong.

closes odoo/odoo#117819

Related: odoo/enterprise#39483
Signed-off-by: Rémy Voet <ryv@odoo.com>
2023-04-28 16:04:27 +02:00
Rémy Voet (ryv) fde7727dd8 [FIX] core: fix recursion error from unlink
On a model X, where there is a field related x_related
(related= 'y_id.y_translate') towards a translate field y_translate
on Model Y.
When you unlink at least 1001 records of X
(r1, r2, ... , r1000, r1001) (cr.MAX_IN + 1), you get a traceback
(`RecursionError: maximum recursion depth exceeded in comparison`).

The stack looks like:

File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 3609, in unlink
  self.env.flush_all()
File "/home/odoo/Documents/dev/odoo/odoo/api.py", line 732, in flush_all
  self._recompute_all()
File "/home/odoo/Documents/dev/odoo/odoo/api.py", line 728, in _recompute_all
  self[field.model_name]._recompute_field(field)
File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 6179, in _recompute_field
  field.recompute(records)
File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 1348, in recompute
  self.compute_value(record)
File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 1368, in compute_value
  records._compute_field_value(self)
File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 4209, in _compute_field_value
  fields.determine(field.compute, self)
File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 100, in determine
  return needle(records, *args)
File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 689, in _compute_related
  values = [first(value[name]) for value in values]
File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 689, in <listcomp>
  values = [first(value[name]) for value in values]
File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 5874, in __getitem__
  return self._fields[key].__get__(self, type(self))
File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 2771, in __get__
  return super().__get__(records, owner)
File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 1186, in __get__
  recs._fetch_field(self)
File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 3162, in _fetch_field
  self._read(fnames)
File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 3214, in _read
  self.flush_recordset(translated_field_names)
...

-> Extra info:
translated_field_names = ['x_related']
`self = X(r1001, r1, r2, ..., r999)`

...
File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 5585, in flush_recordset
  self._recompute_recordset(fnames)
File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 6162, in _recompute_recordset
  self._recompute_field(field, self._ids)
...

-> Long recursion starts here but first records to be recomputed will be r1, then r2, then r3, ...
But the maximum recursion depth error will be triggered earlier at the
end of the recursion, because the stack limit in Python is 1000
by default.
...
File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 6179, in _recompute_field
    field.recompute(records)
File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 1348, in recompute
  self.compute_value(record)
File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 1368, in compute_value
  records._compute_field_value(self)
File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 4209, in _compute_field_value
  fields.determine(field.compute, self)
File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 100, in determine
  return needle(records, *args)
File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 689, in _compute_related
  values = [first(value[name]) for value in values]
File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 689, in <listcomp>
  values = [first(value[name]) for value in values]
File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 5874, in __getitem__
  return self._fields[key].__get__(self, type(self))
File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 2771, in __get__
  return super().__get__(records, owner)
File "/home/odoo/Documents/dev/odoo/odoo/fields.py", line 1186, in __get__
  recs._fetch_field(self)
File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 3162, in _fetch_field
  self._read(fnames)
File "/home/odoo/Documents/dev/odoo/odoo/models.py", line 3214, in _read
  self.flush_recordset(translated_field_names)
...

------ Extra info:
translated_field_names = ['x_related']
`self = X(r1, r2, ... , r999)`
...

Since 9c3b9a4926, the `x_related` is
flagged to be recomputed at the end of the delete
loop of `unlink`, but it shouldn't be, because records are deleted now.
The problem is in these lines:
```python
with self.env.protecting(self._fields.values(), records):
    self.modified(self._fields, before=True)
```
The `records` are only a part of `self` (batch of 1000), so the
`protecting` call only protects the current batch and not `self`.
Then, the second batch (here with only one record), will flag to recompute
`x_related` of the first batch records. Then, later on, the `flush_all`
will generate the recursion error trying to resolve
these `to_recompute`.

To fix it, only move the modified call (+ protecting) before
the batch loop and executes it on `self`.

This issue shouldn't exist in master, because having a
related translate field triggers a warning (`Translated stored related
field (<field_name>) will not be computed correctly in all languages`).
Also `https://github.com/odoo/odoo/pull/100472` fixes the issue in
master, but it generates one SQL request by record, which isn't great.
Then in master, we should forward this commit (but test will be remove
because it generates the warning message).

closes odoo/odoo#119471

X-original-commit: 78450cae5900e267de439e4988b314aa644e76bd
Signed-off-by: Raphael Collet <rco@odoo.com>
Signed-off-by: Rémy Voet <ryv@odoo.com>
2023-04-24 23:48:55 +02:00
Rémy Voet (ryv) f25df506af [FIX] calendar: groupby on calendar.event triggers 'danger' notification
The `read_group` of `calendar.event` sends a danger notification when
the user groups by any field in the list view. It is because the
security reenforcement done in 2c0b3ab670168a155b3974d5a5b39aa1f1df3452
is too strict. It checks all `fields`, even the ones that are filtered
out by the `read_group` (`fields` without aggregation specification
nor `group_operator`).

closes odoo/odoo#119459

Signed-off-by: Raphael Collet <rco@odoo.com>
2023-04-24 23:48:53 +02:00
Rémy Voet (ryv) 0c9e591965 [FIX] core: read_group same relational grouping
Before 916c9c46f58f27c68c5558a2a57fdca43a89fe89, we could
groupby several times on the same relational field with `read_group`
Example: `read_group(..., groupby=['product_id', 'product_id'], ...)`.
Now, it produces a traceback:

  File "/data/build/odoo/odoo/models.py", line 2583, in read_group
    self._read_group_format_result(rows_dict, lazy_groupby)
  File "/data/build/odoo/odoo/models.py", line 2396, in _read_group_format_result
    ids = [row[group].id for row in rows_dict if row[group]]
  File "/data/build/odoo/odoo/models.py", line 2396, in <listcomp>
    ids = [row[group].id for row in rows_dict if row[group]]
AttributeError: 'tuple' object has no attribute 'id'

It is because `_read_group_format_result` try to convert the record
into tuple (id, display_name) twice (one for each groupby).

Fix this issue introduced by the refactor of `_read_group`.

Part-of: odoo/odoo#119459
2023-04-24 23:48:53 +02:00
Rémy Voet (ryv) 70fd18ef67 [FIX] web: fix bad groupby as str instead of list
A mistake introduced in  234db70d86, the
`groupby` of  `_read_group` should be list/tuple of  `str`, not a `str`.

closes odoo/odoo#119400

Signed-off-by: Raphael Collet <rco@odoo.com>
2023-04-22 06:53:49 +02:00
Rémy Voet (ryv) 3864f2cf84 [FIX] core: fetch method with 'id' always generates sql query.
Calling `fetch` with 'id' in the `fields_name`, will always generate
SQL query even if all requested field values are in the cache.
This is because we also look for values in the 'id' field cache,
but we don't ever fill the cache for `Id` fields.

closes odoo/odoo#119107

X-original-commit: 3ba8d0e5e57e1358cc8958abf511d514515eccb2
Signed-off-by: Vincent Schippefilt (vsc) <vsc@odoo.com>
2023-04-20 10:51:54 +02:00
Rémy Voet (ryv) 6d930c7b9d [REF] mail: replace override of read_progress_bar _read_group_groupby
closes odoo/odoo#110737

Related: odoo/documentation#4064
Related: odoo/enterprise#38639
Signed-off-by: Vincent Schippefilt (vsc) <vsc@odoo.com>
2023-04-19 21:58:28 +02:00
Rémy Voet (ryv) b60cf1f977 [REF] _read_group_raw doesn't exists anymore
`_read_group_raw` has been remove in previous commit. Convert all usage.

Part-of: odoo/odoo#110737
2023-04-19 21:58:28 +02:00
Rémy Voet (ryv) 81a892ac8c [REF] core: read_group() now uses _read_group()
Make the public `read_group` depends of its private method `_read_group`
refactored to match the backend usage.

We try to keep the public API similar for this first part of the
rafactor, but there are still some API change:
- We cannot order by `id` anymore.
- The display_name of many2x group values are not lazy anymore.

Part-of: odoo/odoo#110737
2023-04-19 21:58:28 +02:00
Rémy Voet (ryv) e5b42a0a20 [IMP] tools: date_range accept date params
Part-of: odoo/odoo#110737
2023-04-19 21:58:27 +02:00
Rémy Voet (ryv) 88b5135e5f [IMP] core,*: simplify security of read_group
Lot of override of read_group reimplement partially custom security
rule of `_search` method. In order to simplify these security check
and have a consistent behavior between the search and read_group method,
_read_group now use _search to create the from and where clause.

Part-of: odoo/odoo#110737
2023-04-19 21:58:27 +02:00
Rémy Voet (ryv) 687d0ed285 [IMP] *: change the override of read_group
The new backend version of read_group can simplify the
current overrides of read_group. Do it for each of them and
avoid making extra search (done with __domain) when it is possible.

Part-of: odoo/odoo#110737
2023-04-19 21:58:27 +02:00
Rémy Voet (ryv) 234db70d86 [IMP] *: Use the new API of _read_group for backend use
Part-of: odoo/odoo#110737
2023-04-19 21:58:27 +02:00
Rémy Voet (ryv) cdebf336a5 [REF] core: _read_group for backend use
The `_read_group` was designed to be used by the web client to
efficiently compute aggregations grouped by one or more fields.
However, more and more developers have been using it from the backend
to make computations more efficient (avoid doing the aggregation
in Python). Unfortunately, the API was designed for the web client,
which added a lot of boilerplate when used in the Python (list of
dict with misleading key name choices).

`_read_group` was created to improve the performance of read_group
for backend use (4ef0c00b4b), but didn't
change the API and based the implementation on read_group itself.

Rewrite `_read_group` from scratch with a new API to make it easier
to use from the backend (see the method documentation). Also, split
the method to make it easy to override and add custom behavior.

Part-of: odoo/odoo#110737
2023-04-19 21:58:26 +02:00
Rémy Voet (ryv) 9c5554faba [FIX] web: don't create warned orderby for read_group
`read_group` cannot order on the fields that are not
aggregates in the same transaction. We get a warning for it:
`<model>: read_group order by '<field_name> ASC' ignored, cannot sort on empty columns (not grouped/aggregated)`

Fix it.

Part-of: odoo/odoo#110737
2023-04-19 21:58:26 +02:00
Rémy Voet (ryv) cfbcc71c0a [FIX] core: sequence fields doens't have default group_operator
Part-of: odoo/odoo#110737
2023-04-19 21:58:26 +02:00
Rémy Voet (ryv) 603c16b1a0 [FIX] core,fleet: remove group_operator for Many2oneReference
The default `group_operator` of `Many2oneReference` is 'sum' (inherited
from the parent class `Integer`), it doesn't make any functional sense
to sum ids.

Also set group_operator to None on `co2` instead of override read_group
for the same result.

Part-of: odoo/odoo#110737
2023-04-19 21:58:26 +02:00
Rémy Voet (ryv) 016f26a931 [FIX] base: wrong value profiled
closes #odoo/odoo#88276

Signed-off-by: Christophe Matthieu (chm) <chm@odoo.com>
2023-03-13 11:03:46 +01:00
Rémy Voet (ryv) 360c48299a [REV] base_automation: avoid recomputing readonly field before write
This reverts commit 0561ad2324905aed5afd5cc163e5e6aebee09d40.

closes odoo/odoo#115425

X-original-commit: f188bf734f56cf2ab045aff1060aa048ed6cf58c
Signed-off-by: Julien Castiaux (juc) <juc@odoo.com>
Signed-off-by: Rémy Voet <ryv@odoo.com>
2023-03-16 09:37:11 +01:00
Rémy Voet (ryv) 9b7c372602 [IMP] core: avoid queries when creating records with one2many values
When we create a new record X and its values contains an one2many field
with either a recordset Y or a Command.set(ids), the ORM generates extra
SQL queries to search and remove the existing lines of the new record.
The call chain is as follows:

    Model._create
        -> _RelationalMulti.create
            -> _RelationalMulti.write_batch
                -> One2many.write_real

But because record X is brand new, it has no lines to begin with.  The
fix consists in skipping the search and removal in that case.

Also remove any nondeterministic behavior of method write_real() using
an OrderedSet instead of a builtin set.

closes odoo/odoo#114726

X-original-commit: 428827e5213ee002a5b50d864f8ce1f24d1842b3
Signed-off-by: Raphael Collet <rco@odoo.com>
2023-03-09 15:54:53 +01:00
Rémy Voet (ryv) 45115cb9c2 [IMP] core: add a representation of a TriggerTree
The TriggerTree class does not have a specific __repr__().  It thus
falls back on dict's __repr__(), which does not show the root of the
tree.  This makes debugging hard and confusing.

closes odoo/odoo#113521

Signed-off-by: Raphael Collet <rco@odoo.com>
2023-03-08 22:29:51 +01:00
Rémy Voet (ryv) f5503bf295 [REM] core: remove useless/inefficient flush_all in unlink
Doing a `flush_all` at the end of `unlink` is useless and can generate
extra queries for no reason. Remove it.

Part-of: odoo/odoo#113521
2023-03-08 22:29:51 +01:00
Rémy Voet (ryv) ba565758c6 [REM] core: remove deprecated methods of models.py
closes odoo/odoo#112817

Signed-off-by: Vincent Schippefilt (vsc) <vsc@odoo.com>
2023-02-17 12:07:14 +01:00
Rémy Voet (ryv) 5c413c43e2 [REM] core: remove deprecated method of sql_db.py
Part-of: odoo/odoo#112817
2023-02-17 12:07:14 +01:00
Rémy Voet (ryv) 529a363519 [REM] core: remove useless return value from Field.write.
According to the method documentation, `Field.write` should return
the subset of record actually write.
But it is not respected at every return, it is not used at all and it generated extra completixy for nothing.

Then remove every return values.

closes odoo/odoo#111108

Signed-off-by: Raphael Collet <rco@odoo.com>
2023-02-17 12:07:08 +01:00
Rémy Voet (ryv) 90b6334aca [REM] core: simplify _update of _RelationalMulti
The `_update` of `_RelationalMulti` return a bool but the
others `_update` methods doesn't return anything.
It is actually not used. Also, the `_update` is always called with
a recordset as `value`, then the first part of the method is useless.

Part-of: odoo/odoo#111108
2023-02-17 12:07:07 +01:00
Rémy Voet (ryv) b998d9a5de [REM] base: remove useless _remove_inverses of Many2oneReference
The `_remove_inverses` method of `Many2oneReference` class
has been unused since its introduction. Remove it.

Part-of: odoo/odoo#111108
2023-02-17 12:07:07 +01:00
Rémy Voet (ryv) 79a46cffb8 [REM] core: remove small deadcode from Selection field
`convert_to_cache` of the `Selection` field type, check that the
column_type is 'int4'. But nowadays (since
a0e05e2ab9), a `Selection` field can
only be a Varchar type.

Part-of: odoo/odoo#111108
2023-02-17 12:07:07 +01:00
Rémy Voet (ryv) 22b0c88958 [REM] core: remove unused null method on Field class
This method was unused since 8bc7d8565c.

Part-of: odoo/odoo#111108
2023-02-17 12:07:06 +01:00
Rémy Voet (ryv) 5a870462e0 [REM] base: remove _patch_method and _revert_method
There is only one legitimate usage of `_patch_method` (base_automation)
and none of `_revert_method`. The other uses are in tests and they are
all wrong: if the test crashes between the `_patch_method` and the
`_revert_method`, the method will not be reverted.

For the only proper usage of `_patch_method`, move the code to this
place. Correct tests using `_patch_method`/`_revert_method` by calling
the `patch` method of `BaseCase`.

Also, remove the `api.returns` from the `create` method of `BaseModel`
as it is useless and confusing. In fact, we never use it because we
have a special treatment at the RPC level for the `create` method
(see `_call_kw_model_create`).

closes odoo/odoo#110370

Signed-off-by: Raphael Collet <rco@odoo.com>
2023-01-25 16:36:53 +01:00
Rémy Voet (ryv) f6cf94d4bd [FIX] *: ormcache works with annotation
The `ormcache` decorator fails to create the key method
(`determine_key`) when the method signature contains any annotation.
Fix it by removing annotation of the signature.

closes odoo/odoo#109777

Signed-off-by: Raphael Collet <rco@odoo.com>
2023-01-23 14:25:14 +01:00
Rémy Voet (ryv) 880daf6e59 [REM] base: remove useless module_nr field of ir.module.category
The last usage was an unused tree, previously removed.

closes odoo/odoo#109420

Related: odoo/enterprise#35586
Related: odoo/upgrade#4192
Signed-off-by: Vincent Schippefilt (vsc) <vsc@odoo.com>
2023-01-10 18:54:48 +01:00
Rémy Voet (ryv) 782fb61a3b [REM] base: remove useless list view of ir.module.category
The tree view `view_module_category_tree` was unused, remove it.
The form view (`view_module_category_form`) of `ir.module.category` is
still used in the `view_groups_form` view.

Part-of: odoo/odoo#109420
2023-01-10 18:54:47 +01:00
Rémy Voet (ryv) 2e2ca3dac0 [REM] base: remove useless field field_parent of ir.ui.view.
This field is useless since 905e01921f.
Remove it and all occurence of it in view records.

Part-of: odoo/odoo#109420
2023-01-10 18:54:47 +01:00
Rémy Voet (ryv) f9dc7e2698 [REM] base: remove deprecated methods of ir.ui.view
Since https://github.com/odoo/odoo/pull/87522 (landed in v16),
`fields_view_get`, `_fields_view_get` and `load_views` are deprecated.
Remove it for maintenance (and it is the last usage of
the useless field `field_parent`).

Part-of: odoo/odoo#109420
2023-01-10 18:54:47 +01:00
Rémy Voet (ryv) 2f5eaedd2e [REM] purchase_stock: remove avg_receipt_delay field
Since https://github.com/odoo/enterprise/pull/31641, this field
is not used anymore. Then remove it and remove
the override of read_group linked to.

odoo/upgrade#4162

closes odoo/odoo#108977

Signed-off-by: William Henrotin (whe) <whe@odoo.com>
2023-01-05 15:37:25 +01:00
Rémy Voet (ryv) 81d3c982fa [REM] purchase: remove avg_days_to_purchase field
Since https://github.com/odoo/enterprise/pull/31641, this field
is not used anymore. Then remove it and remove
the override of read_group linked to.

odoo/upgrade#4162

Part-of: odoo/odoo#108977
2023-01-05 15:37:24 +01:00
Rémy Voet (ryv) c6c79473dd [FIX] core: unexpected MissingError when prefetching record
This is a rare case where method _read() raises a MissingError instead
of just ignoring it.

The issue is triggered by several conditions on a model M:
 - at least one ir.rule on M with a domain using a column field on M;
 - one deleted record Y which is in the prefetch set of a record X;
 - one reads a non-column field on record X.

Fix method _read() to manage that case.  It adds an extra call to
exists() in that case, but adds no overhead in the general case.

closes odoo/odoo#108050

X-original-commit: 1876dc87e5c88c711c9b3882c39be704ffc46532
Signed-off-by: Raphael Collet <rco@odoo.com>
Signed-off-by: Rémy Voet <ryv@odoo.com>
2022-12-15 14:43:57 +01:00
Rémy Voet (ryv) 7f7006da02 [REM] stock_account: remove useless overwrite of read_group.
Force `group_operator` of `unit_cost` to be None to be able to
remove useless `read_group` override.

closes odoo/odoo#104863

Related: odoo/upgrade#4012
Related: odoo/enterprise#33568
Signed-off-by: William Henrotin (whe) <whe@odoo.com>
2022-11-28 14:51:00 +01:00
Rémy Voet (ryv) 29e9d99364 [FIX] core: field indexed by trigram cannot become translated
Field indexed by trigram cannot become translated because the
`convert_column_translatable` is based on the old index naming convention
(changed in https://github.com/odoo/odoo/pull/100736).
Then it generates a PostgreSQL error:
`psycopg2.errors.DatatypeMismatch: operator class "gin_trgm_ops" does not accept data type jsonb`

closes odoo/odoo#105295

Signed-off-by: Raphael Collet <rco@odoo.com>
2022-11-08 13:36:05 +01:00
Rémy Voet (ryv) 0787f150b1 [FIX] core: make index naming without conflicts
There are two issues with the index naming convention used by the ORM:

Problem 1: it is possible to have naming conflict for indexes.  For
instance, the name 'slide_channel_tag_group_sequence_index' is used for
both fields slide.channel.tag.group.sequence and
slide.channel.tag.group_sequence.  Only the first index will be created.

Solution 1: we separate the model and field names with a double
underscore instead of a single one, which is the same strategy as with
LEFT JOIN aliases.  This is correct because model names don't contain
such double underscores or underscores as prefix or suffix (it is not
forbiden but model name should follow the 'dot notation'.)

Problem 2: index names can be longer than 63 chars, but PostgreSQL
silently truncates it.  This doesn't actually break anything (PostgreSQL
also truncates values when we check the existence of indexes) but it can
lead to using the same name twice.  There is hopefully not any example
in our code.

Solution 2: if the name is too large, we truncate it and pad it with a
hash of the complete name to match 63 characters, which is also the
strategy used for LEFT JOIN aliases.

task-2984730

closes odoo/odoo#100736

Related: odoo/upgrade#3957
Signed-off-by: Raphael Collet <rco@odoo.com>
2022-11-07 15:57:28 +01:00
Rémy Voet (ryv)andJulien Castiaux f6c7ead359 [IMP] tests: RecordCapturer now keep the order of record creation
closes odoo/odoo#104838

Related: odoo/enterprise#33562
Signed-off-by: Rémy Voet <ryv@odoo.com>
Co-authored-by: Julien Castiaux <juc@odoo.com>
2022-11-07 11:26:33 +01:00
Rémy Voet (ryv) 550c97bab1 [REM] pos_loyalty: remove unused and incorrect method
`action_view_pos_orders` is unused and it is incorrect
because it generates a wrong domain (`('id', 'in', pos_order_ids)` where
`pos_order_ids` is a list of tuple(<id>, <display_name>))

Part-of: odoo/odoo#104838
2022-11-07 11:26:32 +01:00
Rémy Voet (ryv)andJulien Castiaux e967e25095 [IMP] *: remove bad usage of search_read.
Reading only 'id' with `search_read` is equivalent to use `search` but
complexify the result usage. Fix all these bad usages.

Part-of: odoo/odoo#104838
Co-authored-by: Julien Castiaux <juc@odoo.com>
2022-11-07 11:26:32 +01:00
Rémy Voet (ryv) f66fa9433f [MOV] core,base: move logic of O2MIdMapper
O2MIdMapper Class inherit of 'base' Model to change the behavior of
`create`.
Because it is in base and the `_import_current_module` is set in
models.py, move it at the end of create in models.py.

closes odoo/odoo#99550

Related: odoo/enterprise#33063
Signed-off-by: Vincent Schippefilt (vsc) <vsc@odoo.com>
2022-11-02 15:34:30 +01:00
Rémy Voet (ryv) 3353dfdb29 [REM] core,base,*: deprecated some methods to be remove correctly later
- Deprecated `norecompute` (on Environment class) because it is useless
and do nothing.
- Deprecated `cache_restart` (`res.company`) because `clear_caches` do
the stuff.
- Deprecated `write_company_and_print_report` (`res.company`) because
since https://github.com/odoo/odoo/pull/33863/, it is unused
- Deprecated `open_company_edit_report` (`res.company`) because since
25f7040998, it is unused.

Part-of: odoo/odoo#99550
2022-11-02 15:34:30 +01:00
Rémy Voet (ryv)andniyasraphy 998de25f51 [IMP] base: improve model description
closes odoo/odoo#104555

X-original-commit: 67323e4f0af942e60bc6332282315c811c7d7e24
Signed-off-by: Rémy Voet <ryv@odoo.com>
Co-authored-by: niyasraphy <niyasraphyk@gmail.com>
2022-11-02 10:40:19 +01:00
Rémy Voet (ryv) 9ec19fd59f [FIX] barcodes_gs1_nomenclature: fix QUnit test
'Barcode GS1 Parser' test sent RPC to the server (moreover
it was wrong rpc). Fix it.

closes odoo/odoo#102239

Signed-off-by: Steve Van Essche <svs@odoo.com>
2022-10-25 13:03:07 +02:00
Rémy Voet (ryv) 370cae07b8 [FIX] test_performance: adapt query count of one2many operations
Since https://github.com/odoo/odoo/pull/99415,
query count for one2many operation was too high, fix it.

closes odoo/odoo#103807

X-original-commit: 09acbbe9ca7cc524288589cb2f9dfcd30b244461
Signed-off-by: Vincent Schippefilt (vsc) <vsc@odoo.com>
Signed-off-by: Rémy Voet <ryv@odoo.com>
2022-10-24 08:22:45 +02:00
Rémy Voet (ryv) 1cfc896f2c [IMP] core: add Json field
To be able to create easily a jsonb column in database,
we create a Json type Field. Currently, it is quite limited
field:

- We cannot modified the value in-place, we need to always set
the entire jsonify value.
- No domain operator is done to work with jsonb. Now, it works as a
text field.

closes odoo/odoo#103097

X-original-commit: 7eeba9d205d2dace571b5d0895ddba6290a512db
Related: odoo/enterprise#32729
Signed-off-by: Quentin De Paoli <qdp@odoo.com>
2022-10-17 10:11:09 +02:00
Rémy Voet (ryv) 75686c7311 [IMP] core: small miscellaneous improvement of models.py
- In `unlink`, since https://github.com/odoo/odoo/pull/66938
modified is called on self for each batch of 1_000.
But it should be called on the batched records.
- In `write`, remove useless `records_to_inverse`
(there from ORM refactor but never used)
- make `_modified_triggers` more deterministic by
changing a `set` into `OrderedSet`.

closes odoo/odoo#100472

Signed-off-by: Raphael Collet <rco@odoo.com>
2022-09-23 22:04:04 +02:00
Rémy Voet (ryv) c80265b91c [IMP] stock: add index on stock.quant.package_id
Because `quant_ids` (`stock.quant.package`) is a one2many inverse of
`package_id` and some depends use it.
It is important to have in index on package_id on `stock.quant`

closes odoo/odoo#100371

Signed-off-by: Arnold Moyaux (arm) <arm@odoo.com>
2022-09-16 16:20:59 +02:00
Rémy Voet (ryv) 37a9a2e166 [REM] sql_db.py: clean file and our Cursor class
- Remove `_default_log_exceptions` of our `Cursor` class (unused except
in one test)
- Deprecated `serialized` args of `__init__` (Cursor class) and
`cursor()` (of Connection class), our cursor is always serialized.
- Remove `sql_log` attribute of Cursor class and replace it with
appropriate code to do the same stuff dynamically.
- Simplify some code
- Update some docstring
- Clean import

task-2766494

closes odoo/odoo#85078

Signed-off-by: Rémy Voet <ryv@odoo.com>
2022-09-09 11:11:59 +02:00